nyoj-1132-promise me a medal(求线段交点)

题目链接

 1 /*
 2     Name:nyoj-1132-promise me a medal
 3     Copyright:
 4     Author:
 5     Date: 2018/4/26 20:26:22
 6     Description:
 7     向量之间的运算,不熟悉就绕蒙逼了
 8     用 ACM国际大学生程序设计竞赛 算法与实现的模板
 9     有个坑:    如果第二条线段的起点是第一条线段的终点,那么不需要计算  
10      
11     1
12     1 2 1 3 1 3 1 4
13     输出
14     yes 1.0 3.0 
15     
16 */
17 #include <iostream>
18 #include <cstdio>
19 #include <cmath>
20 #include <algorithm>
21 using namespace std;
22 const double pi = acos(-1.0);
23 inline double sqr(double x) {
24     return x * x;
25 }
26 const double eps = 1e-8;
27 int cmp(double x) {
28     if (fabs(x) < eps) return 0;
29     if (x >0 )return 1;
30     return -1;
31 }
32 //point
33 struct point {
34     double x, y;
35     point (){}
36     point (double a, double b):x(a), y(b) {    }
37     void input() {
38         scanf("%lf %lf", &x, &y);
39     }
40     friend point operator - (const point &a, const point &b) {
41         return point(a.x-b.x, a.y-b.y);
42     }
43     friend point operator * (const double &a, const point &b) {
44         return point (a * b.x, a*b.y);
45     }
46     friend point operator / (const point &a, const double &b) {
47         return point (a.x / b, a.y /b);
48     }
49     friend bool operator == (const point &a, const point &b) {
50         return (cmp(a.x - b.x) == 0 && cmp(a.y - b.y) == 0);
51     }
52 };
53 double det(const point &a, const point &b) {
54     return a.x * b.y - a.y * b.x;
55 }
56 //line
57 struct line {
58     point a, b;
59     line() {    }
60     line(point x, point y):a(x), b(y){    }
61 };
62 line point_make_line(const point a, const point b) {
63     return line(a, b);
64 }
65 bool parallel(line a, line b)  {
66     return !cmp(det(a.a - a.b, b.a - b.b));
67 }
68 bool line_make_point(line a, line b, point &res) {
69     if(parallel(a,b)) return false;
70     double s1 = det(a.a-b.a, b.b - b.a);
71     double s2 = det(a.b-b.a, b.b - b.a);
72     res = (s1 * a.b - s2 * a.a)/(s1-s2);
73     return true;
74 }
75 int main()
76 {
77     int t;
78     cin>>t;
79     while (t--) {
80         point a, b ,c ,d;
81         cin>>a.x>>a.y>>b.x>>b.y>>c.x>>c.y>>d.x>>d.y;
82         line ab(a, b), cd(c, d);
83         if (b == c) {//如果第二条线段的起点是第一条线段的终点,那么不需要计算 
84             cout<<"yes ";
85             printf("%.1f %.1f\n",b.x, b.y) ;
86             continue;
87         }
88         if (parallel(ab, cd)) cout<<"no\n";
89         else {
90             cout<<"yes ";
91             point ans;
92             line_make_point(ab, cd, ans);
93             printf("%.1f %.1f\n",ans.x, ans.y) ;
94         }
95     }
96     return 0;
97 }

 

posted @ 2018-04-26 21:14  朤尧  阅读(318)  评论(0编辑  收藏  举报