现在的位置: 首页 > 综合 > 正文

POJ 1269 Intersecting Lines(计算几何 直线交点 点在直线上 直线平行)

2013年09月04日 ⁄ 综合 ⁄ 共 2954字 ⁄ 字号 评论关闭
Intersecting Lines
Time Limit: 1000MS   Memory Limit: 10000K
Total Submissions: 8417   Accepted: 3821

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

这个题目是计算几何的入门题目

解题步骤是先判断两个直线是否平行,如果平行,那么判断两条直线是否重合输出结果

如果不平行求出两直线交点,输出结果

注意精度的要求

#include <iostream>
#include <stdio.h>
#include <algorithm>
#include <cmath>
using namespace std;
#define eps 1e-8
struct point
{
double x;
double y;
};
struct line
{
point a;
point b;
};
line line_a,line_b;
bool zero(double a)//判断结果是否为0
{
return fabs(a) <= eps;
}
bool parallel(line &u,line &v)//判断直线u和直线v是否平行
{
return zero((u.a.x-u.b.x)*(v.a.y-v.b.y) - (v.a.x-v.b.x)*(u.a.y-u.b.y));
}
bool parallel(point &u1,point &u2,point &v1,point &v2)//判断直线u1 u2 and v1 v2 是否平行
{
return zero((u1.x-u2.x)*(v1.y-v2.y)-(v1.x-v2.x)*(u1.y-u2.y));
}
bool dot_in_line(point &a,point &b,point &c)//判断点a是否在线段bc上
{
return parallel(b,a,a,c);//就是判断线段b a 和a c 是否平行
}
point intersection(line &u,line &v)
{
point ret=u.a;
double t=((u.a.x-v.a.x)*(v.a.y-v.b.y) - (u.a.y-v.a.y)*(v.a.x-v.b.x))/((u.a.x-u.b.x)*(v.a.y-v.b.y)-(u.a.y-u.b.y)*(v.a.x-v.b.x));
ret.x+=(u.b.x-u.a.x)*t;
ret.y+=(u.b.y-u.a.y)*t;
return ret;
}
int find_ans()
{
point p;
if(parallel(line_a,line_b))
{
if(dot_in_line(line_a.a,line_b.a,line_b.b))
{ printf("LINE\n"); return 0; }
else
{ printf("NONE\n"); return 0; }
}
else
{
p=intersection(line_a,line_b);
printf("POINT %.2lf %.2lf\n",p.x,p.y);
return 0;
}
return 0;
}
int main()
{
int i,j,t;
scanf("%d",&t);
printf("INTERSECTING LINES OUTPUT\n");
while(t--)
{
scanf("%lf%lf%lf%lf%lf%lf%lf%lf",&line_a.a.x,&line_a.a.y,&line_a.b.x,&line_a.b.y,&line_b.a.x,&line_b.a.y,&line_b.b.x,&line_b.b.y);
find_ans();
}
printf("END OF OUTPUT\n");
return 0;
}

抱歉!评论已关闭.