i have a line that goes through a circle. how can i find intersection points?

Hi. I have an point on edge of a circle and a direction where that point facing. When i draw a ray from that point i want that line to end at other edge of circle but i don’t know how can i calculate that point because that line does not go from center(if it would then length of line would be same as diameter of circle). if i can find the other point then i can calculate the length of it by using chord of circle formula which is what i actually need.

144629-imgg.png

Let’s take the following image representing your situation.

144634-circle.jpg

O is the center of the circle

A is the point you know

B is the point you are looking for

v is the direction vector you know

α is the angle between AO and AB vectors

ω is the angle between OA and OB vectors

Because AOB is an isosceles triangle, the OAB and OBA angles are equal. And the sum of the angles of a triangle = 180°.

Pseudo code

α  = Angle( O - A, v )
ω  = 180 - 2 * a // because ω + α + α = 180
BO = Rotate(A - O, ω)
B  = O + BO

Translated in C#:

private Vector3 ComputeB( Vector3 circleCenter, Vector3 circleNormal, Vector3 point, Vector3 direction )
{
    float a = Vector3.SignedAngle( circleCenter - point, direction, circleNormal );
    float w = 0;
    if ( a >= 0 ) w = 180 - 2 * a; // because w + a + a = 180;
    else w = -( 180 + 2 * a );
    Vector3 BO = Quaternion.AngleAxis(w, -circleNormal) * (point - circleCenter);
    return circleCenter + BO;
}