POJ 1269 Intersecting Lines(求直线交点)

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

求交点见zhhx课件

#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cmath>
using namespace std;
const double eps=1e-8;
const int maxn=100004;
int dcmp(double x) {
    if(fabs(x) < eps) return 0;
    return x < 0 ? -1 : 1;
}
#define Point Vector
struct Vector{
    double x,y;
    Vector(double x=0,double y=0):x(x),y(y){};
    Vector operator + (const Vector &A) const{
        return Vector(x+A.x,y+A.y);
    }
    Vector operator - (const Vector &A) const{
        return Vector(x-A.x,y-A.y);
    }
    Vector operator * (const double &A) const{
        return Vector(x*A,y*A);
    }
    Vector operator / (const double &A) const{
        return Vector(x/A,y/A);
    }
    bool operator == (const Vector &A) const{
        return dcmp(x-A.x)==0&&dcmp(y-A.y)==0;
    }
};
struct Line{
    Point p1,p2;
    Line(){};
    Line(Point p1,Point p2):p1(p1),p2(p2){}
}li[maxn];
bool operator < (Line l1,Line l2){
    if(l1.p1.x==l2.p1.x) return l1.p1.y<l2.p1.y;
    else return l1.p1.x<l2.p1.x;
}
int ans[maxn],num[maxn],n,m;
double U,L,R,D;
double PolarAngle(Vector A){
    return atan2(A.y,A.x);
}
Vector rotate(Vector &A,double a){
    return A=Vector(A.x*cos(a)-A.y*sin(a),A.x*sin(a)+A.y*cos(a));
}
double Dot(Vector A,Vector B){
    return A.x*B.x+A.y*B.y;
}
double Cross(Vector A,Vector B){
    return A.x*B.y-A.y*B.x;
}
Point GLI(Point P,Vector v,Point Q,Vector w){
    Vector u=P-Q;
    double t=Cross(w,u)/Cross(v,w);
    return P+v*t;
}
int main(){
    scanf("%d",&n);
    printf("INTERSECTING LINES OUTPUT\n");
    for(int i=1;i<=n;i++){
        Point a,b,c,d;
        scanf("%lf%lf%lf%lf%lf%lf%lf%lf",&a.x,&a.y,&b.x,&b.y,&c.x,&c.y,&d.x,&d.y);
        Vector p1,p2;
        p1=b-a,p2=d-c;
        if(dcmp(Cross(p1,p2))==0){
            if(dcmp(Cross(c-a,p1))==0&&dcmp(Cross(d-a,p1))==0) printf("LINE\n");
            else printf("NONE\n");
            continue;
        }
        Point p=GLI(a,p1,c,p2);
        printf("POINT %.2lf %.2lf\n",p.x,p.y);
    }
    printf("END OF OUTPUT");
}

 

posted @ 2019-01-26 16:30  WiFiMonster  阅读(137)  评论(0编辑  收藏  举报