How to get the size of the intersecting part in a circle in Java

I need the black part size of this image:
Image

I did some research on how to find it in ordinary math, and I was pointed to this site: Website

The final answer to this was pict
(from MathWorld - Wolfram web resource: wolfram.com )

where r is the radius of the first circle, R is the radius of the second circle, and d is the distance between the two centers.

The code I tried to use to get its size was as follows:

float r = getRadius1(); float R = e.getRadius1(); float deltaX = Math.abs((getX() + getRadius()) - (e.getX() + e.getRadius())); float deltaY = Math.abs((getY() + getRadius()) - (e.getY() + e.getRadius())); float d = (float) Math.sqrt(Math.pow(deltaX, 2) + Math.pow(deltaY, 2)); float part, part2, part3; //Chopping it in parts, because it easier. part = (float) (Math.pow(r,2) * Math.acos( Math.toRadians((Math.pow(d, 2) + Math.pow(r, 2) - Math.pow(R, 2))/(2*d*r)))); part2 = (float) (Math.pow(R,2) * Math.acos( Math.toRadians((Math.pow(d, 2) + Math.pow(R, 2) - Math.pow(r, 2))/(2*d*R)))); part3 = (float) (0.5 * Math.sqrt((-d + r + R) * (d+rR) * (d-r+R) * (d+r+R))); float res = part + part2 - part3; Main.log(res + " " + part + " " + part2 + " " + part3+ " " + r + " " + R + " " + d); //logs the data and System.out it 

I did some testing and the result was this:

 1345.9663 621.6233 971.1231 246.78008 20.0 25.0 43.528286 

So, this indicates that the size of the overlapping part was larger than the circle itself (i.e. r^2 * PI ).

What have I done wrong?

+6
source share
1 answer

Just suppose (as indicated in my comment): try removing the Math.toRadians(...) transform.

Since there are no degrees in the formula, but rather radii, I assume that the parameter cos -1 (...) is already a value in radians.

If I delete the transform and run your code, I get the following overlap area size: 11.163887023925781 , which seems plausible, since the length of the overlap segment on the line between the two centers is 20 + 25 - 43.5 = 1.5 (approximate)

Edit:

If I set the distance to 5 (the smaller circle was completely contained in the larger one, but touched its edge), I get the size of the overlap area 1256.63 , which is exactly the area of ​​the smaller circle (20 2 * ). The calculation does not work if the distance is less than the difference of the radii (i.e., in your case, less than 5), but it may just be a problem of numerical representation (ordinary data types may not be able to present some intermediate results).

+5
source

Source: https://habr.com/ru/post/905637/


All Articles