标签:style blog http color os io for ar
poj 1269 (直线交点):
这道题是给两条直线(输入直线上的两个点),然后问你两条直线是相交、重合还是平行。
很简单一道题。不过用g++测试的话double要用%f输出。不知道为什么。
(求交点的模板感觉很科学)
1 #include <cstdio> 2 #include <iostream> 3 #define FOR(i,l,r) for(int i=(l);i<=(r);i++) 4 #define FE(it,v) for(__typeof((v).begin()) it=(v).begin();it!=(v).end();it++) 5 #define rep(i,n) for(int i=0;i<(n);i++) 6 #define zero(x) ((x>=0?x:-x)<1e-10) 7 #define debug(x) cout<<#x<<" = "<<x<<endl 8 using namespace std; 9 10 struct pot{ 11 double x,y; 12 pot() { x=0; y=0; } 13 pot(double _x, double _y):x(_x),y(_y){}; 14 }; 15 double cross(pot a,pot b) { return a.x*b.y-a.y*b.x; } 16 pot to_v(pot a,pot b) { pot c=pot(b.x-a.x,b.y-a.y); return c; } 17 pot intersection_line(pot u1,pot u2,pot v1,pot v2) 18 { 19 pot ret = u1; 20 double t = ((u1.x-v1.x)*(v1.y-v2.y) - (u1.y-v1.y)*(v1.x-v2.x)) 21 / ((u1.x-u2.x)*(v1.y-v2.y) - (u1.y-u2.y)*(v1.x-v2.x)); 22 ret.x += (u2.x-u1.x) * t; 23 ret.y += (u2.y-u1.y) * t; 24 return ret; 25 } 26 27 pot a[5]; 28 int main() 29 { 30 puts("INTERSECTING LINES OUTPUT"); 31 int n; scanf("%d",&n); 32 for (pot ans;n--;) 33 { 34 FOR (i,1,4) scanf("%lf%lf",&a[i].x,&a[i].y); 35 if (zero(cross(to_v(a[1],a[2]),to_v(a[3],a[4])))) 36 if (zero(cross(to_v(a[1],a[2]),to_v(a[1],a[3])))) puts("LINE"); 37 else puts("NONE"); 38 else ans=intersection_line(a[1],a[2],a[3],a[4]),printf("POINT %.2f %.2f\n",ans.x,ans.y); 39 } 40 puts("END OF OUTPUT"); 41 return 0; 42 } 43 /*pot intersection_line(pot u1,pot u2,pot v1,pot v2) 44 { 45 pot ret; 46 double a1=u1.y-u2.y,b1=u2.x-u1.x,c1=u1.y*(u1.x-u2.x)-u1.x*(u1.y-u2.y); 47 double a2=v1.y-v2.y,b2=v2.x-v1.x,c2=v1.y*(v1.x-v2.x)-v1.x*(v1.y-v2.y); 48 ret.y=(a1*c2-a2*c1)/(a2*b1-a1*b2); 49 ret.x=(b1*c2-b2*c1)/(a1*b2-a2*b1); 50 return ret; 51 }*/
标签:style blog http color os io for ar
原文地址:http://www.cnblogs.com/monmonde/p/3920946.html