Claudia
Claudia

Reputation: 726

Finding most distant point in circle from point

I'm trying to find the best way to get the most distant point of a circle from a specified point in 2D space. What I have found so far, is how to get the distance between the point and the circle position, but I'm not entirely sure how to expand this to find the most distant point of the circle.

The known variables are:

To find the distance between the point and the circle position, I have found this:

xd = x2 - x1

yd = y2 - y1

Distance = SquareRoot(xd * xd + yd * yd)

It seems to me, this is part of the solution. How would this be expanded to get the position of Point x in the below image?

The problem is graphically explained here. The desired outcome is the position of Point x. As an additional but optional part of the question: I have read in some places that it would be possible to get the distance portion without using the Square Root, which is very performance intensive and should be avoided if fast code is necessary. In my case, I would be doing this calculation quite often; Any comments on this within the context of the main question would be welcome too.

Upvotes: 1

Views: 428

Answers (1)

Damon
Damon

Reputation: 70206

What about this?

  1. Calculate A-B.
    We now have a vector pointing from the center of the circle towards A (if B is the origin, skip this and just consider point A a vector).
  2. Normalize. Now we have a well defined length (the length is 1)
  3. If the circle is not of unit radius, multiply by radius. If it is unit radius, skip this. Now we have the correct length.
  4. Invert sign (can be done in one step with 3., just multiply with the negative radius)
    Now our vector points in the correct direction.
  5. Add B (if B is the origin, skip this).
    Now our vector is offset correctly so its endpoint is the point we want.

(Alternatively, you could calculate B-A to save the negation, but then you have to do one more operation to offset the origin correctly.)

By the way, it works the same in 3D, except the circle would be a sphere, and the vectors would have 3 components (or 4, if you use homogenous coords, in this case remember -- for correctness -- setting w to 0 when "turning points into vectors" and to 1 at the end when making a point from the vector).

EDIT:
(in reply of pseudocode)
Assuming you have a vec2 class which is a struct of two float numbers with operators for vector subtraction and scalar multiplicaion (pretty trivial, around a dozen lines of code) and a function normalize which needs to be no more than a shorthand for multiplying with inv_sqrt(x*x+y*y), the pseudocode (my pseudocode here is something like a C++/GLSL mix) could look something like this:

vec2 most_distant_on_circle(vec2 const& B, float r, vec2 const& A)
{
    vec2 P(A - B);
    normalize(P);
    return -r * P + B;
}

Most math libraries that you'd use should have all of these functions and types built-in. HLSL and GLSL have them as first type primitives and intrinsic functions. Some GPUs even have a dedicated normalize instruction.

Upvotes: 6

Related Questions