博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Intersecting Lines (计算几何基础+判断两直线的位置关系)
阅读量:5312 次
发布时间:2019-06-14

本文共 4488 字,大约阅读时间需要 14 分钟。

题目链接:

题面:

Description

We all know that a pair of distinct points on a plane defines a line and that a pair of lines on a plane will intersect in one of three ways: 1) no intersection because they are parallel, 2) intersect in a line because they are on top of one another (i.e. they are the same line), 3) intersect in a point. In this problem you will use your algebraic knowledge to create a program that determines how and where two lines intersect.
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

The first line contains an integer N between 1 and 10 describing how many pairs of lines are represented. The next N lines will each contain eight integers. These integers represent the coordinates of four points on the plane in the order x1y1x2y2x3y3x4y4. Thus each of these input lines represents two lines on the plane: the line through (x1,y1) and (x2,y2) and the line through (x3,y3) and (x4,y4). The point (x1,y1) is always distinct from (x2,y2). Likewise with (x3,y3) and (x4,y4).

Output

There should be N+2 lines of output. The first line of output should read INTERSECTING LINES OUTPUT. There will then be one line of output for each pair of planar lines represented by a line of input, describing how the lines intersect: none, line, or point. If the intersection is a point then your program should output the x and y coordinates of the point, correct to two decimal places. The final line of output should read "END OF OUTPUT".

Sample Input

50 0 4 4 0 4 4 05 0 7 6 1 0 2 35 0 7 6 3 -6 4 -32 0 2 27 1 5 18 50 3 4 0 1 2 2 5

Sample Output

INTERSECTING LINES OUTPUTPOINT 2.00 2.00NONELINEPOINT 2.00 5.00POINT 1.07 2.20END OF OUTPUT 思路:本题求的就是两条直线之间的位置关系,如果平行输出“NONE”,相交输出“POINT”和交点坐标,重合就输出“LINE”。判断两条直线是否平行则判断两条直线的单位方向向量是否相等或相反(即斜率是否相等),如果满足则是平行或重合,否则就是相交,相交就调用求交点的函数求出交点即可;而判断是否重合只需判断一条直线上的某一点是否在另一条直线上即可。 代码实现如下:
1 #include 
2 #include
3 #include
4 using namespace std; 5 6 struct Point { 7 double x, y; 8 Point (double x = 0, double y = 0) : x(x), y(y) {} 9 };10 11 typedef Point Vector;12 13 int n;14 Point A, B, C, D;15 16 Vector operator + (Vector A, Vector B) {17 return Vector(A.x + B.x, A.y + B.y);18 }19 20 Vector operator - (Vector A, Vector B) {21 return Vector(A.x - B.x, A.y - B.y);22 }23 24 Vector operator * (Vector A, double p) {25 return Vector(A.x * p, A.y * p);26 }27 28 bool operator < (const Point& a, const Point& b) {29 return a.x < b.x || (a.x == b.x && a.y < b.y);30 }31 32 const double eps = 1e-10;33 int dcmp(double x) {34 if(fabs(x) < eps)35 return 0;36 else37 return x < 0 ? -1 : 1;38 }39 40 bool operator == (const Point& a, const Point& b) {41 return dcmp(a.x - b.x) == 0 && dcmp(a.y - b.y) == 0;42 }43 44 double Dot(Vector A, Vector B) {45 return A.x * B.x + A.y * B.y;46 }47 48 double Length(Vector A) {49 return sqrt(Dot(A, A));50 }51 52 double Cross(Vector A, Vector B) {53 return A.x * B.y - A.y * B.x;54 }55 56 //求单位方向向量57 Vector Unit_direction_vector(Vector w) {58 return Vector(w.x / Length(w), w.y / Length(w));59 }60 61 //判断两直线是否不相交62 bool isIntersection(Vector A, Vector B) {63 return Unit_direction_vector(A) == Unit_direction_vector(B) || Unit_direction_vector(Vector(- A.x, - A.y)) == Unit_direction_vector(B);64 }65 66 Point GetLineIntersection(Point P, Vector v, Point Q, Vector w) {67 Vector u = P - Q;68 double t = Cross (w, u) / Cross(v, w);69 return P + v * t;70 }71 72 //判断两直线是否重合只要判断是否有公共点即可73 bool OnLine(Point p, Point a1, Point a2) {74 return dcmp(Cross(a1 - p, a2 - p)) == 0;75 }76 77 78 int main() {79 while(~scanf("%d", &n)) {80 printf("INTERSECTING LINES OUTPUT\n");81 while(n--) {82 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);83 if(isIntersection(A - B, C - D)) {84 if(OnLine(A, C, D)) {85 printf("LINE\n");86 } else {87 printf("NONE\n");88 }89 } else {90 Point P = GetLineIntersection(A, A - B, C, C - D);91 printf("POINT %.2f %.2f\n", P.x, P.y);92 }93 }94 printf("END OF OUTPUT\n");95 }96 }

 

转载于:https://www.cnblogs.com/Dillonh/p/8795184.html

你可能感兴趣的文章
转载 python多重继承C3算法
查看>>
【题解】 bzoj1597: [Usaco2008 Mar]土地购买 (动态规划+斜率优化)
查看>>
css文本溢出显示省略号
查看>>
git安装和简单配置
查看>>
面向对象:反射,双下方法
查看>>
鼠标悬停提示文本消息最简单的做法
查看>>
Java面向对象重要关键字
查看>>
课后作业-阅读任务-阅读提问-2
查看>>
面向对象设计中private,public,protected的访问控制原则及静态代码块的初始化顺序...
查看>>
fat32转ntfs ,Win7系统提示对于目标文件系统文件过大解决教程
查看>>
500 Lines or Less
查看>>
adb devices unauthorized的解决办法
查看>>
ubuntu qq
查看>>
串口调试工具
查看>>
Awesome Adb——一份超全超详细的 ADB 用法大全
查看>>
shell cat 合并文件,合并数据库sql文件
查看>>
通过adb命令查看SN、CID码等信息
查看>>
linux 常用shell命令之wc
查看>>
win 解除鼠标右键关联
查看>>
Android 将drawable下的图片转换成bitmap、Drawable
查看>>