Intersecting Lines
来源:互联网 发布:网络约租车管理办法 编辑:程序博客网 时间:2024/05/16 07:35
源自POJ1269
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.
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
分析:判断先后顺序-是否共线->是否平行->相交
CODE:#include<stdio.h>#include<math.h>const double ERR = 0.000001;struct point{ double x; double y;};void get_ans(point a[]){ if((fabs((a[1].x-a[0].x)*(a[2].y-a[1].y)-(a[1].y-a[0].y)*(a[2].x-a[1].x))<=ERR)&&(fabs((a[1].x-a[0].x)*(a[3].y-a[1].y)-(a[1].y-a[0].y)*(a[3].x-a[1].x))<=ERR))//共线 printf("LINE\n"); else if(((a[0].x-a[1].x)*(a[2].y-a[3].y)-(a[0].y-a[1].y)*(a[2].x-a[3].x))==0)//平行 printf("NONE\n"); else//相交 { double a1=a[0].y-a[1].y,b1=a[1].x-a[0].x,c1=a[0].x*a[1].y-a[0].y*a[1].x; double a2=a[2].y-a[3].y,b2=a[3].x-a[2].x,c2=a[2].x*a[3].y-a[2].y*a[3].x; printf("POINT %.2lf %.2lf\n",(c1*b2-c2*b1)/(a2*b1-a1*b2),(a2*c1-a1*c2)/(a1*b2-a2*b1)); }}int main(){ int n; scanf("%d",&n); printf("INTERSECTING LINES OUTPUT\n"); while(n--) { point a[4]; for(int i=0;i<4;i++) scanf("%lf %lf",&a[i].x,&a[i].y); get_ans(a); } printf("END OF OUTPUT\n"); return 0;}
- Intersecting Lines
- Intersecting Lines
- Intersecting Lines
- Intersecting Lines
- zoj1820 Intersecting Lines
- POJ 1269 Intersecting Lines
- POJ 1269 Intersecting Lines
- poj 1269 Intersecting Lines
- poj 1269 Intersecting Lines
- poj 1269 Intersecting Lines
- POJ 1269 Intersecting Lines
- poj 1269 Intersecting Lines
- poj 1069 Intersecting Lines
- POJ 1269 Intersecting Lines
- POJ 1269 Intersecting Lines
- POJ 1269 Intersecting Lines
- poj 1269 Intersecting Lines
- poj 1269 Intersecting Lines
- 时隔四年的回归--写给高考学子以及大学还在迷茫的人
- rtl8187L驱动在linux2.6.35上的编译
- 网站标题关键词如何设置才能对排名有促进作用
- QT5杂谈之初体验
- Android源代码下载
- Intersecting Lines
- 2013年5月15日 00:06:46
- 600多种编程语言怎么学
- Java类变量和成员变量初始化过程
- hibernate缓存
- Android读取XML文件
- mysql 5.5 更改默认字符集
- mysql触发器
- 全选反选