Intersecting Lines (计算几何基础+判断两直线的位置关系)
题目链接:http://poj.org/problem?id=1269
题面:
Description
Your program will repeatedly read in four points that define two lines in the x-y plane and determine how and where the lines intersect. All numbers required by this problem will be reasonable, say between -1000 and 1000.
Input
Output
Sample Input
5
0 0 4 4 0 4 4 0
5 0 7 6 1 0 2 3
5 0 7 6 3 -6 4 -3
2 0 2 27 1 5 18 5
0 3 4 0 1 2 2 5
Sample Output
INTERSECTING LINES OUTPUT
POINT 2.00 2.00
NONE
LINE
POINT 2.00 5.00
POINT 1.07 2.20
END OF OUTPUT
思路:本题求的就是两条直线之间的位置关系,如果平行输出“NONE”,相交输出“POINT”和交点坐标,重合就输出“LINE”。判断两条直线是否平行则判断两条直线的单位方向向量是否相等或相反(即斜率是否相等),如果满足则是平行或重合,否则就是相交,相交就调用求交点的函数求出交点即可;而判断是否重合只需判断一条直线上的某一点是否在另一条直线上即可。
代码实现如下:
#include <cstdio>
#include <cmath>
#include <algorithm>
using namespace std; struct Point {
double x, y;
Point (double x = , double y = ) : x(x), y(y) {}
}; typedef Point Vector; int n;
Point A, B, C, D; Vector operator + (Vector A, Vector B) {
return Vector(A.x + B.x, A.y + B.y);
} Vector operator - (Vector A, Vector B) {
return Vector(A.x - B.x, A.y - B.y);
} Vector operator * (Vector A, double p) {
return Vector(A.x * p, A.y * p);
} bool operator < (const Point& a, const Point& b) {
return a.x < b.x || (a.x == b.x && a.y < b.y);
} const double eps = 1e-;
int dcmp(double x) {
if(fabs(x) < eps)
return ;
else
return x < ? - : ;
} bool operator == (const Point& a, const Point& b) {
return dcmp(a.x - b.x) == && dcmp(a.y - b.y) == ;
} double Dot(Vector A, Vector B) {
return A.x * B.x + A.y * B.y;
} double Length(Vector A) {
return sqrt(Dot(A, A));
} double Cross(Vector A, Vector B) {
return A.x * B.y - A.y * B.x;
} //求单位方向向量
Vector Unit_direction_vector(Vector w) {
return Vector(w.x / Length(w), w.y / Length(w));
} //判断两直线是否不相交
bool isIntersection(Vector A, Vector B) {
return Unit_direction_vector(A) == Unit_direction_vector(B) || Unit_direction_vector(Vector(- A.x, - A.y)) == Unit_direction_vector(B);
} Point GetLineIntersection(Point P, Vector v, Point Q, Vector w) {
Vector u = P - Q;
double t = Cross (w, u) / Cross(v, w);
return P + v * t;
} //判断两直线是否重合只要判断是否有公共点即可
bool OnLine(Point p, Point a1, Point a2) {
return dcmp(Cross(a1 - p, a2 - p)) == ;
} int main() {
while(~scanf("%d", &n)) {
printf("INTERSECTING LINES OUTPUT\n");
while(n--) {
scanf("%lf%lf%lf%lf%lf%lf%lf%lf", &A.x, &A.y, &B.x, &B.y, &C.x, &C.y, &D.x, &D.y);
if(isIntersection(A - B, C - D)) {
if(OnLine(A, C, D)) {
printf("LINE\n");
} else {
printf("NONE\n");
}
} else {
Point P = GetLineIntersection(A, A - B, C, C - D);
printf("POINT %.2f %.2f\n", P.x, P.y);
}
}
printf("END OF OUTPUT\n");
}
}
Intersecting Lines (计算几何基础+判断两直线的位置关系)的更多相关文章
- TOYS(计算几何基础+点与直线的位置关系)
题目链接:http://poj.org/problem?id=2318 题面: TOYS Time Limit: 2000MS Memory Limit: 65536K Total Submiss ...
- Intersecting Lines---poj1269(求两直线的位置关系)
题目链接:http://poj.org/problem?id=1269 题意:给你两条直线上的任意不同的两点,然后求两条直线的位置关系,如果相交于一点输出该点坐标; #include<iostr ...
- POJ P2318 TOYS与POJ P1269 Intersecting Lines——计算几何入门题两道
rt,计算几何入门: TOYS Calculate the number of toys that land in each bin of a partitioned toy box. Mom and ...
- POJ 1269 Intersecting Lines(判断两直线位置关系)
题目传送门:POJ 1269 Intersecting Lines Description We all know that a pair of distinct points on a plane ...
- poj 1269 判断直线的位置关系
题目链接 题意 判断两条直线的位置关系,重合/平行/相交(求交点). 直线以其上两点的形式给出(点坐标为整点). 思路 写出直线的一般式方程(用\(gcd\)化为最简), 计算\(\begin{vma ...
- 判断两条直线的位置关系 POJ 1269 Intersecting Lines
两条直线可能有三种关系:1.共线 2.平行(不包括共线) 3.相交. 那给定两条直线怎么判断他们的位置关系呢.还是用到向量的叉积 例题:POJ 1269 题意:这道题是给定四个点p1, ...
- POJ 1269 /// 判断两条直线的位置关系
题目大意: t个测试用例 每次给出一对直线的两点 判断直线的相对关系 平行输出NODE 重合输出LINE 相交输出POINT和交点坐标 1.直线平行 两向量叉积为0 2.求两直线ab与cd交点 设直线 ...
- 【POJ 1269】判断两直线相交
题 利用叉积解方程 #include <cstdio> #define MAX 1<<31 #define dd double int xmult(dd x1,dd y1,dd ...
- [置顶] 如何判断两个IP大小关系及是否在同一个网段中
功能点 判断某个IP地址是否合法 判断两个IP地址是否在同一个网段中 判断两个IP地址的大小关系 知识准备 IP协议 子网掩码 Java 正则表达式 基本原理 IP地址范围 0.0.0.0- 255 ...
随机推荐
- epoll&ext4文件系统
1.第一个终端运行nesttest,nesttest首先fork一个子进程,然后父进程退出,子进程首先打开一个txt普通文件对应fd为3,然后创建一个epfd,对应fd为4 lybxin@Inspir ...
- GPS定位,根据经纬度查询附近地点的经纬度-sql方法实现
根据当前所在的坐标点也即经纬度,查找数据库中附近5公里或10公里附近的所有信息的实现,经过查找资料,原来是我高二学的,就是求弦长,数学忘完了,没想到数学还这么有用,数学啊 真是用途太大了. 用到的什么 ...
- VUE01指令
一.下载Vue2.0的两个版本: 官方网站:http://vuejs.org/ 开发版本:包含完整的警告和调试模式 生产版本:删除了警告,进行了压缩 二.项目结构搭建 这个部分要视频中有详细讲解. 三 ...
- 将sublime添加到右键菜单
sublime text 添加到鼠标右键功能: 把以下内容复制并保存到文件,重命名为:sublime_addright.reg,然后双击就可以了. (注意:需要把下面代码中的Sublime的安装目录( ...
- perf 是怎么计算调用栈的时间的?
在我真个malloc的执行过程中共调用了8次的syswrite的系统调用,其中有两次来自于__lib_write, 两次来自于__memmove_avx_unaligned,然后__memmove_a ...
- Sitemesh小记
一.前言 因参与公司框架改造,接触到了Sitemesh这个用于网页布局和修饰的框架,因之前没有接触过(汗颜),但是发现其小巧好用,便以此文记之~ 二.正文 Sitemesh有什么作用呢?我相信很多人在 ...
- OBJ文件
OBJ文件是Alias|Wavefront公司为它的一套基于工作站的3D建模和动画软件"Advanced Visualizer"开发的一种标准3D模型文件格式,很适合用于3D软件模 ...
- RT-thread内核之IO设备管理系统
RT-Thread系统的IO设备管理模块为上层应用提供了一个对设备进行访问的通用抽象接口,而对于下层设备来说则提供了底层设备驱动框架,并通过定义的数据结构对设备信息和底层设备驱动进行管理.从系统整体位 ...
- 【刷题】BZOJ 3527 [Zjoi2014]力
Description 给出n个数qi,给出Fj的定义如下: 令Ei=Fi/qi,求Ei. Input 第一行一个整数n. 接下来n行每行输入一个数,第i行表示qi. n≤100000,0<qi ...
- [BZOJ5303] [HAOI2018] 反色游戏
题目链接 LOJ:https://loj.ac/problem/2524 BZOJ:https://lydsy.com/JudgeOnline/problem.php?id=5303 洛谷:https ...