I need to calculate the distance between two points on a coordinate system without the use of the Pythagorean theorem. This question has answers here, however they are not really what I need. I have the following problem:

I'm creating a game in which there are around twenty circles at the same time on the game screen. I need to calculate if two circles touched each other very frequently (60 times a second in most cases). I did this by using the Pythagorean theorem to calculate the distance between two circle center points. However, the battery consumption is too high, which is probably because the game calculates 400 times the square root of a number for twenty circles.

What I did to significantly decrease battery consumption was to first check whether a square on top of the circles touches another one of these square, and only in that case calculating square roots.

Now I keep thinking about whether there are perhaps other formulas to do the same job that might be impractical for every day use on paper because they perhaps are longer, yet use less battery consumption as they don't need for example a square root of a number.

Keep in mind, that I'm still in school and don't know much about "higher" mathematics, so please try to explain things as simple as possible :)

  • 2
    $\begingroup$ Do you know the radii of the circles? Then compare the square of the distance between the centers to the square of the sum of their radii. Squaring is cheaper than taking square roots. $\endgroup$ – dxiv Jun 13 '17 at 22:55
  • 1
    $\begingroup$ Kind of embarrassing that I did not think about this earlier... I was too focused on looking for alternative formulas. Thank you, this works perfectly! $\endgroup$ – Benni Jun 16 '17 at 5:58

Suppose that you want to check if circle $(x_1,y_1,r_1)$ touches circle $(x_2,y_2,r_2)$.

Then all you need to check is if $(x_1-x_2)^2+ (y_1-y_2)^2 \leq (r_1+r_2)^2$, this way no square roots are necessary, which should shave a $\log$ from the complexity.

Also, it could potentially remove the necessity of doubles.


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.