Line & Circle Intersetcion

This topic is 2154 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

Recommended Posts

Hi there,

i am having so much troubles finding C++ code that tell me what is the point of intersection of a circle and a line that grows from its center.

I have a circle with center C and a point A  (is given by the touch of a person on the screen)

The line between C-A intersect with the circle in some place in the circle Z, i want to know how to calculate this Z(x,y) and if it possible a c++ solution will be awesome.

Has someone some useful code i can try?

SO many thanks mates

I have reades this web but when i translate the code i got some strange values , dunno why-

http://mathworld.wolfram.com/Circle-LineIntersection.html

As i said the only important point is the one which is isnide the circle AND is between the center C and the touch point A.

Share on other sites

let c = (cx, cy) EDIT: That's the centre of the circle, should have mentioned that huh? lol

let touch point p = (px, py)

vector from c to p = p - c = (px - cx, py - cy), call this r.

intersect point = c + normalize( r ) * radius

You can also use trig, the angle in radians from the centre is atan2(ry, rx) but then you have to use trig again to find intersect point = (cx + radius * cos(angle), cy + radius * sin(angle)).

I'm guessing the mathworld link is for the generic case of line-circle intersection (i.e. line doesn't have to go through the centre so there are 0, 1 or 2 solutions).

Share on other sites

@OP you say that A is outside the circle. Is the always the case? Could the user touch within the circle? What point do you want returned if they do?

-Josh

• 9
• 18
• 29
• 11
• 24