POJ 1269 Intersecting Lines(计算几何 求交点)

来源:互联网 发布:扬尘检测仪规定数据 编辑:程序博客网 时间:2024/05/18 21:06

题意: 求共线,平行或交点

#include <iostream>#include <cstdio>#include <cstring>#include <algorithm>#include <cmath>#include <queue>using namespace std;const double EPS = 1e-6;const double INF = 1e20;bool zero(double a){return -EPS<a&&a<EPS;}struct cvector{    double x,y;    cvector(double a,double b){x=a,y=b;}    cvector(){}};cvector operator+(cvector a,cvector b){    return cvector(a.x+b.x,a.y+b.y);}cvector operator-(cvector a,cvector b){    return cvector(a.x-b.x,a.y-b.y);}double operator*(cvector a,cvector b){    return a.x*b.x+a.y*b.y;}cvector operator*(double a,cvector b){    return cvector(a*b.x,a*b.y);}double operator^(cvector a,cvector b){    return a.x*b.y-b.x*a.y;}double length(double t){return t>0?t:-t;}double length(cvector a){return sqrt(a*a);}struct cpoint{    double x,y;    cpoint(double a,double b){x=a,y=b;}    cpoint(){}};cvector operator-(cpoint a,cpoint b){    return cvector(b.x-a.x,b.y-a.y);}double dist(cpoint a,cpoint b){    return length(b-a);}struct cline{    cpoint a,b;} l1,l2;bool parallel(cline a,cline b){    return zero((a.a-a.b)^(b.a-b.b));}double dist(cpoint a,cline b){    return length((a-b.a)^(b.b-b.a))/length(b.b-b.a);}cpoint intersect(cline a,cline b){    double x,y;    y=((b.a.x-a.a.x)*(a.b.y-a.a.y)*(b.b.y-b.a.y)+(a.b.x-a.a.x)*(b.b.y-b.a.y)*a.a.y-(a.b.y-a.a.y)*(b.b.x-b.a.x)*b.a.y)/((a.b-a.a)^(b.b-b.a));    x=((b.a.y-a.a.y)*(a.b.x-a.a.x)*(b.b.x-b.a.x)+(a.b.y-a.a.y)*(b.b.x-b.a.x)*a.a.x-(a.b.x-a.a.x)*(b.b.y-b.a.y)*b.a.x)/((b.b-b.a)^(a.b-a.a));    return cpoint(x,y);}int main(){    freopen("in.txt","r",stdin);    int cas;    printf("INTERSECTING LINES OUTPUT\n");    scanf("%d",&cas);    while(cas--)    {        scanf("%lf%lf%lf%lf%lf%lf%lf%lf",&l1.a.x,&l1.a.y,&l1.b.x,&l1.b.y,&l2.a.x,&l2.a.y,&l2.b.x,&l2.b.y);        if(parallel(l1,l2))        {            if(zero(dist(l1.a,l2)))            printf("LINE\n");            else            printf("NONE\n");        }        else        {            cpoint t = intersect(l1,l2);            printf("POINT %.2lf %.2lf\n",t.x,t.y);        }    }    printf("END OF OUTPUT\n");    return 0;}


原创粉丝点击