潸然泪下的小飞飞

导航

三角形外接圆+圆的参数方程

  1 //pku_1266_三角形外接圆+圆的参数方程.cpp
  2 #include <stdio.h>
  3 #include <math.h>
  4 
  5 const double eps = 1e-6;
  6 const double pi = acos(-1);
  7 const double inf = 9999999.0;
  8 
  9 typedef struct TPoint
 10 {
 11     double x, y;
 12 }TPoint;
 13 
 14 typedef struct TCircle
 15 {
 16     TPoint c;
 17     double r;
 18 }TCircle;
 19 
 20 typedef struct TTriangle 
 21 {
 22     TPoint t[3];
 23 }TTriangle;
 24 
 25 double max(double x, double y)
 26 {
 27     if(x > y) return x;
 28     else return y; 
 29 }
 30 
 31 double min(double x, double y)
 32 {
 33     if(x < y) return x;
 34     else return y; 
 35 }
 36 
 37 double distance(TPoint p1, TPoint p2)
 38 {
 39    return sqrt((p1.x - p2.x) * (p1.x - p2.x) 
 40            + (p1.y - p2.y) * (p1.y - p2.y));    
 41 }
 42 
 43 double triangleArea(TTriangle t)
 44 {
 45     //已知三角形三个顶点的坐标,求三角形的面积 
 46     return fabs(t.t[0].x * t.t[1].y + t.t[1].x * t.t[2].y 
 47               + t.t[2].x * t.t[0].y - t.t[1].x * t.t[0].y
 48               - t.t[2].x * t.t[1].y - t.t[0].x * t.t[2].y) / 2;   
 49 }
 50 
 51 TCircle circumcircleOfTriangle(TTriangle t)
 52 {
 53     //三角形的外接圆
 54     TCircle tmp;
 55     double a, b, c, c1, c2;
 56     double xA, yA, xB, yB, xC, yC;
 57     a = distance(t.t[0], t.t[1]);
 58     b = distance(t.t[1], t.t[2]);
 59     c = distance(t.t[2], t.t[0]);
 60     //根据S = a * b * c / R / 4;求半径R 
 61     tmp.r = a * b * c / triangleArea(t) / 4;
 62     
 63     xA = t.t[0].x; yA = t.t[0].y;
 64     xB = t.t[1].x; yB = t.t[1].y;
 65     xC = t.t[2].x; yC = t.t[2].y;
 66     c1 = (xA * xA + yA * yA - xB * xB - yB * yB) / 2;
 67     c2 = (xA * xA + yA * yA - xC * xC - yC * yC) / 2;
 68     
 69     tmp.c.x = (c1 * (yA - yC) - c2 * (yA - yB)) / 
 70          ((xA - xB) * (yA - yC) - (xA - xC) * (yA - yB)); 
 71     tmp.c.y = (c1 * (xA - xC) - c2 * (xA - xB)) / 
 72          ((yA - yB) * (xA - xC) - (yA - yC) * (xA - xB)); 
 73          
 74     return tmp;     
 75 }
 76 
 77 double Get_angle(TPoint p, TCircle c)
 78 {
 79     double ans;
 80     ans = acos((p.x - c.c.x) / c.r);
 81     if(p.y < c.c.y){
 82         if(ans >= M_PI / 2){
 83             ans = (M_PI - ans) + M_PI;
 84         }
 85         else {
 86             ans = 2 * M_PI - ans;
 87         } 
 88     }
 89     return ans;
 90 }
 91 
 92 double swap(double &a, double &b)
 93 {
 94     double c;
 95     c = a;
 96     a = b;
 97     b = c;
 98 }
 99 
100 int UP(double a)
101 {
102     int a0;
103     if(a >= 0){
104         a0 = int(a);
105         if(fabs(a0 - a) < eps) return a0;
106         else return a0 + 1;
107     }
108     else {
109         a0 = (int)a; 
110         if(fabs(a0 - 1 - a) < eps) return a0 - 1;
111         return a0;   
112     }
113 }
114 
115 int DOWN(double a)
116 {
117     int a0;
118     if(a >= 0){
119         a0 = (int)a;
120         if(fabs(a - (a0 + 1)) < eps) return a0 + 1;
121         return a0;
122     }
123     else {
124         a0 = (int)a;
125         if(fabs(a0 - a) < eps) return a0;
126         else return a0 - 1;
127     }  
128 }
129 
130 int main()
131 {
132     TCircle c;
133     TTriangle t;
134     TPoint p1, p2, p3;
135     double a1, a2, a3, tmp;
136     int max_x, min_x, max_y, min_y;
137         scanf("%lf%lf", &p1.x, &p1.y);
138         scanf("%lf%lf%lf%lf", &p2.x, &p2.y, &p3.x, &p3.y);
139         t.t[0] = p1;
140         t.t[1] = p2;
141         t.t[2] = p3;
142         c = circumcircleOfTriangle(t);
143         a1 = Get_angle(p1, c);
144         a2 = Get_angle(p2, c);
145         a3 = Get_angle(p3, c);
146         if(a1 > a2) swap(a1, a2);
147         //讨论
148         if(a3 >= a1 && a3 <= a2){
149             tmp = 0;
150             if(tmp >= a1 && tmp <= a2){
151                 max_x = UP(c.c.x + c.r);
152             }
153             else max_x = UP(max(p1.x, p2.x));
154             tmp = pi / 2;
155             if(tmp >= a1 && tmp <= a2){
156                 max_y = UP(c.c.y + c.r);
157             }
158             else max_y = UP(max(p1.y, p2.y));
159             tmp = pi;
160             if(tmp >= a1 && tmp <= a2){
161                 min_x = DOWN(c.c.x - c.r);
162             }
163             else min_x = DOWN(min(p1.x, p2.x));
164             tmp = pi * 3 / 2;
165             if(tmp >= a1 && tmp <= a2){
166                 min_y = DOWN(c.c.y - c.r);
167             }
168             else min_y = DOWN(min(p1.y, p2.y)); 
169         }
170         else {
171             tmp = 0;
172             if(tmp <= a1 || tmp >= a2){
173                 max_x = UP(c.c.x + c.r);
174             }
175             else max_x = UP(max(p1.x, p2.x));
176             tmp = pi / 2;
177             if(tmp <= a1 || tmp >= a2){
178                 max_y = UP(c.c.y + c.r);
179             }
180             else max_y = UP(max(p1.y, p2.y));
181             tmp = pi;
182             if(tmp <= a1 || tmp >= a2){
183                 min_x = DOWN(c.c.x - c.r);
184             }
185             else min_x = DOWN(min(p1.x, p2.x));
186             tmp = pi * 3 / 2;
187             if(tmp <= a1 || tmp >= a2){
188                 min_y = DOWN(c.c.y - c.r);
189             }
190             else min_y = DOWN(min(p1.y, p2.y)); 
191         }
192         printf("%d\n", (max_x - min_x) * (max_y - min_y));
193     return 0;
194 }

 

posted on 2015-03-05 16:22  潸然泪  阅读(533)  评论(0编辑  收藏  举报