Intersection of two circles

Source: Internet
Author: User
/* Calculates the intersection of the circle (x1, Y1, R) and the circle (X2, Y2, R). array p Stores the center (1 ~ N-1) and intersection (n ~), Array radius storage radius count is used to record the intersection points (including the number of centers) */For (INT I = 0; I <n; I ++) {// initialize double X1, y1, R; X1 = P [I]. x, Y1 = P [I]. y, r = radius [I]; double X2, Y2, R; x2 = P [J]. x, y2 = P [J]. y, r = radius [J]; double D = dist (x1, Y1, X2, Y2 ); // obtain the two circular centers from if (D> r + R | D <F (r-r) continue; // If (x1 = X2 & Y1 = Y2) continue when the two circles are separated; // else if (Y1 = Y2 & X1! = X2) {double A = (R * r-r * r)-(x1 * x1-x2 * x2)/(2 * x2-2 * X1 ); if (D = f (r-r) | D = R + r) // only one intersection point P [count]. X = A, P [count]. y = Y1, Count ++; else // two intersections {double T = r * r-(a-x1) * (a-x1); P [count]. X = A, P [count]. y = Y1 + SQRT (t), Count ++; P [count]. X = A, P [count]. y = y1-sqrt (t), Count ++;} else if (Y1! = Y2) {Double K, DISP; k = (2 * x1-2 * x2)/(2 * y2-2 * Y1); disp = (R * r-r * r) -(x1 * x1-x2 * x2)-(Y1 * y1-y2 * Y2)/(2 * y2-2 * Y1); // line offset double A, B, C; A = (k * k + 1); B = (2 * (disp-y1) * K-2 * X1); C = (disp-y1) * (disp-y1) -R * r + X1 * x1; double disc; disc = B * B-4 * a * C; // a quadratic equation discriminant if (D = f (r-r) | D = R + r) {P [count]. X = (-B)/(2 * A), P [count]. y = K * P [count]. X + disp, Count ++;} else {P [count]. X = (-B) + SQRT (disc)/(2 * A), P [count]. y = K * P [count]. X + disp, Count ++; P [count]. X = (-B)-SQRT (disc)/(2 * A), P [count]. y = K * P [count]. X + disp, Count ++ ;}}}

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.