Draw arc from 2 points and radius

Started by
4 comments, last by BlackJoker 8 years, 2 months ago

Hi, I am trying to draw arc from 2 points and radius, but my drawing algorithm need center + radius + start and end angles for drawing.

Center I can easily find, radius already present, but how to find angles?

Is there a way to convert start and end points + radius to start and end angles?

Does anyone faced with such problem?

Advertisement

There are two valid solutions for an arc, given two points and a radius. Consider that if the two points form an imaginary line, the arc could be on either side on that line, if unconstrained otherwise.

The equation is invalid if the distance between the points is greater than radius * 2, because the arc cannot reach both the points if this is the case. It is also invalid if the points are exactly coincidental, because this would imply that the center would be infinitely far away.

For any other case, the center of the arc can be thought to offset from the middle of the two points, perpendicular (or 90 degrees) to the line formed by the points. I don't have the exact formula for the distance, but it can be solved with some arc trigonometry.

When you have determined the center offset, you have to pick the side of the line to which to offset the center of the arc. Commonly this is done by defining the order of the two points (start and end) and rotating the formed line 90 degrees about the start point to arrive to a baseline direction. Then you'd take the dot product of said baseline direction and the offset vector to determine which side you're on, and constrain the direction based on that.

You can find the angles by observing the tangent of the circle, part of which the arc is. The tangent of a given point on a circle is the vector from the center of the circle to the point, rotated 90 degrees (or pi/2 radians) and starting from said point. The actual angle on the xy plane (about z) is given by atan2(x,y), with x and y being the tangent vector's components.

Niko Suni

Sorry, but my math skills not enough to understand you correctly. Could you please right some kind of pseudo code that I could understand you better, please?

See the attached file.

Consider that n1 is the normalized vector from the center to the arc start point p1. Its tangent t1 with respect to the circle is simply n1 rotated 90 degrees. If you need to "place" the tangent vector, move it to start it from the arc point. However, for measuring the screen-space angle of it, you don't have to offset it.

angleOfTangent1 = atan2 (t1.x, t1.y)

Since you calculate the n1 already, you can find the angle of the tangent vector by swapping the x and y components of the normal n1 (thus implicitly performing the 90deg rotation), and put them to the atan2.

You can find the t2 and its angle exactly the same way.

Note that common implementations of atan2 return the angle in radians. 90 degrees is pi/2 radians. Also, atan2 is usually a helper layer on top of atan, that considers if either or both components are zero or negative, and adjusts the return value accordingly to arrive to a correct angle across the full circle. Atan, by itself, would only make sense when both parameters are positive.

Niko Suni

basic trig.

theta = atan(y/x)

where:

theta is the angle from the center to the point (p1 or p2)

x,y are the coordinates of the point when the center is at the origin.

be sure to adjust accordingly based on which quadrant the point lies in.

see trig, unit circle for further info.

https://en.wikipedia.org/wiki/Unit_circle

https://upload.wikimedia.org/wikipedia/commons/8/8f/Unit_circle.svg

note that theta is measured starting from the 3 o'clock position in a counter clockwise direction. you may need to convert that to the degrees system used by your graphics package.

Norm Barrows

Rockland Software Productions

"Building PC games since 1989"

rocklandsoftware.net

PLAY CAVEMAN NOW!

http://rocklandsoftware.net/beta.php

Thanks for hint. Seems that works.

This topic is closed to new replies.

Advertisement