[poj1269]Intersecting Lines

时间:2024-12-02 19:34:39

题目大意:求两条直线的交点坐标。

解题关键:叉积的运用。

证明:

直线的一般方程为$F(x) = ax + by + c = 0$。既然我们已经知道直线的两个点,假设为$(x_0,y_0), (x_1, y_1)$,那么可以得到$a = {y_0} - {y_1}$,$b = x_1 – x_0$,$c = x_0y_1 – x_1y_0$。

因此我们可以将两条直线分别表示为

${F_0}(x) = {\rm{ }}{a_0}x{\rm{ }} + {\rm{ }}{b_0}y{\rm{ }} + {c_0} = 0,{F_1}(x) = {a_1}x + {b_1}y + {c_1} = 0$

那么两条直线的交点应该满足

${a_0}x + {b_0}y + {c_0} = {\rm{ }}{a_1}x + {b_1}y + {c_1}$

由此可推出

$\begin{array}{*{20}{l}}
{x = ({b_0}{c_1} - {b_1}{c_0})/D}\\
{y = ({a_1}{c_0} - {a_0}{c_1})/D}
\end{array}$

$D = {a_0}{b_1} - {a_1}{b_0}$ (D为0时,表示两直线平行)

#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cstdlib>
#include<cmath>
#include<iostream>
#define pi acos(-1)
using namespace std;
typedef long long ll;
const double eps=1e-;
const int N=,maxn=,inf=0x3f3f3f3f;
struct point{
double x,y;
};
struct line{
point a,b;
}l[N]; int main(){
int t;
double x1,x2,x3,x4,y1,y2,y3,y4;
cin>>t;
cout<<"INTERSECTING LINES OUTPUT"<<endl;
while(t--){
cin>>x1>>y1>>x2>>y2>>x3>>y3>>x4>>y4;
if((x4-x3)*(y2-y1)==(y4-y3)*(x2-x1)){
if((x2-x1)*(y3-y1)==(y2-y1)*(x3-x1)) cout<<"LINE"<<endl;//用叉积判断共线
else cout<<"NONE"<<endl;
}
else{
double a1=y1-y2,b1=x2-x1,c1=x1*y2-x2*y1;//c是叉积
double a2=y3-y4,b2=x4-x3,c2=x3*y4-x4*y3;
double x=(c2*b1-c1*b2)/(b2*a1-b1*a2);
double y=(a2*c1-a1*c2)/(b2*a1-b1*a2);
printf("POINT %.2f %.2f\n",x,y);
}
}
cout<<"END OF OUTPUT"<<endl;
return ;
}

kuangbin模板。求直线的交点。(由于poj waiting,未测试)

#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cstdlib>
#include<cmath>
#include<iostream>
using namespace std;
typedef long long ll;
const double eps = 1e-;
int sgn(double x){
if(fabs(x)<eps)return ;
if(x<)return -;
else return ;
}
struct Point{
double x,y;
Point(){}
Point(double _x,double _y){x=_x;y=_y;}
Point operator-(const Point &b)const{return Point(x-b.x,y-b.y);}
double operator^(const Point &b)const{return x*b.y-y*b.x;}
double operator*(const Point &b)const{return x*b.x+y*b.y;}
bool operator==(const Point &b)const{return x==b.x&&y==b.y;}
};
struct Line{
Point s,e;
Line(){}
Line(Point _s,Point _e){s=_s;e=_e;}
//两直线相交求交点
pair<int,Point>operator&(const Line &b)const{
Point res=s;
if(sgn((s-e)^(b.s-b.e))==){
if(sgn((s-b.e)^(b.s-b.e))==)
return make_pair(,res);//重合
else return make_pair(,res);//平行
}
double t=((s-b.s)^(b.s-b.e))/((s-e)^(b.s-b.e));
res.x+=(e.x-s.x)*t;
res.y+=(e.y-s.y)*t;
return make_pair(,res);
}
}; //*判断线段相交
bool inter(Line l1,Line l2){
return
max(l1.s.x,l1.e.x) >= min(l2.s.x,l2.e.x) &&
max(l2.s.x,l2.e.x) >= min(l1.s.x,l1.e.x) &&
max(l1.s.y,l1.e.y) >= min(l2.s.y,l2.e.y) &&
max(l2.s.y,l2.e.y) >= min(l1.s.y,l1.e.y) &&
sgn((l2.s-l1.e)^(l1.s-l1.e))*sgn((l2.e-l1.e)^(l1.s-l1.e))<= &&
sgn((l1.s-l2.e)^(l2.s-l2.e))*sgn((l1.e-l2.e)^(l2.s-l2.e))<=;
}
int main(){
int n;
printf("INTERSECTING LINES OUTPUT\n");
cin>>n;
while(n--){
int a1,b1,c1,d1,a2,b2,c2,d2;
cin>>a1>>b1>>c1>>d1>>a2>>b2>>c2>>d2;
Line a=Line(Point(a1,b1),Point(c1,d1)),b=Line(Point(a2,b2),Point(c2,d2));
pair<int,Point>pp=a&b;
if(pp.first==){
printf("LINE\n");
}else if(pp.first==){
printf("NONE\n");
}else{
Point tmp=pp.second;
printf("POINT %.2lf %.2lf\n",tmp.x,tmp.y);
}
}
printf("END OF OUTPUT\n");
}