POJ原题
ZOJ原题
多组数据。每次给出四个点,前两个点确定一条直线,后两个点确定一条直线,若平行则输出"NONE",重合输出"LINE",相交输出“POINT”+交点坐标(保留两位小数)
先判重合:两条线重合意味着四点共线,即ABC共线且ABD共线(共线即为叉积=0)
再判平行:正常的数学方法,\(\overrightarrow{AB}\) // \(\overrightarrow{CD}\)
求交点:
//这个公式很好用,背下来好伐
#include<cstdio>
#include<algorithm>
#define eps 1e-8
using namespace std;
int n;
struct hhh
{
double x,y;
hhh() {}
hhh(double _x,double _y) { x=_x; y=_y; }
hhh operator - (const hhh &b) const
{
return hhh(x-b.x,y-b.y);
}
double operator * (const hhh &b) const
{
return x*b.y-b.x*y;
}
}p[2],q[2];
double abs(double x) { return x>0?x:-x; }
bool check(hhh a,hhh b,hhh c)
{
if (abs((a-b)*(c-b))<eps) return 1;
return 0;
}
int main()
{
puts("INTERSECTING LINES OUTPUT");
scanf("%d",&n);
for (int i=1;i<=n;i++)
{
for (int j=1;j<=4;j++)
scanf("%lf%lf",&q[j].x,&q[j].y);
if (check(q[1],q[2],q[3]) && check(q[1],q[2],q[4]))
{
puts("LINE");
continue;
}
if (abs((q[1].x-q[2].x)*(q[3].y-q[4].y)-(q[1].y-q[2].y)*(q[3].x-q[4].x))<eps)
{
puts("NONE");
continue;
}
double s1=(q[3]-q[1])*(q[4]-q[1]),s2=(q[4]-q[2])*(q[3]-q[2]);
printf("POINT ");
hhh tmp=(q[2]-q[1]);
printf("%.2f %.2f\n",(q[1].x*(s1+s2)+tmp.x*s1)/(s1+s2),(q[1].y*(s1+s2)+tmp.y*s1)/(s1+s2));
}
puts("END OF OUTPUT");
return 0;
}