• Create Account

# Solve path around a given point and radius

Old topic!

Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

6 replies to this topic

### #1Slig Commando  Members   -  Reputation: 360

Like
0Likes
Like

Posted 12 January 2014 - 12:48 PM

The attached image helps to describe my problem.

Given a direction vector(V1), location to avoid along with the radius, and a destination point, how could I calculate the blue travel path?

I know that given the angle in radians of a circle you can find the vector location at that angle on the circle, but I am not sure how I would implement this equation to solve this.

Thank you

### #2Doublefris  Members   -  Reputation: 316

Like
0Likes
Like

Posted 12 January 2014 - 01:24 PM

If you are having a point particle:

vector velocity = normalize(destination - start) * speed;

vector t = p - c; //vector from point to circle center
float d = dot(t, t); // squared length of t
if( d < r*r)  // check if circle radius^2 > d
{
float root = sqrt(d);
vector overlap = (t / root) * (r - root);
velocity -= overlap; // offset the point
}



edit:small correction

Edited by Doublefris, 12 January 2014 - 01:38 PM.

### #3ferrous  Members   -  Reputation: 4135

Like
1Likes
Like

Posted 13 January 2014 - 01:11 PM

So, it looks like you'll end up with sort of an inverse dubins car, where instead of an arc, line, arc, you end up with a line, arc, line.

So, first thing I'd do is a Line vs Circle intersection test, and get the two points of intersection with the circle.  (if you only have 1, then you are in the circle, and no points, you just head down the line.)

From there, you just need the start angle (atan2 is your friend) and the angle between that and the end point.  I can dig up the exact code later if you need it.

angleStart = atan2(ptStart.y - center.y, ptStart.x - center.x);
deltaAngle = atan2(ptEnd.y - center.y, ptEnd.x - center.x);
if (deltaAngle < 0.0f && (!clockwise))
{
deltaAngle += 2.0f * PI;
}
else if (deltaAngle > 0.0f && (clockwise))
{
deltaAngle -= 2.0f * PI;
}

endTheta = startTheta + deltaAngle



You might be able to figure out ahead of time whether you'd be going clockwise or not, worse comes to worse, check both angles, take the smaller arc length

Edited by ferrous, 14 January 2014 - 10:21 AM.

### #4cadjunkie  Members   -  Reputation: 1861

Like
2Likes
Like

Posted 14 January 2014 - 06:44 PM

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.

### #5LorenzoGatti  Crossbones+   -  Reputation: 3543

Like
2Likes
Like

Posted 16 January 2014 - 03:18 AM

The shortest path would have the two straight line portions tangent to the circumference rather than aligned with each other. As a bonus, continuity would be G1 rather than G0.

Omae Wa Mou Shindeiru

### #6ferrous  Members   -  Reputation: 4135

Like
0Likes
Like

Posted 16 January 2014 - 04:29 PM

The shortest path would have the two straight line portions tangent to the circumference rather than aligned with each other. As a bonus, continuity would be G1 rather than G0.

How would one go about calculating that?  Find the two tangent lines to the circle for each endpoint, turn them into rays and intersect them with each other?

Edited by ferrous, 16 January 2014 - 04:29 PM.

### #7LorenzoGatti  Crossbones+   -  Reputation: 3543

Like
0Likes
Like

Posted 17 January 2014 - 02:03 AM

The shortest path would have the two straight line portions tangent to the circumference rather than aligned with each other. As a bonus, continuity would be G1 rather than G0.

How would one go about calculating that?  Find the two tangent lines to the circle for each endpoint, turn them into rays and intersect them with each other?

Almost. You would intersect the two tangents with the circle, to find the tangency points where the arc portion of the path begins and ends.

A concise explanation with formulas: http://mathworld.wolfram.com/CircleTangentLine.html

Omae Wa Mou Shindeiru

Old topic!

Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

PARTNERS