正规的做法是找切点。三角形三个顶点分别对圆作切线,然后求切点(2个)。两圆之间也要求切点(4个)。
扯淡了这就。。麻烦的要命。。
下面是写了一半的代码。。
void process_circle(point p, point o, double r, point &intersect1, point &intersect2)
{ point vec; vec = o - p; double angle = asin(r*1.0 / sqrt(dist(p, o))); double scale = sqrt(1 - r*r / dist(p, o)); intersect1 = Rotate(vec, -angle, scale) + p; intersect2 = Rotate(vec, angle, scale) + p; return; } void process_two_circles(point o1, point o2, double r1, double r2, point &intersect1, point &intersect2, point &intersect3, point &intersect4) { point vec = o2 - o1; double angle = asin(fabs(r2 - r1) / sqrt(dist(o2, o1))); intersect1 = Rotate(o1, angle + (pi / 2), sqrt(dist(o2, o1)) / r1); intersect2 = Rotate(o1, -angle - (pi / 2), sqrt(dist(o2, o1)) / r1); point vec2 = o1 - o2; } |
当然了,可以水过它,把圆离散化成点,构成1000边形,然后注意,周长不要直接两点的距离,最好还是采用它们之间围成的那段弧的长度来计算。不难得到如下代码,但是我实在找不到错误在哪里了。。
#include <iostream>
#include <math.h> #include <stdlib.h> #include <iomanip> using namespace std; #define pi acos(-1.0) #define eps 1e-8 #define zero(x) (((x)>0?(x):-(x))<eps) #define number_of_devision 600 struct point { double x, y; bool flag; double r; }p[number_of_devision * 50 + 155], convex[number_of_devision * 50 + 155]; double xmult(point p1, point p2, point p0){ return (p1.x - p0.x)*(p2.y - p0.y) - (p2.x - p0.x)*(p1.y - p0.y); } point p1, p2; int graham_cp(const void* a, const void* b){ double ret = xmult(*((point*) a), *((point*) b), p1); return zero(ret) ? (xmult(*((point*) a), *((point*) b), p2) > 0 ? 1 : -1) : (ret > 0 ? 1 : -1); } void _graham(int n, point* p, int& s, point* ch){ int i, k = 0; for (p1 = p2 = p[0], i = 1; i<n; p2.x += p[i].x, p2.y += p[i].y, i++) if (p1.y - p[i].y>eps || (zero(p1.y - p[i].y) && p1.x > p[i].x)) p1 = p[k = i]; p2.x /= n, p2.y /= n; p[k] = p[0], p[0] = p1; qsort(p + 1, n - 1, sizeof(point), graham_cp); for (ch[0] = p[0], ch[1] = p[1], ch[2] = p[2], s = i = 3; i < n; ch[s++] = p[i++]) for (; s>2 && xmult(ch[s - 2], p[i], ch[s - 1]) < -eps; s--); } int wipesame_cp(const void *a, const void *b) { if ((*(point *) a).y < (*(point *) b).y - eps) return -1; else if ((*(point *) a).y > (*(point *) b).y + eps) return 1; else if ((*(point *) a).x < (*(point *) b).x - eps) return -1; else if ((*(point *) a).x > (*(point *) b).x + eps) return 1; else return 0; } int _wipesame(point * p, int n) { int i, k; qsort(p, n, sizeof(point), wipesame_cp); for (k = i = 1; i < n; i++) if (wipesame_cp(p + i, p + i - 1) != 0) p[k++] = p[i]; return k; } int graham(int n, point* p, point* convex, int maxsize = 1, int dir = 1){ point* temp = new point[n]; int s, i; n = _wipesame(p, n); _graham(n, p, s, temp); for (convex[0] = temp[0], n = 1, i = (dir ? 1 : (s - 1)); dir ? (i < s) : i; i += (dir ? 1 : -1)) if (maxsize || !zero(xmult(temp[i - 1], temp[i], temp[(i + 1)%s]))) convex[n++] = temp[i]; delete []temp; return n; } double dist(point p1, point p2) { return sqrt((p1.x - p2.x)*(p1.x - p2.x) + (p1.y - p2.y)*(p1.y - p2.y)); } int main() { int n, m; while (cin >> n >> m) { if (n == 1 && m == 0) { double x, y, r; cin >> x >> y >> r; cout << 2 * pi * r << endl; continue; } int counts = 0; for (int i = 0; i < n; i++) { double x, y, r; cin >> x >> y >> r; double step = 2 * pi / (number_of_devision); for (double angle = 0; angle <= pi * 2; angle += step) { p[counts].x = x + r * cos(angle); p[counts].y = y + r * sin(angle); p[counts].r = r; p[counts++].flag = true; } /*for (int j = 0; j < counts; j++) { cout << p[j].x << ' ' << p[j].y << endl; }*/ } for (int i = 0; i < m; i++) { double x1, y1, x2, y2, x3, y3; cin >> x1 >> y1 >> x2 >> y2 >> x3 >> y3; p[counts].x = x1, p[counts].y = y1, p[counts].r = 0, p[counts].flag = false; p[++counts].x = x2, p[counts].y = y2, p[counts].r = 0, p[counts].flag = false; p[++counts].x = x3, p[counts].y = y3, p[counts].r = 0, p[counts].flag = false; } /*for (int j = 0; j <= counts; j++) { cout << p[j].x << ' ' << p[j].y << endl; } cout << counts << endl;*/ int size_of_convex = graham(counts+1, p, convex, 0); /*for (int j = 0; j < size_of_convex; j++) { cout << j << ' ' << convex[j].x << ' ' << convex[j].y << endl; } cout << size_of_convex << endl;*/ double dis = 0; for (int i = 1; i < size_of_convex; i++) { if (convex[i].flag && convex[i-1].flag) { dis += (2 * pi * convex[i].r) / number_of_devision; //cout << "dis between " << i - 1 << " and " << i << " is " << dis << endl; //cout << dis << endl; continue; } dis += dist(convex[i], convex[i - 1]); //cout << "dis between" << i - 1 << " and " << i << " is " << dis <<endl; //cout << dis << endl; } dis += dist(convex[0], convex[size_of_convex - 1]); cout << fixed << setprecision(10) << dis << endl; } } |