当前位置: 代码迷 >> 综合 >> poj 1254 Hansel and Grethel 直线求交
  详细解决方案

poj 1254 Hansel and Grethel 直线求交

热度:64   发布时间:2024-01-19 05:32:00.0

题意:

通过点和方向给出两条直线,求他们的交点。

分析:

裸的直线求交,向量做法可避免特殊情况的的讨论。

代码:

//poj 1254
//sep9
#include <iostream>
#include <cmath>
using namespace std;
const double pi=acos(-1.0);struct P
{double x,y;
}a,b,c,d;double det(double x1,double y1,double x2,double y2)
{return x1*y2-x2*y1;	
}int main()
{int cases;scanf("%d",&cases);while(cases--){double alpha;scanf("%lf%lf%lf",&a.x,&a.y,&alpha);alpha=pi*(450-alpha)/180;b.x=a.x+cos(alpha);b.y=a.y+sin(alpha);scanf("%lf%lf%lf",&c.x,&c.y,&alpha);alpha=pi*(450-alpha)/180;d.x=c.x+cos(alpha);d.y=c.y+sin(alpha);double s1=det(c.x-a.x,c.y-a.y,b.x-a.x,b.y-a.y);double s2=det(d.x-a.x,d.y-a.y,b.x-a.x,b.y-a.y);double xx=(s1*d.x-s2*c.x)/(s1-s2);double yy=(s1*d.y-s2*c.y)/(s1-s2);printf("%.4lf %.4lf\n",xx+1e-9,yy+1e-9);}return 0;	
}