11
21
2015
0

POJ1269 -- Intersecting Lines

Intersecting Line
 
 

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

 
 
 
题目给定N组直线,要求判断每组两条直线间的关系(即相交,平行,共线),若两直线相交则求其交点。
 
 
用叉积判断两直线是否共线 (det=0则共线)
用斜率判断两直线是否平行 ((x1-x2)*(y3-y4)=(x3-x4)(y1-y2) 则平行)
对于相交的两直线,求其交点,wy并不会简洁描述QAQ,所以...详见 计算几何教程
 
#include<cstdio>
int N;
struct point{
	double x,y;
	point(double a=0,double b=0){
		x=a; y=b;
	}
	point operator +(const point &b)const {
		point c;
		c.x=x+b.x;
		c.y=y+b.y;
		return c;
	}
    point operator -(const point &b)const{
    	point c;
    	c.x=x-b.x;
    	c.y=y-b.y;
    	return c;
	}
	point operator *(double r)const{
		point c;
		c.x=x*r;
		c.y=y*r;
		return c;
	}	
	point operator /(double r)const{
		point c;
		c.x=x/r;
		c.y=y/r;
		return c;
	}
	double operator *(const point &b)const{
		return x*b.y-b.x*y;
	}
};
struct line{
	point a,b;
}l1,l2;
int check(const line &a,const line &b){
	if (!((a.b-a.a)*(b.a-a.a)) && !((a.b-a.a)*(b.b-a.a))) return -1;  //共线返回-1 
	if ((a.a.x-a.b.x)*(b.a.y-b.b.y)==(b.a.x-b.b.x)*(a.a.y-a.b.y)) return 0; //共线返回0
	return 1; 
}
point intersection(const line &a,const line &b){
	double r1=(b.b-a.a)*(b.a-a.a);
	double r2=(b.a-a.b)*(b.b-a.b);
	point c=a.a*r2+a.b*r1;
	return c/(r1+r2);
}
int main(){
	scanf("%d",&N);
	printf("INTERSECTING LINES OUTPUT\n");
	for (int i=1;i<=N;++i){
	    scanf("%lf%lf%lf%lf",&l1.a.x,&l1.a.y,&l1.b.x,&l1.b.y);
	    scanf("%lf%lf%lf%lf",&l2.a.x,&l2.a.y,&l2.b.x,&l2.b.y);
	    int sign=check(l1,l2);
	    if (sign<0){
	   	    printf("LINE\n");
	   	    continue;
	    }
	    if (!sign){
	    	printf("NONE\n");
	    	continue;
		}
		point F=intersection(l1,l2);
		printf("POINT %.2f %.2f\n",F.x,F.y);
	}
	printf("END OF OUTPUT\n");
} 

 

Ps:这道题有一个奇怪的问题,就是输出用%.2lf时WA了,应改用%.2f...

Category: POJ | Tags: 计算几何 POJ | Read Count: 320

登录 *


loading captcha image...
(输入验证码)
or Ctrl+Enter

Host by is-Programmer.com | Power by Chito 1.3.3 beta | Theme: Aeros 2.0 by TheBuckmaker.com