Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7961437
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T05:02:10+00:00 2026-06-04T05:02:10+00:00

So apparently calculating square roots is not very efficient, which leaves me wondering what

  • 0

So apparently calculating square roots is not very efficient, which leaves me wondering what the best way is to find out the distance (which I’ve called range below) between two circles is?

Find range between two circles

So normally I would work out:

a^2 + b^2 = c^2
dy^2 + dx^2 = h^2
dy^2 + dx^2 = (r1 + r2 + range)^2
(dy^2 + dx^2)^0.5 = r1 + r2 + range
range = (dy^2 + dx^2)^0.5 - r1 - r2

Trying to avoid the square root works fine when you just look for the situation when “range” is 0 for collisions:

 if ( (r1 + r2 + 0 )^2 > (dy^2 + dx^2) )

But if I’m trying to work out that range distance, I end up with some unwieldy equation like:

 range(range + 2r1 + 2r2) = dy^2 + dx^2 - (r1^2 + r2^2 + 2r1r2)

which isn’t going anywhere. At least I don’t know how to solve it for range from here…

The obvious answer then is trignometry and first find theta:

  Tan(theta) = dy/dx
  theta = dy/dx * Tan^-1

Then the find the hypotemuse
Sin(theta) = dy/h
h = dy/Sin(theta)

Finally work out the range
range + r1 + r2 = dy/Sin(theta)
range = dy/Sin(theta) – r1 – r2

So that’s what I’ve done and have got a method that looks like this:

private int findRangeToTarget(ShipEntity ship, CircularEntity target){


    //get the relevant locations
    double shipX = ship.getX();
    double shipY = ship.getY();
    double targetX =  target.getX();
    double targetY =  target.getY();
    int shipRadius = ship.getRadius();
    int targetRadius = target.getRadius();

    //get the difference in locations:
    double dX = shipX - targetX;
    double dY = shipY - targetY;

    // find angle 
    double theta = Math.atan(  ( dY / dX ) );

    // find length of line ship centre - target centre
    double hypotemuse = dY / Math.sin(theta);

    // finally range between ship/target is:
    int range = (int) (hypotemuse - shipRadius - targetRadius);

    return range;

}

So my question is, is using tan and sin more efficient than finding a square root?

I might be able to refactor some of my code to get the theta value from another method (where I have to work it out) would that be worth doing?

Or is there another way altogether?

Please excuse me if I’m asking the obvious, or making any elementary mistakes, it’s been a long time since I’ve used high school maths to do anything…

Any tips or advice welcome!

****EDIT****

Specifically I’m trying to create a “scanner” device in a game that detects when enemies/obstacles are approaching/ going away etc. The scanner will relay this information via an audio tone or a graphical bar or something. Therefore although I don’t need exact numbers, ideally I would like to know:

  1. target is closer/further than before
  2. target A is closer/further than target B, C, D…
  3. A (linear hopefully?) ratio that expresses how far a target is from the ship relative to 0 (collision) and max range (some constant)
  4. some targets will be very large (planets?) so I need to take radius into account

I’m hopeful that there is some clever optimisation/approximation possible (dx + dy + (longer of dx, dy?), but with all these requirements, maybe not…

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-04T05:02:11+00:00Added an answer on June 4, 2026 at 5:02 am

    Math.hypot is designed to get faster, more accurate calculations of the form sqrt(x^2 + y^2). So this should be just

    return Math.hypot(x1 - x2, y1 - y2) - r1 - r2;
    

    I can’t imagine any code that would be simpler than this, nor faster.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Apparently, there's been a big brouhaha over whether or not Python needs tail-call optimization
Apparently, this does not work. WHY ??????? I don't want to do all this
Apparently I've done something I'm not aware of All my classes now have warnings
Apparently this used to be a way in VB6 and VBA to short circuit
Apparently this function in SDL_Mixer keeps dying, and I'm not sure why. Does anyone
Apparently, I can't find any help on this. I have a scenario where I
Apparently there's a lot of variety in opinions out there, ranging from, " Never!
Apparently you can easily obtain a client IP address in WCF 3.5 but not
Apparently, I'm not understanding how to use the ContinueWith method. My goal is to
Apparently (at least according to gcc -std=c99 ) C99 doesn't support function overloading. The

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.