题意
给你两条直线
若其相交则输出 交点的坐标
若其平行则输出 NONE
若其重合则输出 LINE
分析
比较基础的计算几何,就是考向量的使用
判断两条直线是否平行,就是看其叉积是否为0(可以这样理解,两个向量的叉积的集合意义就是平行四边形的面积,当你两条直线平行的时候,面积自然为0)
进一步如何判断是否重合呢?只需要在判断平行的基础上再看两条直线中是否有一个端点位于另一条直线上即可
相交的时候输出其交点也很好办,利用好叉积的性质
计算出S1和S2,然后利用比值关系,对向量AB进行放缩,就可以得到交点P的坐标了
代码
#include<cstdio>
#include<cstring>
#include<iostream>
#include<cmath>
using namespace std;
const double eps=1e-7;
struct point{
double x,y;
point (double _x=0.0,double _y=0.0):x(_x),y(_y){}
friend inline point operator +(const point &a,const point &b){
return point(a.x+b.x,a.y+b.y);
}
friend inline point operator -(const point &a,const point &b){
return point(a.x-b.x,a.y-b.y);
}
friend inline point operator *(double k,const point &a){
return point(k*a.x,k*a.y);
}
friend inline double dot(const point &a,const point &b){
return (a.x*b.x+a.y*b.y);
}
friend inline double cross(const point &a,const point &b){
return (a.x*b.y-b.x*a.y);
}
friend inline double len(const point &a){
return sqrt(dot(a,a));
}
friend inline double dis (const point &a,const point &b){
return len(a-b);
}//向量常见的运算
}a[5];
int n,m;
bool on(const point &a,const point &b,const point &c){
int det=cross(a-c,b-c);
if(det!=0) return 0;
return 1;
}
int main(){
scanf("%d",&n);
printf("INTERSECTING LINES OUTPUT\n");
int i,j,k;
for(i=1;i<=n;++i){
for(j=1;j<=4;++j)
scanf("%lf%lf",&a[j].x,&a[j].y);
double det=cross(a[2]-a[1],a[4]-a[3]);
if(det==0){
if(on(a[3],a[4],a[1])) {printf("LINE\n");continue;}
printf("NONE\n");
}
else{
double S1=cross(a[3]-a[1],a[3]-a[4]),S2=cross(a[3]-a[4],a[3]-a[2]);
double k=S1/(S1+S2);
point c=k*(a[2]-a[1]);
printf("POINT %.2f %.2f\n",a[1].x+c.x,a[1].y+c.y);
}
}
printf("END OF OUTPUT");
return 0;
}