POJ 1269 Intersecting Lines(计算几何)

来源:互联网 发布:bilibili mac版下载 编辑:程序博客网 时间:2024/05/24 08:34

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

题意:给定4个点的坐标,前2个点是一条线,后2个点是另一条线,求这两条线的关系,如果相交,就输出交点。

题解:先判断是否共线,我用的是叉积的性质,用了2遍就可以判断4个点是否共线了,在用斜率判断是否平行,最后就是相交了,求交点就好了。

求交点的过程和高中知识差不多,用y=kx+c来求,只不过要注意斜率不存在的时候特殊处理,还有就是求斜率的时候一定要强制转换,(坑爹的我,调试了一小时才找到这个bug抓狂抓狂抓狂

AC代码:

#include <map>#include <set>#include <list>#include <cmath>#include <queue>#include <stack>#include <vector>#include <cstdio>#include <string>#include <cstdlib>#include <cstring>#include <iostream>#include <algorithm>using namespace std;const int INF=0x3f3f3f3f;typedef long long ll;typedef unsigned long long ull;#define prN printf("\n")#define SI(N) scanf("%d",&(N))#define SII(N,M) scanf("%d%d",&(N),&(M))#define SIII(N,M,K) scanf("%d%d%d",&(N),&(M),&(K))#define cle(a,val) memset(a,(val),sizeof(a))#define rep(i,b) for(int i=0;i<(b);i++)#define Rep(i,a,b) for(int i=(a);i<=(b);i++)#define reRep(i,a,b) for(int i=(a);i>=(b);i--)const int MAX_N= 1 ;const int EPS= 1e-9 ;int a[10][8],n;int Multic(int x0,int y0,int x1,int y1,int x2,int y2){    int a1=x1-x0,b1=y1-y0;    int a2=x2-x0,b2=y2-y0;    return a1*b2-a2*b1;}void sol(int row){    int te1=Multic(a[row][0],a[row][1],a[row][4],a[row][5],a[row][2],a[row][3]);    int te2=Multic(a[row][0],a[row][1],a[row][6],a[row][7],a[row][2],a[row][3]);    if (te1==0&&te2==0)    {        puts("LINE");        return;    }    if ((a[row][1]-a[row][3])*(a[row][4]-a[row][6])==            (a[row][0]-a[row][2])*(a[row][5]-a[row][7]))    {        puts("NONE");        return;    }    if (a[row][0]-a[row][2]==0||(a[row][4]-a[row][6])==0)    {        if (a[row][0]-a[row][2]==0)        {            double ansx=a[row][0];            double k2=(a[row][5]-a[row][7])/(double)(a[row][4]-a[row][6]);            double c2=a[row][7]-k2*a[row][6];            double ansy=k2*ansx+c2;            printf("POINT %.2f %.2f\n",ansx,ansy);        }        if ((a[row][4]-a[row][6])==0)        {            double ansx=a[row][4];            double k1=(a[row][1]-a[row][3])/(double)(a[row][0]-a[row][2]);            double c1=a[row][1]-k1*a[row][0];            double ansy=k1*ansx+c1;        }        return;    }    double k1=(a[row][1]-a[row][3])/(double)(a[row][0]-a[row][2]);///一定要注意这 ,要强制类型转换!!!!!!!!!!!!    double k2=(a[row][5]-a[row][7])/(double)(a[row][4]-a[row][6]);///否则就是int除int了        double c1=a[row][1]-k1*a[row][0];    double c2=a[row][7]-k2*a[row][6];        double ansx=(c2-c1)/(k1-k2);    double ansy=k1*ansx+c1;        printf("POINT %.2f %.2f\n",ansx,ansy);}int main(){    SI(n);    rep(i,n)    rep(j,8)    SI(a[i][j]);    puts("INTERSECTING LINES OUTPUT");    rep(i,n)    sol(i);    puts("END OF OUTPUT");    return 0;}

0 0
原创粉丝点击