Distance to Edge of Circle

Started by
9 comments, last by MossStone 14 years, 5 months ago
I'm trying to find a way to calculate the distance between an arbitrary point inside a circle and the edge of the circle at a certain angle, but my maths is a bit rusty (ok, very rusty). Any help would be much appreciated.
[Insert Witty Quote Here]
Advertisement
distance_to_circle = abs(radius - distance_to_center)

If you know the point is inside the circle, you don't even need the abs().

EDIT: Oh, wait. What do you mean by "at a certain angle"?
You may need to be a little more specific, but if you are just looking for the distance from the edge of the circle to the point then simply do the following.

1/ Get a vector from center of circle to your point.
2/ Grab its length.
3/ Subtract length from the circles radius.

const Vector3 lkToPointFromCenterOfCircle = lPoint - lCenterOfCircle;F32 lfDistanceFromCenter = lkToPointFromCenterOfCircle.Length();F32 lfDistanceFromEdge = skfCircleRadius - lfDistanceFromCenter;


If you want to know the point on the edge of the circle then...

1/ Get a vector from center of circle to your point.
2/ Normalize
3/ Multiply by radius
4/ Add new vector to the vector representing the center of your circle.

Vector3 lToPointFromCenterOfCircle = lPoint - lCenterOfCircle;lToPointFromCenterOfCircle.Normalize();lToPointFromCenterOfCircle *= skfCircleRadius;Vector3 lPositionOnEdgeOfCircle = skCircleCenter + lToPointFromCenterOfCircle;


I think what I was trying to ask was the same as this question here http://stackoverflow.com/questions/583591/what-is-the-equation-to-calculate-the-distance-from-the-end-of-a-line-segment-to

I'm basically trying to code a radial gradient. I have a circle with center point c, radius r and focus point f and my current texture coordinate of p. Then I'm lerping between two colours based on the distance between the focus and the edge of the circle that the texture coordinate falls on.
[Insert Witty Quote Here]
const Vector3 lkToPointFromCenterOfCircle = lPoint - lCenterOfCircle;F32 lfDistanceFromCenter = lkToPointFromCenterOfCircle.Length();F32 lfLerpValue = lfDistanceFromCenter / skfRadius;lfLerpValue = FClamp( lfLerpValue, 0.f, 1.f );lOutputColour = Lerp( lColourA, lColourB, lfLerpValue );


If you know distance from center, divide by radius and will give you a 0 to 1 value you can then use as your lerp value.
That's the equation I'm using if the focal point is at the centre of the circle.

float lerp = Vector2.Distance(centre, currentPoint) / radius;

However when the focal point isn't at the centre of the circle I need to use the distance from the focal point to the edge of the circle and use it instead of the radius. The effect I'm trying to achieve is the second example shown here http://www.svgbasics.com/radial_gradients.html

Sorry if I'm not describing the problem well enough.

EDIT:

I'm trying to find the distance of fi in this diagram, and i is unknown.
[Insert Witty Quote Here]
What you need is to compute the intersection of a ray and a circle. If you parametrize the ray as

i(t) = f + (p-f)*t

Now you now that dot_product(i-c,i-c) = radius^2

dot_product((f-c) + (p-f)*t, (f-c) + (p-f)*t) = radius^2
dot_product(p-f,p-f)*t^2 + 2*dot_product(f-c,p-f)*t + dot_product(f-c,f-c) - radius^2 = 0

This is a second-degree equation in t. The solution with positive t corresponds to the point you are looking for.

For interpolating you don't actually need to compute distances: You can use t above directly.


Give it a try. If you get stuck I'll write a little bit of code for you.
Ah right, I think I've almost got it (took me a while to remember how to do quadratic equations):

//dot_product(p-f,p-f)*t^2 + 2*dot_product(f-c,p-f)*t + dot_product(f-c,f-c) - radius^2 = 0
float a = Vector2.Dot(currentPoint - focus, currentPoint - focus);
float b = 2 * Vector2.Dot(focus-centre, currentPoint-focus);
float c = Vector2.Dot(focus-centre, focus-centre)- (radius * radius);
// (ax^2) + (bx) + c = 0
float lerp = (-b + (float)Math.Sqrt((b*b) - 4*a*c)) / (2*a);

The only problem now is that it's looking inside out, but I'm pretty sure I can get the rest of the way on my own now. Thanks for the help. :)
[Insert Witty Quote Here]
float t = (-b + (float)Math.Sqrt((b*b) - 4*a*c)) / (2*a);float lerp = 1/t;
The algorithm to find the intersection of a ray and circle can be found here if you get stuck: RayToCircleIntersection.

This topic is closed to new replies.

Advertisement