POJ 1269
计算几何基础,叉积应用,直线判相交及交点求解。
取两条直线的方向向量做叉积,结果为0则平行。平行时,求一直线与另一直线的一端点的叉积,结果为0则不是同一条线。不平行时,
x1=L[0].s.x, y1=L[0].s.y;
x2=L[1].s.x, y2=L[1].s.y;
x3=L[1].t.x, y3=L[1].t.y;
a1=L[0].s.x-L[0].t.x, b1=L[0].s.y-L[0].t.y;
设交点为(x1+k*a1,y1+k*b1),由点(x1,y1)在直线(x2,y2)-(x3,y3)上,
则有方程:
(x1+k*a1-x3)*(y2-y3)-(y1+k*b1-y3)*(x2-x3)=0,
化简得
k*(a1*(y2-y3)-b1*(x2-x3))+(x1-x3)*(y2-y3)-(y1-y3)*(x2-x3)=0,
k=-cross(1,2,3)/cross(A,B) (A=L[0].s-L[0].t, B=L[1].s-L[1].t)
#include<cmath>
#include<ctime>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<iostream>
#define LL long long
#define mm0(a) memset(a,0,sizeof(a));
#define debug puts("It's ok here!")
using namespace std;
const int maxn=5e3+5;
const double eps=1e-8;
struct Point{
LL x, y;
Point(){}
Point(LL _x, LL _y):x(_x), y(_y){}
Point operator - (const Point &rhs)const{
return Point(x-rhs.x,y-rhs.y);
}
Point operator + (const Point &rhs)const{
return Point(x+rhs.x,y+rhs.y);
}
};
typedef struct Point Vector;
struct line{
Point s, t;
line(){}
line(LL sx,LL sy,LL tx,LL ty){s.x=sx; s.y=sy; t.x=tx; t.y=ty;}
line(Point _s, Point _t):s(_s), t(_t){}
};
int dcmp(double x){
if(fabs(x)<eps) return 0;
return x<0?-1:1;
}
LL cross(Vector A,Vector B){
return A.x*B.y-A.y*B.x;
}
LL cross(Point a,Point b,Point c){
return (a.x-c.x)*(b.y-c.y)-(a.y-c.y)*(b.x-c.x);
}
bool crossleft(Point a,Point b,Point c){
return cross(a, b, c)>0;
}
Point P[maxn];
line L[maxn];
int n, m;
int main()
{
LL x1, y1, x2, y2;
int T=0;
// scanf("%d",&T);
while(~scanf("%d",&n)){
puts("INTERSECTING LINES OUTPUT");
for(int i=0;i<n;i++){
scanf("%lld%lld%lld%lld",&x1,&y1,&x2,&y2);
L[0]=line(Point(x1,y1),Point(x2,y2));
scanf("%lld%lld%lld%lld",&x1,&y1,&x2,&y2);
L[1]=line(Point(x1,y1),Point(x2,y2));
if(cross(L[0].s-L[0].t,L[1].s-L[1].t)==0){
if(cross(L[0].s,L[1].s,L[1].t)==0){
puts("LINE");
}else{
puts("NONE");
}
}else{
x1=L[0].s.x, y1=L[0].s.y;
x2=L[1].s.x, y2=L[1].s.y;
Vector A=L[0].s-L[0].t;
Vector B=L[1].s-L[1].t;
double k=-cross(L[0].s, L[1].s, L[1].t)*1.0/cross(A, B)*1.0;
double ansx=x1*1.0+k*A.x, ansy=y1*1.0+k*A.y;
printf("POINT %.2lf %.2lf\n",ansx, ansy);
}
}
puts("END OF OUTPUT");
}
return 0;
}