Tangents To Circles And Ellipses
From GPWiki
The wiki is now hosted by GameDev.NET at wiki.gamedev.net. All gpwiki.org content has been moved to the new server. However, the GPWiki forums are still active! Come say hello.
[edit] AbstractIn this tutorial I present a conceptually simple and computationally inexpensive way to compute the tangents to circles and ellipses. The method presented is remarkably simple, however it is rather technical and does require understanding of basic linear algebra. [edit] Prerequisite Linear AlgebraThe math in this article is unfortunately fairly technical. It is not necessarily difficult (it is, in fact, fairly straightforward), but merely uncommon knowledge. For many people, hearing of "homomorphisms of vector spaces" is something akin to stirring thoughts of the occult. There is little I can do about this, and I make no pretense of having spared from the reader these complexities. Rather than include an exposition on the prerequisite notions of linear algebra and abstract algebra, I will instead give a list of links to external resources explaining these things in much more detail than I could here. You should be familiar with the following concepts:
Although unnecessary, it would ease comprehension to be familiar with Set Notation. [edit] Tangents to a CircleA circle or 2-sphere is given by the set
for some positive If the circle is positioned so that its center is the origin, then we have that the vector normal to the circle at the point We may parameterize a line in the direction of
Hence, the slope of the line
However, this line will pass through the origin, not the point at which the tangent it describes touches the circle. We remedy this by applying the offset
Observe that we obtain the very same tangent by flipping the signs of the x- and y-components of [edit] Tangents to an EllipseFirstly, I should note that there exist many methods for computing the tangents to ellipses. The one which follows is only one of them, and may or may not suit your needs. The derivation is detailed, however, and is such that it has a variety of applications other than ellipse tangents. To determine the tangent to an ellipse, we use the clever technique of mapping our ellipse into a new vector space in which it becomes a unit circle. Consider the ellipse in the above diagram. It can be seen that we may uniquely specify an ellipse so long as it is situated so that its center is the origin by a tuple (w,h) where w and h are its half-width and half-height, respectively. Here, we exploit the properties of a vector space. The trick is to map points along the ellipse The change in basis vectors means that the point (w,0) in Such a mapping may be defined as follows:
However, this is nothing but a linear transformation, and a simple one at that, so we shall simply write it as a matrix:
We may prove the correctness of this transformation by showing that applying it to the standard equation for an ellipse yields a geometric entity for which all points are equidistant from the origin (i.e. a circle):
However,
and
from the standard equation for an ellipse above. Hence,
We now show that all points of the form λ(x,y) where (x,y) is a point on the ellipse in Euclidean space are equidistant from the origin in E (assuming the ellipse was centered on the origin). We know that
However, observe that the radicand is the left side of the standard equation for an ellipse. We derived our new vector λ(x,y) based on the assumption that
and we have proved that not only is our new structure a circle, but the unit circle. Suppose now that we have mapped our ellipse into E. Then it's circular image is given by
That is, it is consisted of all points
However, this line exists in E. To transform it into
Hence, the tangent to an ellipse in standard Euclidean space
This equation requires, however, that the point In order to use the above equation, we must first map
Simplifying yields the following parameterization:
As an example, consider that we have an ellipse parameterized by (x(t),y(t))E = (wcost,hsint). Then, any point along the ellipse's circular image is simply (x(t),y(t))C = (cost,sint) obtained by multiplying (x(t),y(t))E by the λ transformation. This shows much more clearly than the laborious math above that λ properly maps ellipses to circles. By the above, we may determine the tangent to the ellipse at a point thereon with angle of elevation α thusly: (x(t),y(t))t = (wcosα - wtsinα,hsinα + htcosα), (x(t),y(t))t = (w(cosα - tsinα),h(sinα + tcosα)). [edit] Testing ItI have tested the formula using the Microsoft Student Graphing Calculator 2006 so that I could watch the tangent move around the circle as I changed the parameters. If you have this software, or similar software, I highly recommend inputting the equations so you can see first-hand that they work. Here is a picture of a tangent computed with the formula: In the image, the trigonometric parameterizations for the ellipse and it's tangents have been used, with the following:
[edit] Transformations to CodeTransforming the final equation into code is an easy and straightforward process. Here is a snippet of pseudo-code which demonstrates how one might use the equation // Exactly how this will be implemented will depend on how you store your ellipse. We will // assume the ellipse is located at the origin and specified by the tuple (w,h) as discussed // in the tutorial. // To store an ellipse centered at the origin: struct Ellipse2D { float HalfWidth; float HalfHeight; }; // Simple 2-vector: struct Vector2D { float x; float y; // Include constructor... }; // To calculate the tangent depends on the exact information you want about it. If, for example, you simply want a vector // describing the direction of the tangent to an ellipse at a point, you might do the following: Vector2D DetermineEllipseTangent( const Ellipse2D& Ellipse, const Vector2D& Point ) { // This is just an implementation of the equation in the tutorial. All we need is a vector // giving the direction of the tangent, hence we only pay attention to the final terms. // The variable t is simply a parameter to the equation. We are only paying attention to // the final terms, and since they give the slope (i.e., the direction of the tangent), we // let t = 1. Vector2D tangent = Vector2D(0, 0); tangent.x = Point.x - (Ellipse.HalfWidth*Point.y)/Ellipse.HalfHeight; tangent.y = Point.y + (Ellipse.HalfHeight*Point.x)/Ellipse.HalfWidth; return tangent; } [edit] Various ApplicationsIn all it's simplicity, the λ transformation matrix is rather powerful. It morphs ellipses into circles, and it's inverse morphs circles into ellipses. Here I list several ways to use or exploit the equation for the ellipse tangent found in this article or the λ matrix itself. [edit] Rebound Effect in PongOne application of the tangent equation is that of obtaining a good rebound effect in Pong. Often times, in Pong when the ball collides with a paddle, a perturbation is applied to the reflected path of the ball to add variety to the game. We can systematically compute this perturbation easily by exploiting the equation for an ellipse tangent. Assume for simplicity that the paddle is a rectangle and the ball is a sphere. This means the normals to the paddle on the upper-side are all the same. If we cleverly treat the paddle, however, as if it were an ellipse (even though it is not and is not so rendered), we can obtain a curved upper-side which contributes itself to the perturbation applied to the ball's reflected path. That is, we reflect the ball's velocity vector over the normal to the ellipse approximating the paddle rather than the actual normal. In this case, the half-width and half-height w and h become tuning constants which we may use to control how much perturbation is applied. We know that the tangent to an ellipse at a point
Hence, the normals to an ellipse are given parametrically by
However, how do we determine
Hence, the vector normal to the ellipse over which to reflect the ball's velocity vector is
[edit] Collision Detection with EllipsesThis does not directly deal with ellipse tangents, but rather is an exploitation of the properties of the λ transformation. Suppose we wish to perform a simple boolean intersection test of an ellipse and a segment. This sounds difficult, but it doesn't have to be. If we map both the ellipse and the segment into E, then we have the situation that the ellipse becomes a circle but the segment remains a segment. We may then perform a boolean intersection test of a circle and a segment! A detailed explanation of how to do this (including the ellipse-to-circle trick) may be found here. |
) which preserves all relevant structure (e.g.,
. The unit circle, then, is that for which
is
, that is, points along the circle specify implicitly the normals. We will not prove this, but take it on intuition.
by the vector-valued function
.
is
. It follows from elementary algebra that the vector perpendicular to
, which describes the slope of the tangent line. We may parameterize this line similarly:
.
:
.

, where
becomes
.
.
.
.
which are unit distance from the origin. Thus, we may use the math described in the section "Tangents to a Circle" above to determine it's tangents as being lines parameterized by
.
represented in
.
. If we let
be the tangential vector to the ellipse's circular image, then we have
.
.
.
is the point on the rectangle and
is the point on the ellipse:
,
.
.


