Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 650 Accepted Submission(s): 316
1 #include <iostream>
2 #include <iomanip>
3 #include <cmath>
4 using namespace std; 5 struct Point {double x,y;}; //点
6 struct Ldir{double dx,dy;}; //方向向量
7 struct Lline{Point p; Ldir dir;}; //直线 8 // 计算直线的一般式 Ax+By+C=0
9 void format(Lline ln,double& A,double& B,double& C) 10 { 11 A=ln.dir.dy; 12 B=-ln.dir.dx; 13 C=ln.p.y*ln.dir.dx-ln.p.x*ln.dir.dy; 14 } 15 // 求点p1关于直线ln的对称点p2
16 Point mirror(Point P,Lline ln) 17 { 18 Point Q; 19 double A,B,C; 20 format(ln,A,B,C); 21 Q.x=((B*B-A*A)*P.x-2*A*B*P.y-2*A*C)/(A*A+B*B); 22 Q.y=((A*A-B*B)*P.y-2*A*B*P.x-2*B*C)/(A*A+B*B); 23 return Q; 24 } 25 //求线段交点
26 struct TLine 27 { 28 //直线标准式中的系数
29 double a, b, c; 30 }; 31 TLine lineFromSegment(Point p1, Point p2) 32 { 33 TLine tmp; 34 tmp.a = p2.y - p1.y; 35 tmp.b = p1.x - p2.x; 36 tmp.c = p2.x * p1.y - p1.x * p2.y; 37 return tmp; 38 } 39 /*求直线的交点,注意平形的情况无解,避免RE*/
40 const double eps = 1e-6; //注意一定要加,否则错误
41 Point LineInter(TLine l1, TLine l2) 42 { 43 //求两直线得交点坐标
44 Point tmp; 45 double a1 = l1.a; 46 double b1 = l1.b; 47 double c1 = l1.c; 48 double a2 = l2.a; 49 double b2 = l2.b; 50 double c2 = l2.c; 51 //注意这里b1 = 0
52 if(fabs(b1) < eps){ 53 tmp.x = -c1 / a1; 54 tmp.y = (-c2 - a2 * tmp.x) / b2; 55 } 56 else{ 57 tmp.x = (c1 * b2 - b1 * c2) / (b1 * a2 - b2 * a1); 58 tmp.y = (-c1 - a1 * tmp.x) / b1; 59 } 60 return tmp; 61 } 62 int main() 63 { 64 int T; 65 cin>>T; 66 cout<<setiosflags(ios::fixed)<<setprecision(3); 67 while(T--){ 68 Point p1,p2,ps,pe; 69 Lline l; 70 cin>>p1.x>>p1.y; 71 cin>>p2.x>>p2.y; 72 cin>>ps.x>>ps.y; 73 cin>>pe.x>>pe.y; 74 l.p = p1; 75 l.dir.dx = p2.x - p1.x; 76 l.dir.dy = p2.y - p1.y; 77 Point duichen = mirror(ps,l); //求对称点 78 //cout<<duichen.x<<' '<<duichen.y<<endl;
79 TLine l1,l2; 80 l1 = lineFromSegment(p1,p2); 81 l2 = lineFromSegment(duichen,pe); 82 Point inter = LineInter(l1,l2); //求交点
83 cout<<inter.x<<' '<<inter.y<<endl; 84 } 85 return 0; 86 }
Freecode : www.cnblogs.com/yym2013