How to calculate the intersection of two circles (Java)

 

 

出处:http://fypandroid.wordpress.com/2011/07/03/how-to-calculate-the-intersection-of-two-circles-java/

Let’s say you’re trying to find the intersection points of the circles C1 and C2 where C1 has it’s center point at (-9, 1) and has a radius of 7, and C2’s center lies at (5, -5) and has a radius of 18.
Note that I posted the image to make clear what the variables names in the formula’s are.

First calculate the distance, ‘d’, between the center-points of the two circles:

Expand|Select|Wrap|Line Numbers
  1. d = √(|-9 – 5| + |1 – -5|)
  2.   = 15.23

Now we calculate ‘d1′:

Expand|Select|Wrap|Line Numbers
  1. d1 = (r1^2 – r2^2 + d^2) / 2*d
  2.    = (7^2 – 18^2 + 15.23^2) / 2*15.23
  3.    = (49 – 324 + 231.95) / 30.46
  4.    = -43.05 / 30.46
  5.    = -1.41

Now we solve ‘h’, which is 1/2 * ‘a’

Expand|Select|Wrap|Line Numbers
  1. h = √(r1^2 – d1^2)
  2.   = √(7^2 – -1.41^2)
  3.   = √(49 – 1.99)
  4.   = 6.86

To find point P3(x3,y3) which is the intersection of line ‘d’ and ‘a’ we use the following formula:

Expand|Select|Wrap|Line Numbers
  1. x3 = x1 + (d1 * (x2 – x1)) / d
  2.    = -9 + (-1.41 * (5 – -9)) / 15.23
  3.    = -10.29
  4. y3 = y1 + (d1 * (y2 – y1)) / d
  5.    = 1 + (-1.41 * (-5 – 1)) / 15.23
  6.    = 1.55

Last but not least, calculate the points P4_i and P4_ii which are the intersection points of the two circles:

Expand|Select|Wrap|Line Numbers
  1. x4_i = x3 + (h * (y2 – y1)) / d
  2.      = -10.29 + (6.86 * (-5 – 1)) / 15.23
  3.      = -12.99
  4. y4_i = y3 – (h * (x2 – x1)) / d
  5.      = 1.55 – (6.86 * (5 – -9)) / 15.23
  6.      = -4.75
  7. x4_ii = x3 – (h * (y2 – y1)) / d
  8.       = -10.29 – (6.86 * (-5 – 1)) / 15.23
  9.       = -7.59
  10. y4_ii = y3 + (h * (x2 – x1)) / d
  11.       = 1.55 + (6.86 * (5 – -9)) / 15.23
  12.       = 7.86

So, as you can see, the intersection points are (-12.99, -4.75) and (-7.59, 7.86)

Reference

http://bytes.com/topic/java/answers/645269-circle-circle-intersection-more

 

 
posted @ 2014-08-07 13:06  Unikanade  阅读(306)  评论(0编辑  收藏  举报