POJ_1269_Intersecting_Lines_(计算几何基础)

描述


http://poj.org/problem?id=1269

给出两条直线,判断它们是平行,重合,还是相交,如果相交,求出交点.

 

分析


比较裸的一道题.学习了直线的写法(参数方程)

 

 

 1 #include <cstdio>
 2 #include <cmath>
 3 using namespace std;
 4 
 5 const double eps=1e-8;
 6 
 7 struct pt{ double x,y; pt(double x=0,double y=0):x(x),y(y){} };
 8 typedef pt vt;
 9 int dcmp(double x){ if(fabs(x)<eps) return 0; return x>0?1:-1; }
10 vt operator + (vt a,vt b){ return vt(a.x+b.x,a.y+b.y); }
11 vt operator - (vt a,vt b){ return vt(a.x-b.x,a.y-b.y); }
12 vt operator * (vt a,double p){ return vt(a.x*p,a.y*p); }
13 double cross(vt a,vt b){ return a.x*b.y-a.y*b.x; }
14 struct line{
15     pt p; vt v;
16     line(){}
17     line(pt a,pt b){ p=a; v=b-a; }
18 };
19 int line_intersection(line A,line B){
20     if(dcmp(cross(A.v,B.v)!=0)) return -1;
21     return dcmp(cross(A.v,A.p-B.p))==0;
22 }
23 pt get_line_intersection(line A,line B){
24     vt v=A.v,w=B.v,u=A.p-B.p;
25     double t=cross(w,u)/cross(v,w);
26     return A.p+v*t;
27 }
28 int main(){
29     int n;
30     scanf("%d",&n);
31     puts("INTERSECTING LINES OUTPUT");
32     while(n--){
33         pt p[4]; line l[2];
34         for(int i=0;i<4;i++) scanf("%lf%lf",&p[i].x,&p[i].y);
35         l[0]=line(p[0],p[1]);
36         l[1]=line(p[2],p[3]);
37         int t=line_intersection(l[0],l[1]);
38         if(t==-1){
39             pt x=get_line_intersection(l[0],l[1]);
40             printf("POINT %.2lf %.2lf\n",x.x,x.y);
41         }
42         else if(t==1) puts("LINE");
43         else puts("NONE");
44     }
45     puts("END OF OUTPUT");
46     return 0;
47 }
View Code

 

Intersecting Lines
Time Limit: 1000MS   Memory Limit: 10000K
Total Submissions: 13622   Accepted: 6060

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

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

Source

 

posted @ 2016-05-27 23:08  晴歌。  阅读(219)  评论(0编辑  收藏  举报