I have a question pertaining writing a java program and in part of the program it checks if the circles overlap or intersect at any point and displays if they do.
I believe the formula to do this is (r1+r2)^2 <= (x2-x1)^2+(y2-y1)^2
After applying this formula to my code it is not able to detect if overlapping or intersecting.
//variables
//crOneRadius = radius of circle 1
//crTwoRadius = radius of circle 2
//crOneCenterX = x axis location of circle 1
//crOneCenterX = y axis location of circle 1
//crTwoCenterX = x axis location of circle 2
//crTwoCenterX = y axis location of circle 2
//BOoverlap = is boolean if it is ovelapping circles set to true
if((crOneRadius+crTwoRadius)*(crOneRadius+crTwoRadius)<=((crTwoCenterX-crOneCenterX)*(crTwoCenterX-crOneCenterX))+((crTwoCenterY-crOneCenterY)*(crTwoCenterY-crOneCenterY)))
BOoverlap=true;//overlap is true
BOverlap
result? I suggest you isolate this calculation in its own method and test it with different values (you can write a test in amain()
method if you don' t want to use JUnit or TestNG). – aro_techsumOfRadii*sumOfRadii <= deltaX*deltaX + deltaY*deltaY
. To improve readablity even more, you might even want to write your ownsquare()
method or useMath.pow(x,2)
, giving something likesquare(sumOfRadii) <= square(deltaX) + square(deltaY)
. – aro_tech