poj1269 (intersection of the line of the cross product demand)

Topic link: https: //vjudge.net/problem/POJ-1269

The meaning of problems: given four vertices, represent two straight lines, find where the two lines intersect, coincidence output LINE, NONE parallel output, intersect at a point away from the output point.

Ideas:

  Analyzing and coincides with the cross product of parallel lines, cross-product and can be used to find the intersections of the straight line intersect.

  Template cross product to find the point of intersection of the straight line:

double t=((a-c)^(c-d))/((a-b)^(c-d));
ans=Point(a.x+(b.x-a.x)*t,a.y+(b.y-a.y)*t)

  See proof https://www.cnblogs.com/jklover/p/10484313.html.

AC code:

#include<cstdio>
#include<algorithm>
#include<cmath>
#include<cstdlib>
using namespace std;

const double eps=1e-8;
int T,flag;
double x1,yy1,x2,yy2,x3,yy3,x4,yy4;

struct Point{
    double x,y;
    Point(){}
    Point(double xx,double yy):x(xx),y(yy){}
    Point operator + (const Point& b)const{
        return Point(x+b.x,y+b.y);
    }
    Point operator - (const Point& b)const{
        return Point(x-b.x,y-b.y);
    }
    double operator * (const Point& b)const{
        return x*b.x+y*b.y;
    }
    double operator ^ (const Point& b)const{
        return x*b.y-b.x*y;
    }
}ans;

struct Line{
    Point s,e;
    Line(){};
    Line(Point ss,Point ee){
        s=ss,e=ee;
    }
};

int sgn(double x)
{
    if(abs(x) < eps)return 0;
    if(x < 0)return -1;
    else return 1;
}

void solve(){
    Point a=Point(x1,yy1);
    Point b=Point(x2,yy2);
    Point c=Point(x3,yy3);
    Point d=Point(x4,yy4);
    if(sgn((b-a)^(d-c))==0){
        if(sgn((c-a)^(d-a))==0) flag=0;
        else flag=1;
        return;
    }
    flag=2;
    double t=((a-c)^(c-d))/((a-b)^(c-d));
    ans=Point(a.x+(b.x-a.x)*t,a.y+(b.y-a.y)*t);
}

int main(){
    scanf("%d",&T);
    printf("INTERSECTING LINES OUTPUT\n");
    while(T--){
        scanf("%lf%lf%lf%lf%lf%lf%lf%lf",&x1,&yy1,&x2,&yy2,&x3,&yy3,&x4,&yy4);
        solve();
        if(flag==0) printf("LINE\n");
        else if(flag==1) printf("NONE\n");
        else printf("POINT %.2f %.2f\n",ans.x,ans.y);
    }
    printf("END OF OUTPUT\n");
    return 0;
}

 

  

Guess you like

Origin www.cnblogs.com/FrankChen831X/p/11479672.html