POJ 1269 Intersecting Lines

来源:互联网 发布:python 如何读入文件 编辑:程序博客网 时间:2024/05/16 19:22
Intersecting Lines
Time Limit: 1000MS Memory Limit: 10000KTotal Submissions: 6233 Accepted: 2939

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

Source

Mid-Atlantic 1996


求俩直线关系的,相交,平行,或重合。

基础的计算几何~一点点建立起计算几何的自信~

推了下求交点的公式

首先看图


两直线AB,CD相交于O点

设q = 向量AC与向量DC的叉积 = AG * CD

    p = 向量AB与向量DC的叉积 = (AG + BH) * CD

得AG / (AG + BH) = q / p

因为BHO和AGO是相似三角形,所以

得 AO / AB = AO / (AO + OB) = AG / (AG + BH) = q / p

于是AO = AB * q / p

即O.x - A.x = (B.x - A.x) * q / p

    O.y - A.y = (B.y - A.y) * q / p


剩下的

两直线重合:

C,D点一定在AB上,所以,(X表示叉积)

CB X AB = DB X AB = 0

两直线平行:

AB X CD = 0

#include <stdio.h>#include <stdlib.h>#include <string.h>const double eps = 0.000001;struct LPoint{double x, y;};struct LLine{LPoint bg, ed;};int dblcmp(double a, double b){if (a - b > eps) return 1;if (b - a > eps) return -1;return 0;}double VecPro(double a, double b, double p, double q){return (a * q) - (b * p);}int main(){int n;double p, q, t;LLine a, b;LPoint o;scanf("%d", &n);printf("INTERSECTING LINES OUTPUT\n");while(n--){scanf("%lf%lf%lf%lf", &a.bg.x, &a.bg.y, &a.ed.x, &a.ed.y);scanf("%lf%lf%lf%lf", &b.bg.x, &b.bg.y, &b.ed.x, &b.ed.y);p = VecPro(a.bg.x - a.ed.x, a.bg.y - a.ed.y, b.bg.x - a.ed.x, b.bg.y - a.ed.y);q = VecPro(a.bg.x - a.ed.x, a.bg.y - a.ed.y, b.ed.x - a.ed.x, b.ed.y - a.ed.y);t = VecPro(a.bg.x - a.ed.x, a.bg.y - a.ed.y, b.bg.x - b.ed.x, b.bg.y - b.ed.y);if (dblcmp(p, 0) == 0 && dblcmp(q, 0) == 0){printf("LINE\n");}else if (dblcmp(t, 0) == 0){printf("NONE\n");}else{p = VecPro(b.ed.x - b.bg.x, b.ed.y - b.bg.y, a.bg.x - b.bg.x, a.bg.y - b.bg.y);p = p / t;o.x = a.bg.x + p * (a.ed.x - a.bg.x);o.y = a.bg.y + p * (a.ed.y - a.bg.y);printf("POINT %0.2f %0.2f\n", o.x, o.y);}}printf("END OF OUTPUT\n");return 0;}