Intersecting Lines POJ - 1269

http://poj.org/problem?id=1269

Cross product is really a tool for computational geometry

First, for two straight lines (p1 p2) (p3 p4), if the two straight lines are collinear, then the cross product modulus of p3-p1 and p2-p1 is 0 and the cross product modulus of p4-p1 and p2-p1 is 0

Otherwise, use the slope to judge the parallel

Finally, if the intersection is calculated and the intersection point is set as p0, then the cross-product modulus of p1-p0 and p2-p0 is 0, and the cross-product modulus of p3-p0 and p4-p0 is 0

After the combination, the two linear equations a1*x+b1*y+c1=0 and a2*x+b2*y+c2=0 can be solved directly by Cramer's rule

 

#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
using namespace std;
const double eps=1e-8;

double getval(double x1,double y1,double x2,double y2,double x3,double y3)
{
    return (x2-x1)*(y3-y1)-(x3-x1)*(y2-y1);
}

int main()
{
    double x1,y1,x2,y2,x3,y3,x4,y4;
    double a1,b1,c1,a2,b2,c2,x,y;
    int t;
    scanf("%d",&t);
    printf("INTERSECTING LINES OUTPUT\n");
    while(t--){
        scanf("%lf%lf%lf%lf%lf%lf%lf%lf",&x1,&y1,&x2,&y2,&x3,&y3,&x4,&y4);
        if(fabs(getval(x3,y3,x1,y1,x2,y2))<eps&&fabs(getval(x4,y4,x1,y1,x2,y2))<eps) printf("LINE\n");
        else if(fabs((y2-y1)*(x4-x3)-(y4-y3)*(x2-x1))<eps) printf("NONE\n");
        else{
            a1=y1-y2,b1=x2-x1,c1=x1*y2-x2*y1;
            a2=y3-y4,b2=x4-x3,c2=x3*y4-x4*y3;
            x=(-c1*b2+b1*c2)/(a1*b2-a2*b1);
            y=(-a1*c2+a2*c1)/(a1*b2-a2*b1);
            printf("POINT %.2f %.2f\n",x,y);
        }
    }
    printf("END OF OUTPUT\n");
    return 0;
}

/*
1
5 0 7 6 3 -6 4 -3
*/

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325479581&siteId=291194637