1. 程式人生 > >計算幾何基礎 POJ 1269 Intersecting Lines 【直線相交判斷,求交點】

計算幾何基礎 POJ 1269 Intersecting Lines 【直線相交判斷,求交點】

#include <iostream>
#include <algorithm>
#include <cstdio>
#include <cmath>
using namespace std;
const int maxn=1e5+50;
const double eps=1e-8;
typedef long long ll;
struct point
{
    double x,y;
    point(double _X=0,double _Y=0)
    {
        x=_X;y=_Y;
    }
    point operator
- (const point &oth)const { return point(x-oth.x,y-oth.y); } point operator + (const point &oth)const { return point(x+oth.x,y+oth.y); } point operator * (const double oth)const { return point(x*oth,y*oth); } point operator
/ (const double oth)const { return point(x/oth,y/oth); } }; int dcmp(double x) { if(fabs(x)<eps)return 0; else return x<0?-1:1; } double cross(point a,point b) { return a.x*b.y-b.x*a.y; } point glt(point a,point a1,point b,point b1) { point v= a1-a; point w=b1-b,u = a-b; double
t = cross(w,u)/cross(v,w); return a+v*t; } int main() { //freopen("d://duipai//data.txt","r",stdin); //freopen("d://duipai//out1.txt","w",stdout); int T; scanf("%d",&T); printf("INTERSECTING LINES OUTPUT\n"); while(T--) { double x1,y1,x2,y2,x3,y3,x4,y4; scanf("%lf%lf%lf%lf",&x1,&y1,&x2,&y2); scanf("%lf%lf%lf%lf",&x3,&y3,&x4,&y4); point a,b,c,d; a=point(x1,y1); b=point(x2,y2); c=point(x3,y3); d=point(x4,y4); if(cross(b-a,d-c)) { point t=glt(a,b,c,d); printf("POINT %.2lf %.2lf\n",t.x,t.y); } else { if(!cross(b-a,c-b)) puts("LINE"); else puts("NONE"); } } printf("END OF OUTPUT\n"); return 0; }