Computational Geometry-Circular Template Training Guide 267

Source: Internet
Author: User
Tags acos cos cmath

#include #include #include #include #include #include #include #include #include #include #define MM (a) memset (A,0,sizeof (a)) typedef long Long ll;typedef unsigned long long ull;const double EPS = 1e-10;const int inf = 0x3f3f3f3f;using namespace std;struct point {double x, y; Point () {} (double x, double y) {this->x = x; this->y = y;} void Read () {scanf ("%lf%lf", &x, &y);} };typedef Point Vector; Vector operator + (vector A, vector B) {return vector (a.x + b.x, A.Y + b.y);} Vector operator-(vector A, vector B) {return vector (a.x-b.x, a.y-b.y);} Vector operator * (vector A, double p) {return vector (a.x * p, A.Y * p);} Vector operator/(vector A, double p) {return vector (a.x/p, a.y/p);} BOOL operator < (const point& A, const point& b) {return a.x < b.x | | (a.x = = b.x && a.y < B.Y);} Const double PI = ACOs ( -1.0), int dcmp (double x) {if (Fabs (x) < EPS) return 0; else return x < 0? -1:1;} BOOL operator = = (const point& A, const point& b) {return dcmp (a.x-b.x) = = 0 && dcmp (a.y-b.y) = = 0;} DoubleDot (vector A, vector B) {return a.x * b.x + a.y * B.Y;}/dot product double Length (vector a) {return sqrt (Dot (A, a));}//modulus double of vector Angle (vector A, vector b) {return ACOs (Dot (A, b)/Length (a)/length (b))}//vector angle Double cross (vector a, vector b) {retur N a.x * b.y-a.y * b.x;} Fork Product Double Area2 (Point A, point B, point C) {return cross (b-a, c-a);}//has a direction area double angle (Vector v) {return atan2 (V.Y, v.x);} Point Getlineintersection (Point P, Vector v, point Q, vector w) {vector u = p-q; double t = Cross (w, u)/Cross (V, W); return P + v * t;} Vector Rotate (vector A, double rad) {return vector (a.x * cos (RAD)-A.y * sin (rad), a.x * sin (rad) + a.y * cos (RAD)); Double Distancetoline (Point P, point A, point B) {Vector V1 = b-a, v2 = p-a; return fabs (Cross (v1, v2))/Length (v1); }vector anglebisector (point p, vector v1, vector v2) {//given two vectors, quadrature line Double rad = Angle (v1, v2); return Rotate (v1, dcmp (Cro SS (v1, v2)) * 0.5 * rad);} Finding the true angle of the line and X-axis (0<=x<180) double realanglewithx (vector a) {vector B (1, 0); if (dcmp (A, b) = = 0) return 0.0; else if (dcmp (Dot (A, b) = = 0)) return 90.0; Double rad = Angle (A, b); rad = (RAD/PI) * 180.0; if (dcmp (A.Y) < 0) rad = 180.0-rad; Return RAD;} struct Circle {point C; double R; Circle (point C, double r) {this->c = c; this->r = r;} Point point (Double a) {return point (c.x + cos (a) * R, C.y + sin (a) * R);}};/ /Find the intersection of line and Circle int getlinecircleintersection (point P, Vector v, circle C, Vector &sol) {Double A1 = v.x, B1 = p.x-c.c.x, C1 = v.y, D1 = p.y-c.c.y; Double e1 = A1 * A1 + C1 * C1, F1 = 2 * (A1 * B1 + C1 * D1), G1 = B1 * B1 + d1 * D1-C.R * C.R; Double Delta = F1 * F1-4 * e1 * G1, T; if (dcmp (delta) < 0) return 0; else if (dcmp (delta) = = 0) {t = (-F1)/(2 * E1); Sol.push_back (P + v * t); return 1;} else{t = (-f1 + sqrt (delta))/(2 * E1); Sol.push_back (p + v * t); t = (-f1-sqrt (delta))/(2 * E1); Sol.push_back (p + v * t); return 2; }}//two circle intersect int getcirclecircleintersection (Circle C1, Circle C2, Vector &sol) {Double d = Length (c1.c-c2.c), if (dcmp (d) = = 0) {if (dcmp (C1.R-C2.R) = = 0) return-1;//coincident return 0;} if (dcmp (C1.R + c2.r-d) < 0) return 0; if (dcmp (Fabs (C1.R-C2.R)-D) > 0) return 0; Double A = angle (c2.c-c1.c); Double da = ACOs ((C1.R * C1.R + d * D-C2.R * C2.R)/(2 * C1.R * d)); Point P1 = C1.point (a-da), p2 = c1.point (A + da); Sol.push_back (p1); if (P1 = = p2) return 1; Sol.push_back (p2); return 2;} Points to circle Tangent int gettangents (point P, Circle C, vector *v) {vector u = c.c-p; Double dist = Length (u); if (Dist < C.R) re Turn 0; else if (dcmp (DIST-C.R) = = 0) {V[0] = Rotate (U, PI/2); return 1;} else {double ang = asin (C.R/DIST); v[0] = Rotat E (U,-ang); V[1] = Rotate (U, +ang); return 2; }}//two Gongsche//a[i], b[i] is the tangent of the I line on the circle A and the circle B tangent int getcircletangents (circle A, circle B, point *a, point *b) {int cnt = 0; R < B.R) {swap (A, b), swap (A, b),}//center distance square Double d2 = (a.c.x-b.c.x) * (a.c.x-b.c.x) + (A.C.Y-B.C.Y) * (a.c.y -B.C.Y); Double Rdiff = A.R-B.R; Double rsum = A.R + B.R; Double base = angle (B.C-A.C); Coincident with infinite number of if (D2 = = 0 && dcmp (A.R-B.R) = = 0) return-1; Inscribed if (DCMP (D2-rdiff * rdiff) = = 0) {a[cnt] = A.point (base); b[cnt] = B.point (base); cnt++; return 1;}//Have Wei Panche Doubl E ang = ACOs ((A.R-B.R)/sqrt (D2)); A[CNT] = A.point (base + ang); B[CNT] = B.point (base + ang); cnt++; A[CNT] = A.point (Base-ang); B[CNT] = B.point (Base-ang); cnt++; An inner tangent, two internal tangents if (dcmp (d2-rsum*rsum) = = 0) {a[cnt] = A.point (base), b[cnt] = b.point (PI + base); cnt++;} else if (dcmp (d2-rsum*rsum) > 0) {double ang = ACOs ((A.R + B.R)/sqrt (D2)); a[cnt] = A.point (base + ang); b[cnt] = B.point (base + ang); cnt++; A[CNT] = A.point (Base-ang); B[CNT] = B.point (Base-ang); cnt++; } return CNT;} Triangular circumscribed Circle Circle circumscribedcircle (Point P1, point P2, point p3) {Double Bx = p2.x-p1.x, by = p2.y-p1.y; Double Cx = p3.x-p1.x, Cy = p3.y-p1.y; Double D = 2 * (Bx * cy-by * Cx); Double CX = (Cy * (BX * bx + by * By) – by * (CX * CX + cy * cy))/D + p1.x; Double cy = (BX * (CX * CX + cy * CY)-CX * (BX * bx + by *))/D + p1.y; Point P = Point (CX, CY); Return Circle (P, Length (P1-p));} Triangle Inscribed Circle Circle Inscribedcircle (Point P1, point P2, point p3) {Double A = length (P2-P3); double b = Length (P3-P1); Dou ble c = Length (P1-P2); Point P = (P1 * A + P2 * b + p3 * c)/(A + B + c); Return Circle (P, Distancetoline (P, p1, p2));} P1, tangent to a line (P2, W), a set of round int Circlethroughapointandtangenttoalinewithradius with a radius of R (Point P1, point P2, Vector W, Double R , Vector &sol) {Circle C1 = Circle (P1, r); Double T = r/length (w); Vector u = vector (-w.y, w.x); Point P4 = p2 + u * t; int tot = Getlinecircleintersection (P4, W, C1, SOL); U = Vector (W.y,-w.x); P4 = p2 + u * t; Tot + = Getlinecircleintersection (P4, W, C1, SOL); return tot;} Given two vectors, the center of the circle being clamped in the direction of the two vectors. The circle is tangent to the two lines, the radius of the circle is given point Centre_circletangenttwononparallellinewithradius (point P1, Vector v1, point P2, Vector v2, double R {Point p0 = Getlineintersection (P1, v1, p2, v2); Vector u = anglebisector (P0, V1, v2); Double rad = 0.5 * Angle (v1, v2); Double L = R/sin (RAD); Double T = l/length (U); return p0 + u * t;} 4 circles tangent to two non-parallel lines, radius of the circle given int Circlethroughapointandtangentalinewithradius (point p1, Vector v1, point P2, Vector v2, Double R, point *sol) {int ans = 0; sol[ans++] = Centre_circletangenttwononparallellinewithradius (P1, v1, p2, v2, r); sol [ans++] = Centre_circletangenttwononparallellinewithradius (P1, V1 *-1, P2, v2, R); sol[ans++] = Centre_circletangenttwononparallellinewithradius (p1, v1, p2, v2 *-1, R); sol[ans++] = Centre_circletangenttwononparallellinewithradius (P1, V1 *-1, p2, V2 *-1, R); return ans;} A set of circles with two absent circles, three cases int Circletangenttotwodisjointcircleswithradius (Circle C1, Circle C2, double R, point *sol) { Double dis1 = C1.R + R + R + C2.R; Double dis2= Length (c1.c-c2.c); if (dcmp (DIS1-DIS2) < 0) return 0; Vector u = c2.c-c1.c; Double T = (R + C1.R)/Length (U); if (dcmp (DIS1-DIS2) ==0) {Point p0 = c1.c + u * t; sol[0] = P0; return 1;} double AA = Length (c1.c-c2.c); Double BB = r + C1.R, CC = r + c2.r; Double rad = ACOs ((AA * AA + BB * BB-CC * cc)/(2 * AA * BB)); Vector w = Rotate (U, RAD); Point P0 = c1.c + w * t; Sol[0] = P0; W = Rotate (U,-rad); P0 = c1.c + w * t; SOL[1] = P0; return 2;} Char op[25]; Point P[4];d ouble r[3];int Main () {while (~SCANF ("%s", op)) {if (strcmp (OP, "circumscribedcircle") = = 0) {for (int i = 0; I < 3; i++) P[i].read (); Circle ans = circumscribedcircle (p[0], p[1], p[2]); printf ("(%.6f,%.6f,%.6f) \ n", ans.c. x, Ans.c.y, ANS.R); } else if (strcmp (OP, "inscribedcircle") = = 0) {for (int i = 0; i < 3; i++) P[i].read (); Circle ans = inscribedcircle (p[0], p[1], p[2]); printf ("(%.6f,%.6f,%.6f) \ n", ans.c.x, Ans.c.y, ANS.R); } else if (strcmp (OP, "tangentlinethroughpoint") = = 0) {p[0].read (); scanf ("%lf", &r[0]); P[1].read (); Vector V[3]; int tot = gettangents (p[1], Circle (p[0], r[0]), V); Double ans[3]; for (int i = 0; i < tot; i++) ans[i] = Realanglewithx (V[i]); Sort (ans, ans + tot); printf ("["); for (int i = 0; i < tot; i++) {printf ("%.6f", Ans[i]), if (i! = tot-1) printf (",");} printf ("]\n"); } else if (strcmp (OP, "circlethroughapointandtangenttoalinewithradius") = = 0) {for (int i = 0; i < 3; i++) P[i].read () ; scanf ("%lf", &r[0]); Vector Ans int tot = Circlethroughapointandtangenttoalinewithradius (p[0], p[1], p[2]-p[1], r[0], ans); Sort (Ans.begin (), Ans.end ()); printf ("["); for (int i = 0; i < tot; i++) {printf ("(%.6f,%.6f)", ans[i].x, Ans[i].y), if (i! = tot-1) printf (",");} printf ("]\n "); } else if (strcmp (OP, "circletangenttotwolineswithradius") = = 0) {point ans[4]; for (int i = 0; i < 4; i++) P[i].read (); scanf ("%lf", &r[0]); int tot = Circlethroughapointandtangentalinewithradius (p[0], p[1]-p[0], p[3], p[3]-p[2], r[0], ans); Sort (ans, ans + tot); printf ("["); for (int i = 0; i < tot; i++) {printf ("(%.6f,%.6f)", ans[i].x, Ans[i].y), if (i! = tot-1) printf (",");} printf ("]\n "); } else {p[0].read (); scanf ("%lf", &r[0]); P[1].read (); scanf ("%lf", &r[1]); scanf ("%lf", &r[2]); Point Ans[4]; int tot = Circletangenttotwodisjointcircleswithradius (Circle (p[0], r[0]), Circle (P[1], r[1]), r[2], ans); Sort (ans, ans + tot); printf ("["); for (int i = 0; i < tot; i++) {printf ("(%.6f,%.6f) ", ans[i].x, ANS[I].Y); if (i! = tot-1) printf (","); } printf ("]\n"); }} return 0;}

Computational Geometry-Circular Template training Guide 267

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.