Domanda

Given a description of an arc which has a startpoint and endpoint (both in Cartesian x,y coordinates), radius and direction (clockwise or counter-clockwise), I need to convert the arc to one with a start-angle, end-angle, center, and radius.

Is there known algorithm or pseudo code that allows me to do this? Also, is there any specific term to describe these kinds of transformations?

È stato utile?

Soluzione

You can find a center solving this equation system:

(sx-cx)^2 + (sy-cy)^2=R^2
(ex-cx)^2 + (ey-cy)^2=R^2

where (sx,sy) are coordinates of starting point, (ex,ey) for ending point, unknowns cx, cy for center. This system has two solutions. Then it is possible to find angles as

StartAngle = ArcTan2(sy-cy, sx-cx)
EndAngle = ArcTan2(ey-cy, ex-cx)

Note that known direction doesn't allow to select one from two possible solutions without additional limitations. For example, start=(0,1), end=(1,0), R=1 and Dir = clockwise give us both Pi/2 arc with center (0,0) and 3*Pi/2 arc with center (1,1)

Altri suggerimenti

I'd propose a different approach than MBo to obtain the centers of the two circles, which have the given radius and pass to both start and end point.

If P and Q are start and end point of the arc, the center of each of the two circles lies on the line L which is orthogonal to PQ, the line from P to Q, and which bisects PQ. The distance d from the centers to L is easily obtained by Pythagoras theorem. If e is the length of PQ, then d^2 + (e/2)^2 = r^2. This way you avoid to solve that system of equations you get from MBo's approach.

Note that, in case you have a semicircle, any approach will become numerically unstable because there is only one circle of the given radius with P and Q on it. (I guess I recall the correct term is 'the problem is ill posed' in that case. It happens when P and Q are precisely 2r apart, and to figure out whether this actually true you need to check for equality of two doubles, which is always a bit problematic. If, for some reason, you know you have a semicircle you are better of to just calculate the center of PQ).

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top