ACM/ICPC 之 平面几何-两直线关系(POJ 1269)

时间:2023-03-08 22:13:59

  题意:给定四点的坐标(x,y),分别确定两直线,求出其交点,若重合or平行则输出相应信息

  • 用四个点的坐标算出直线通式(ax+by+c=0)中的a,b,c,然后利用a,b,c计算出交点坐标(其他公式不够通用= =,比如斜率限制)
  • 我利用两次平行判定重合
  • 公式利用 初高中数学知识代数知识 在草纸上仔细推导出来= =,让a,b,c为整数,比如可以演算得到a = y2-y1,b = x1-x2这一类公式。

  详细Code如下

 //给定四点,分别确定两直线,求出其交点,若重合or平行则输出相应信息
//Memory 206 K,Time: 0 Ms
#include<iostream>
#include<cstring>
#include<cstdio>
using namespace std; struct Point{
int x,y;
}; struct Line{
int a,b,c;
}; /*Judge s1-s2和s3-s4两条线平行*/
int parallel(Point s1,Point s2,Point s3,Point s4)
{
if((s1.y-s2.y)*(s3.x-s4.x) == (s1.x-s2.x)*(s3.y-s4.y))
return ;
else
return ;
} /*构造直线*/
Line lineform(int x1,int y1,int x2,int y2)
{
Line temp;
temp.a = y2 - y1;
temp.b = x1 - x2;
temp.c = -(temp.a*x1+temp.b*y1);
return temp;
} int main()
{
int T;
Point p[];
Line l1,l2;
int i; cin>>T;
cout<<"INTERSECTING LINES OUTPUT"<<endl;
while(T--)
{
for(i=;i<;i++)
scanf("%d%d",&p[i].x,&p[i].y); if(parallel(p[],p[],p[],p[])) //平行
{
if(parallel(p[],p[],p[],p[])) //重合
cout<<"LINE"<<endl;
else
cout<<"NONE"<<endl;
}
else
{
l1 = lineform(p[].x,p[].y,p[].x,p[].y);
l2 = lineform(p[].x,p[].y,p[].x,p[].y); /* 求交点 */
double x,y,d;
d = l2.a*l1.b-l1.a*l2.b;
x = -(l1.b*l2.c - l2.b*l1.c)/d;
y = (l1.a*l2.c - l2.a*l1.c)/d;
printf("POINT %.2lf %.2lf\n",x,y);
}
}
cout<<"END OF OUTPUT"<<endl; return ;
}