自己请教了队长和军哥,终于明白了 模版的思想了。
模版还需要自己再写一遍的。
还有要注意的是 对于g++ 编译器来讲 输出要用%f 而对c++编译器来讲,要用 %lf 这是要注意的。
#include<stdio.h>
#include<cmath>
#include<iostream>
#include<stdlib.h>
using namespace std;
struct point{
double x,y;
point(double a,double b):x(a),y(b){} //如果声明了这样的函数 就要重写下面的无参的构造函数
point(){}; // 无参的构造函数
friend point operator - (const point &a,const point &b){ //点的减法
return point(a.x-b.x,a.y-b.y);
}
friend point operator * (const double &a,const point &b){ //数乘以点
return point(a*b.x,a*b.y);
}
friend point operator / (const point &a ,const double b){ //点除以数
return point(a.x/b,a.y/b);
}
};
struct line{
point a,b;
} ;
const double eps=1e-8;
int cmp(double x){ //判断是否趋近于0
if(fabs(x)<eps)return 0;
if(x>eps)return 1;
else return -1;
}
double det(point a,point b){ //叉乘 向量积
return a.x*b.y-a.y*b.x;
}
bool parallel(point a,point b){ //判断平行的函数
if( cmp(det(a,b))==0 ) return true;
else return false;
}
point Line_make_point(line one,line two){ //判断相交的函数
double s1=det(one.a-two.a , two.b-two.a);
double s2=det(one.b-two.a , two.b-two.a);
return (s1*one.b-s2*one.a)/(s1-s2);
}
int main()
{
line one,two;
int n;
scanf("%d",&n);
printf("INTERSECTING LINES OUTPUT\n");
while(n--){
scanf("%lf%lf%lf%lf%lf%lf%lf%lf",&one.a.x,&one.a.y,&one.b.x,&one.b.y,&two.a.x,&two.a.y,&two.b.x,&two.b.y);
if ( (parallel(one.a-one.b,two.a-two.b)==true) && ((parallel(one.a-two.b,two.a-one.b)==true)) ) {
printf("LINE\n");
continue;
}
if ( parallel(one.a-one.b,two.a-two.b)==true ){
printf("NONE\n");
continue;
}
point ans=Line_make_point(one,two);
printf("POINT %.2f %.2f\n",cmp(ans.x)==0?0:ans.x , cmp(ans.y)==0?0:ans.y );
}
printf("END OF OUTPUT\n");
//system("pause");
return 0;
}

本文介绍了一个用于二维空间中直线相交检测的C++模板,并提供了详细的代码实现。通过对点、线的定义和操作,实现了判断两条直线是否相交及求交点的功能。

被折叠的 条评论
为什么被折叠?



