Here's a fairly decent way to solve this problem. I'm assuming this will be a "line,circle,line" problem, but you can alter this for different cases. I would put the line into parametric form and the circle into implicit form:

\[ C(t) = (x-a)^2 + (y-b)^2 - r^2 = 0; L(x(t),y(t)) = [x_0 + ct, y_0+dt] \]

Here, \( (a,b) \) is the center of the circle, \( r \) is the radius, and \( (x_0,y_0) \) is the start point of the path and your direction vector \( V_1 = (c,d) \). By substituting the parametric line formulas into the implicit circle formula, we can get a polynomial whose roots are the intersection points in the line parameter space:

\[ \begin{aligned} (x_0+ct-a)^2 + (y_0+dt-b)^2 -r^2 &= 0 \\ (c^2+d^2)t^2+2[c(x_0-a)+(y_0-b)]t+[(x_0-a)^2+(y_0-b)^2-r^2] &= 0 \\ At^2+Bt+c &= 0 \\ \end{aligned} \]

The roots can be solved for quickly using the quadratic equation:

\[ t = \frac{-B\pm \sqrt{B^2-4AC}}{2A} \]

If the discriminant \( B^2 - 4AC < 0 \), then you have no intersections. If \( B^2 - 4AC = 0 \), you've got 1 intersection. If \( B^2 - 4AC > 0 \), you've got 2 intersections, which is really the only case you have to worry about to avoid the object since the path can stay linear for any other cases. You can then evaluate the parametric line at the roots of the polynomial to get the intersection points in (x,y) form. You can get the angles of the points on the circle using \( \theta = \text{atan2}(y-b, x-a) \). If the problem is as shown in the OP's picture (where the starting path point isn't inside the circle), you also know which point to start from and which point to end with because the point with the lesser parameter value t is the point closest to \( (x_0,y_0) \).

Deciding which path around the circle is shorter is the last issue to resolve. We could calculate arc length, but we can figure out which way to go by using the implicit form of the line and figuring out which side of the line the circle's center is. The implicit line formula is given by \( \mathcal{A}x+\mathcal{B}y+\mathcal{C}=0 \). The coefficients for the implicit line are simple to calculate from the parametric form: \( \mathcal{A} = -d, \,\mathcal{B} = c, \,\mathcal{C} = dx_0-cy_0 \). The equation then becomes \( D = d(x_0-a)+c(b-y_0) \). If D < 0, then the circle center is on the right side of the line (looking in the direction of the line). That means the clockwise path around the circle is the shortest path. If D > 0, then the circle center is on the left side of the line, which means the counterclockwise path is the shortest. If D = 0, then the circle center lies on the line and each path is the same length.

You can step with constant velocity along the path by using the parametric form of the line and along the circle by figuring out your angle step via \( ds = r d\theta \). As I see it, this is fairly elegant because there's nothing more than additions, multiplications, atan2, and a square root function, making it a fairly fast method.

Hope that helps!

**Edited by cadjunkie, 14 January 2014 - 06:56 PM.**