Intersecting Lines(计算几何基础)

来源:互联网 发布:淘宝自定义代码 编辑:程序博客网 时间:2024/06/07 22:04

题目来源:[NWPU][2014][TRN][19]二维计算几何基础

http://vjudge.net/vjudge/contest/view.action?cid=54080#problem/D

作者:npufz

题目:

D - Intersecting Lines
Time Limit:1000MS     Memory Limit:10000KB     64bit IO Format:%I64d & %I64u
Submit Status

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

代码:

#include <iostream>#include <cstdio>#include <cmath>#include <cstring>using namespace std;typedef  struct   point{    double x,y;}poi;int intersection(point be[2],point ed[2]){    double  x,y,k1,b1,k2,b2;    if((ed[0].x-be[0].x)*(ed[1].y-be[1].y)==(ed[0].y-be[0].y)*(ed[1].x-be[1].x))    {        if((ed[0].x-be[0].x)*(be[1].y-be[0].y)==(ed[0].y-be[0].y)*(be[1].x-be[0].x))        printf("LINE\n");        else        printf("NONE\n");     }    else    {        /*if()        f2=ed[0].x*ed[1].x*(be[1].y-be[0].y)+           ed[0].x*be[1].x*(be[0].y-ed[1].y)+           be[0].x*ed[1].x*(ed[0].y-be[1].y)+           be[0].x*be[1].x*(ed[1].y-ed[0].y);        f1=(be[1].y*ed[0].x+be[0].x*ed[1].y-            ed[0].y*be[1].x-be[0].y*ed[1].x);        x=f2/f1;        y=*/       if(be[0].x==ed[0].x)       {           x=be[0].x;           y=be[1].y+(ed[1].y-be[1].y)*(x-be[1].x)/(ed[1].x-be[1].x);       }       else if(be[1].x==ed[1].x)       {           x=be[1].x;           y=be[0].y+(ed[0].y-be[0].y)*(x-be[0].x)/(ed[0].x-be[0].x);       }       else       {          k1=(ed[0].y-be[0].y)/(ed[0].x-be[0].x);          b1=be[0].y-k1*be[0].x;          k2=(ed[1].y-be[1].y)/(ed[1].x-be[1].x);          b2=be[1].y-k2*be[1].x;          x=(b2-b1)/(k1-k2);          y=k1*x+b1;        }     printf("POINT %.2lf %.2lf\n",x,y);    }return 0;}int main(){    int i,t;    poi  be[2],ed[2];    scanf("%d",&t);    for(i=1;i<=t;i++)    {        if(i==1) printf("INTERSECTING LINES OUTPUT\n");        scanf("%lf%lf%lf%lf",&be[0].x,&be[0].y,&ed[0].x,&ed[0].y);        scanf("%lf%lf%lf%lf",&be[1].x,&be[1].y,&ed[1].x,&ed[1].y);        intersection(be,ed);    }   printf("END OF OUTPUT\n"); return 0;}
反思:一开始直接进行了解析求解,发现可能有分母为零的情况,就放弃了

0 0
原创粉丝点击