Ellipse Line Intersection PDF
Ellipse Line Intersection PDF
by
Abstract:
This paper addresses the mathematical equations for ellipses rotated at any angle and how
to calculate the intersections between ellipses and straight lines. The formulas for
calculating the intersection points are derived, and methods are given for plotting these
ellipses on a computer.
Rotating Points
Rotating an Ellipse
Rotating an Ellipse More Than Once
Constructing (Plotting) an Ellipse
Constructing (Plotting) a Rotated Ellipse
Finding the Foci of an Ellipse
Intersection of Lines with a Rotated Ellipse
Intersection with a Horizontal Line
Intersection with a Vertical Line
Intersection of Rotated Ellipse with Sloping Line(s)
Bounding Box for a Rotated Ellipse
Determining the Tangent to an Ellipse
Determining the Tangent to a Rotated Ellipse
Approximating a Segment of an Ellipse with a Bezier Curve
Rotating Points
First, we will rotate a point (x1, y1) around the origin by an angle α.
55
50
(x,y)
y 45
40
35
r
30
(x1 , y1 )
y1 25
20 r
15 α+β
10
α
5
5 10
β 15 20 25 30 35 40 45 50 55
x x1
x1 = r cos β
y1 = r sin β
After rotating the point by angle α, the new coordinates are
x = r cos(β + α )
y = r sin (β + α )
Applying the formulae for the sine and cosine of the sum of two angles,
If you rotate that point around a “center of rotation” at (e1, f1), you get
40
(x1 , y 1 )
30
v
(x, y)
α α
h
20
(e 1 , f 1 )
10
-10 10 20 30 40 50 60 70 80
So if (x1, y1) is a point on the ellipse and (e1, f1) is the center of the ellipse (see above
figure), then equations (2) are true for all points on the rotated ellipse. The “line” from
(e1, f1) to each point on the ellipse gets rotated by α.
To rotate an ellipse about a point (p) other then its center, we must rotate every point
on the ellipse around point p, including the center of the ellipse.
120
100 α
(e, f)
80
60
(e 1, f1 )
40
α
20
(p)
-40 -20 20 40 60 80 100 120 140 160 180 200
This is as if we put a pin in the graph at point p and rotated the entire sheet of paper
around the pin.
Here we are rotating the red ellipse centered at (e1 , f1) around point (p) by an angle α.
So, for every point (x1, y1) on the original ellipse, the rotated point is
x = ( x1 − p x )cos α − ( y1 − p y )sin α + p x
y = ( y1 − p y )cos α + ( x1 − px )sin α + p y
and the rotated center is
So the directed line from the new center (e, f) to the rotated point (x, y) can be expressed
as
which have the same form as equations (2) for the ellipse rotated around its center, except
that the new ellipse is centered at (e, f).
Note: If we are rotating about the center, then (p) = (e1 , f1) and (e, f) = (e1 , f1)
and we are back to equations (2).
When an ellipse gets rotated by angle α about a point p other than its center, the
center of the ellipse gets rotated about point p and the new ellipse at the new
center gets rotated about the new center by angle α.
Rotating an Ellipse More Than Once
If an ellipse is rotated around one center of rotation and then again around a different
point, what is the result?
150
α 1+α 2
(e 3 , f3 )
100 α1
(e 2 , f2 )
α2
50
(e 1 , f1 )
(q) α1
(p)
-150 -100 -50 50 100 150
Let us assume that the ellipse has its center at (e1, f1). The ellipse is first rotated about the
point p by angle of α1, and then rotated about the point q by angle α2.
Using the rule derived in the preceding section, we can compute the center point (e2, f2)
after the first rotation
Then compute the final center point (e3, f3) with similar equations
Finally, the ellipse is plotted centered at point (e3, f3) with a rotation of (α1 + α2).
In other words,
The center point is successively rotated (translated) around each of the two
centers of rotation, and the ellipse itself is rotated about the new center by the
sum of the two rotation angles.
This makes plotting the rotated ellipses easier, since you only have to rotate the points on
the ellipse once.
This process can be applied any number of times for multiple rotations around multiple
points.
Constructing (Plotting) an Ellipse
x = h cos β (3a)
y = v sin β (3b)
x2 y 2
satisfies the equation + = 1 . Simply substitute
h2 v2
(h cos β )2 + (v sin β )2 = h 2 cos 2 β + v 2 sin 2 β = cos 2 β + sin 2 β ≡ 1 .
h2 v2 h2 v2
Therefore, equations (3) satisfy the equation for a non-rotated ellipse, and you can simply
plot them for all values of β from 0 to 360 degrees. For ellipses not centered at the
origin, simply add the coordinates of the center point (e, f) to the calculated (x, y).
if we let
x1 = h cos β
y1 = v sin β
and then rotate these points around the origin by angle α,
x = x1 cos α − y1 sin α
y = y1 cos α + x1 sin α
substituting
You can plot them for all values of β from 0 to 360 degrees. For ellipses not centered at
the origin, simply add the coordinates of the center point (e, f) to the calculated (x, y).
If the ellipse is rotated multiple times around multiple points, first calculate the new
center point by successively rotating it around each center of rotation (equations 2), then
plot the ellipse at the new center point, rotating the ellipse by the sum of the rotation
angles.
Finding the Foci of an Ellipse
b (x, y)
20
d2 d1
10
-c c
-10
-20
If you need to compute its foci, the conversion is easy. The formula is
c2 = a 2 − b2
where a is the major axis and b is the minor axis (measured from the center to the edge of
the ellipse). c is the distance from the center to each focus. The foci lie along the major
axis. If the ellipse is a circle (a=b), then c=0.
You can reverse this conversion if you know the foci and either of the axes, however if
all you have is the foci, you cannot determine a and b.
If you know the foci and any point (x, y) on the ellipse, you can calculate the sum of the
distances to the two foci:
d1 = (x − c )2 + y 2
d2 = (x + c )2 + y 2
Assume we have an ellipse with horizontal radius h and vertical radius v, centered at the
origin (for now), and rotated counter-clockwise by angle α.
(x, y)
20
(x1, y 1)
10
-10
-20
2 2
x1 y
2
+ 12 = 1 (5)
h v
where x1 and y1 are the coordinates of points on the ellipse rotated back (clockwise) by
angle α to produce a “regular” ellipse, with the axes of the ellipse parallel to the x and y
axes of the graph (“red” ellipse).
Using the equations for rotating a point about the origin by angle α clockwise (that is, a
counter-clockwise rotation of -α) , we get
Plugging these into equation (5) for the non-rotated ellipse, we get the equation for a
rotated ellipse:
Expanding,
x 2 cos 2 α + 2 xy cos α sin α + y 2 sin 2 α y 2 cos 2 α − 2 xy cos α sin α + x 2 sin 2 α
+ =1
h2 v2
or
v 2 x 2 cos 2 α + 2v 2 xy cos α sin α + v 2 y 2 sin 2 α
+ h 2 y 2 cos 2 α − 2h 2 xy cos α sin α + h 2 x 2 sin 2 α − h 2 v 2 = 0 (7)
(x, y)
20
(x1, y1)
10
-10
-20
Assume that y is a constant, and re-write equation (7) as a quadratic equation for
x:
(v 2
)
cos 2 α + h 2 sin 2 α x 2 + 2 y cos α sin α v 2 − h 2 x ( )
(
+ y 2 v 2 sin 2 α + h 2 cos 2 α − h 2 v 2 = 0 ) (8)
− b ± b 2 − 4ac
x= , where:
2a
Now, returning to the general case where the ellipse is not centered at the origin, assume
the center of the ellipse is at (e, f). The translation is simple. Merely subtract f from the
value of y, and add e to the resulting values of x.
Note: For a “normal” or non-rotated ellipse, (α = 0), the equations simplify to:
a = v2 (10a)
b=0 (10b)
(
c = h2 y2 − v2 ) (10c)
Intersection with a Vertical Line.
(x, y)
20
10
(x1, y1)
-40 -30 -20 -10 10
α 20 30 40 50 60
-10
-20
(v 2
)
sin 2 α + h 2 cos 2 α y 2 + 2 x cos α sin α v 2 − h 2 y ( )
( )
+ x 2 v 2 cos 2 α + h 2 sin 2 α − h 2v 2 = 0 (11)
− b ± b 2 − 4ac
y= , where:
2a
Now, returning to the general case where the ellipse is not centered at the origin, assume
the center of the ellipse is at (e, f). The translation is simple. Merely subtract e from the
value of x, and add f to the resulting values of y.
Note: For a “normal” or non-rotated ellipse, (α = 0), the equations simplify to:
a = h2 (13a)
b=0 (13b)
(
c = v2 x2 − h2 ) (13c)
Intersection of Rotated Ellipse with Sloping Line(s)
20
10
-10
-20
This is the general case where the ellipse is rotated by angle α and the line has the
equation
Note: We use b1 instead of b to avoid confusion with the b parameter in the quadratic
equation.
Expanding,
or
( ) (
a = v 2 cos 2 α + 2m cos α sin α + m 2 sin 2 α + h 2 m 2 cos 2 α − 2m cos α sin α + sin 2 α )
(14a)
2
( 2
) 2
(
b = 2v b1 cosα sin α + m sin α + 2h b1 m cos α − cos α sin α
2
) (14b)
(
c = b1 v sin α + h cos α − h v
2 2 2 2 2
) 2 2
(14c)
where, again,
− b ± b 2 − 4ac
x=
2a
Note: This method will not work for vertical lines (infinite slope). That would require
reversing the above solution to solve for y instead of x. It is much easier, in this
case, to use the special formula described above for intersections with vertical
lines (equations (12)).
Note: Substituting m = 0 and b1 = y (for a horizontal line) into the above formulas for a,
b, and c (equations 14), we get the same formulas as equations (9).
Again, returning to the general case where the ellipse is not centered at the origin,
assume the center of the ellipse is at (e, f). The equation for the line must also be
translated to coordinates with their origin at (e, f). Letting x1 and y1 be those coordinates,
x1 = x − e y1 = y − f
y1 + f = m(x1 + e ) + b1
or
y1 = mx1 + (me + b1 − f ) .
y1 = mx1 + b2 , where b2 = b1 + me − f
Therefore, we must add (me − f ) to b1 in equations (14). Then calculate the intersection
points, and add e to each calculated x-value to return to the original coordinate system.
Finally, calculate y for each x using y = mx + b1 (equation (15)), where b1 is the original
value.
In the special case, for non-rotated ellipses (α = 0) with sloping line (m≠0):
a = v 2 + h2m2 (11a)
b = 2h 2b1m (11b)
( 2
c = h 2 b1 − v 2 ) (11c)
And for the horizontal line (m = 0, b1 = y), the equations further reduce to the same as
equations (10) for non-rotated ellipses.
Bounding Box for a Rotated Ellipse
maximum y
20
minimum x 10
maximum x
-10
-20
minimum y
We would like to know the maximum and minimum horizontal and vertical values for a
rotated ellipse, i.e., the bounding box for the ellipse.
Assume, as before, that the ellipse is centered at the origin. The top line of the bounding
box, at y=ymax, may be thought of as a horizontal line which intersects the ellipse at a
single point. Equations (9) give the a, b and c values for solving the quadratic equation
for x on that line:
Note that a does not contain the variable y, therefore a is a constant for this condition.
b 2 − 4ac = 0 (17)
we can plug in the values for a, b and c from equations (9) and solve for y.
(2 y cosα sin α (v 2
− h2 )) 2
( (
− 4a y 2 v 2 sin 2 α + h 2 cos 2 α − h 2v 2 = 0) )
Separate out the y terms:
(2 cos α sin α (v 2
− h2 )) y
2 2
( )
− 4a v 2 sin 2 α + h 2 cos 2 α y 2 + 4ah 2v 2 = 0
(4 cos α sin α (v
2 2 2
) 2
(
− h 2 − 4a v 2 sin 2 α + h 2 cos 2 α y 2 + 4ah 2v 2 = 0 ))
− 4ah 2v 2
y2 =
( )
4 cos 2 α sin 2 α v 2 − h 2 − 4a v 2 sin 2 α + h 2 cos 2 α
2
( )
− ah 2v 2
y=± (18)
(
cos 2 α sin 2 α v 2 − h 2 − a v 2 sin 2 α + h 2 cos 2 α )
2
( )
where a = v 2 cos 2 α + h 2 sin 2 α (9a)
The positive and negative values give solutions for ymax and ymin. There is no need to find
the x-values corresponding to these y-values at this time. If the ellipse is not centered at
the origin, add the vertical offset of the ellipse center.
Turning now to the maximum and minimum horizontal values, go back to the
intersection of a vertical line with the ellipse. The right line of the bounding box, at
x=xmax, may be thought of as a vertical line which intersects the ellipse at a single point.
Equations (12) give the a, b and c values for solving the quadratic equation for y on that
line:
a = v 2 sin 2 α + h 2 cos 2 α (12a)
b = 2 x cos α sin α v − h
2 2
( (12b) )
(
c = x v cos α + h sin α − h 2v 2
2 2 2 2 2
) (12c)
(2 x cosα sin α (v 2
− h2 )) 2
( ( )
− 4a x 2 v 2 cos 2 α + h 2 sin 2 α − h 2v 2 = 0 )
Separate out the x terms:
(2 cosα sin α (v 2
− h2 )) x2 2
( )
− 4a v 2 cos 2 α + h 2 sin 2 α x 2 + 4ah 2v 2 = 0
(4 cos α sin α (v
2 2 2
)
2
( ))
− h 2 − 4a v 2 cos 2 α + h 2 sin 2 α x 2 + 4ah 2v 2 = 0
− ah 2v 2
x2 =
( )
cos 2 α sin 2 α v 2 − h 2 − a v 2 cos 2 α + h 2 sin 2 α
2
( )
− ah 2v 2
x=± (19)
(
cos 2 α sin 2 α v 2 − h 2 − a v 2 cos 2 α + h 2 sin 2 α )
2
( )
where a = v 2 sin 2 α + h 2 cos 2 α (12a)
The positive and negative values give solutions for xmax and xmin. There is no need to find
the y-values corresponding to these x-values at this time. If the ellipse is not centered at
the origin, add the horizontal offset of the ellipse center.
Determining the Tangent to an Ellipse
Assume we have an ellipse with horizontal radius h and vertical radius v centered at (e, f).
We wish to determine the tangent to the ellipse at point (x1, y1).
40
(x1 , y 1 )
v
30
dy
dx
20
(e, f) h
10
-10 10 20 30 40 50 60 70 80
If this were a circle, the tangent would be negative inverse of the slope (dy/dx) of the
radial line, or –dx/dy. However, because the axes are not equal, we have to make them
equal by first multiplying dy/dx by h/v. Then take the negative inverse = (-dx/dy)(v/h).
This gives the slope if the ellipse were a circle, so we now have to multiply again by v/h
to re-proportion the circle back to an ellipse.
dx v 2 x − e v2
m1 = − × 2 =− 1 × (20)
dy h y1 − f h 2
y = m1 ( x − x1 ) + y1 (21)
Assume the ellipse in question is rotated by angle α, and we wish to find the tangent line
at (x2, y2) . First rotate the (x2, y2) point back to the corresponding point on a “normal” or
un-rotated ellipse with the equations
(x2 , y 2 )
40 (x 2a , y 2a )
v
30
(x1 , y1 )
α
20
(e, f) h
(x1a , y 1a )
10
-10 10 20 30 40 50 60 70 80 90
Now we can solve for the tangent line of the un-rotated ellipse, using the technique from
the previous section. Substituting x1 and y1 from equations (22) into equation (20),
m1 = −
(x2 − e)cos α + ( y2 − f )sin α × v 2 (23)
( y2 − f )cos α − (x2 − e)sin α h 2
which is the slope of the tangent to the un-rotated ellipse in terms of the rotated points
(x2, y2).
Since the ellipse was rotated, we must rotate the tangent line back to the angle of the
rotated ellipse. In order to rotate the line, we need more than one point on the line.
Now let us rotate (x1a, y1a) back to the rotated ellipse. Call this rotated point (x2a, y2a)
x2 a = ( x1a − e )cos α − ( y1a − f )sin α + e
= ( x1 + z − e )cos α − (m1 z + y1 − f )sin α + e (24a)
y2 a = ( y1a − f )cos α + ( x1a − e )sin α + f
= (m1 z + y1 − f )cos α + ( x1 + z − e )sin α + f (24b)
Now the slope m2 of the rotated tangent line at (x2, y2) can be expressed as dy2/dx2.
Now, using the expressions for (x2a, y2a) in equations (24) and the expressions for (x2, y2)
in equations (25), we can calculate
dx2 = x2 a − x2
= ( x1 + z − e − ( x1 − e ))cos α − (m1 z + y1 − f − ( y1 − f ))sin α + e − e
= z cos α − m1 z sin α
= z (cos α − m1 sin α )
dy2 = y2 a − y2
= (m1 z + y1 − f − ( y1 − f ))cos α + ( x1 + z − e − (x1 − e ))sin α + f − f
= m1 z cos α + z sin α
= z (m1 cos α + sin α )
In the case where the un-rotated slope m1 is infinite, we can still calculate the rotated
slope. In this case,
x1a = x1 (27a)
and let y1a = y1 + z , where z > 0. (27b)
dx2 = x2 a − x2
= ( x1 − e − ( x1 − e ))cos α − ( y1 + z − f − ( y1 − f ))sin α + e − e
= − z sin α
dy2 = y2 a − y2
= ( y1 + z − f − ( y1 − f ))cos α + ( x1 − e − ( x1 − e ))sin α + f − f
= z cos α
dy2 cos α
m2 = = , (29)
dx2 − sin α
which, incidentally, is the limit of equation (26) as m1 → ∞ .
Note In equation (29), m2 is infinite if sin α = 0, that is, rotating a line of infinite slope
by a multiple of 180 degrees (sin α = 0) results in another infinite slope.
Summary: The slope m2 of the tangent at a point (x2, y2) on an ellipse rotated by angle α
is determined by first calculating the tangent m1 on the un-rotated ellipse using equation
(23). Then, if m1 is infinite, calculate m2 using equation (29), otherwise use equation
(26).
y = m2 ( x − x2 ) + y2 (30)
Approximating a Segment of an Ellipse with a Bezier Curve
If you are drawing an elliptical arc, chord, or pie segment, it can be useful (and often
faster) to draw the segment as a cubic Bezier curve. This works for small segments, not
more than 90 degrees and optimally, less than 45 degrees. The smaller the segment, the
greater the accuracy. The larger you magnify a segment of an ellipse, the straighter the
curve appears.
It is assumed that you know the horizontal and vertical radii of the ellipse (h and v), the
center of the ellipse (e, f) and the rotation angle α, if the ellipse is rotated.
If the ellipse is rotated, we will first rotate it back to a “normal” un-rotated ellipse. If you
are starting with 2 points (xstart, ystart) and (xend, yend) on the rotated ellipse, rotate each of
them back using the formula
Next determine the start and end angles. To do that, we first define a function
y
arctan , if x > 0, y ≥ 0
x
y
π − arctan , if x < 0, y ≥ 0
−x
y
arctan 2( x, y ) = π + arctan , if x < 0, y < 0 , where the angle is in radians.
x
−y
2π − arctan , if x > 0, y < 0
x
π 2, if x = 0, y ≥ 0
3π 2, if x = 0, y < 0
This will result in a single value between 0 and 2π (0 and 360 degrees).
These start and end angles, βstart and βend, are the angles used in the formulae
x1 − e = h cos β
y1 − f = v sin β
defining the ellipse. They are not actual angles unless the ellipse is a circle. To find the
angles, use the arctan2 formula, above, as follows:
x1 − e y1 − f
β = arctan 2 ,
h v
To determine the Bezier control points, assume they will lie on lines tangent to the ellipse
at the start and end points. Determine the parameters for the tangent lines at both ends of
the segment, using the technique described in the previous section. Now calculate angles
of approximately 1/3 and 2/3 of the angle (βend -βstart ) and spanned by the segment.
Empirically, the values .32343333 and .67636667 work quite well. Adjust the angles if
the arc crosses 360 degrees.
Now imagine lines from the center of the ellipse at those angles and find their
intersection with the tangent lines. (Take into account the possibility that the tangent or
radial lines may have infinite slope.) These intersections are your Bezier control points.
40
(x end , yend )
35
v
Control Point 2
30
Control Point 1
25 (xstart , ystart )
20
(e, f) h
30 35 40 45 50 55 60 65 70 75 80
Now, if the ellipse is rotated, rotate the Bezier control points by the same angle, using the
formulae
x = ( x1 − e )cos α − ( y1 − f )sin α + e
y = ( y1 − f )cos α + ( x1 − e )sin α + f .
Now draw the Bezier curve from the (rotated) start point to the (rotated) end point using
the calculated (rotated) control points. The curve is shown in blue on the figure above.