Weirdly.net
Weirdly: adv. In a strikingly odd or unusual manner

The main purpose of this page is to serve as a gallery of work I've done. It also gives me a handy URL on which to post my résumé.

To quickly summarize the options on the left:

06-11-2013 A Better Ellipse

I have been bothered by a particular equation for a very long time. It is the commonly provided equation for an ellipse, and is usually defined as follows:

(x - x1)2
a
+
(y - y1)2
b
= 1

Where the center of the ellipse is located at the point (x1, y1), with its x radius equal to a and its y radius equal to b. This is all well and good as far as it goes; my dislike for it lies in the fact that the only ellipses it can be used for are those with a length that is parallel to either the x or y axis of the graph. If you want to plot one that lies on a different angle, then this will not work.

There is however another way to define an ellipse that is not subject to that restriction. It can be done using the relationship between an ellipse's border and its focal points: The sum of the distances between the focal points of an ellipse and any point on its border is a fixed constant. This means that if we take any two points - we'll call them (a, b) and (c, d), and set a fixed value for the sum of the two line segments' length (we'll call that e), then we can define any ellipse as follows:

√[(x - a)2 + (y - b)2] + √[(x - c)2 + (y - d)2] = e

That in itself is fairly simple. The real difficulty is in solving that equation for y, giving us a usable function to work with. This is a problem that I have tried on and off many times throughout the years, and inevitably failed. The trouble isn't in the math itself, but in the sheer volume of it. To solve this, all you need to do is get rid of the radicals by separating and squaring them, then expand the equation out to simple terms, regroup them, and then factor "y" out of the resulting equation. To get rid of the radicals though, you have to square some terms repeatedly, giving you a significant quantity of terms to manage.

I've probably tried to solve that about a dozen times over the years, and always gave up when doing the expansion. I finally cracked it this week when switching from working on paper to working on the computer. I initially expected it to add to the work, but found that once I got used to it, that wasn't the case. It also became much easier to re-check and make corrections, and having an infinite canvas to work on is great.

So after several more failed attempts over the past two weeks, I'm very happy to present this solution worked out from the basic equation given above.

We can begin with removing the radicals by separating and squaring them:

√[(x - a)2 + (y - b)2] + √[(x - c)2 + (y - d)2] = e

√[(x - a)2 + (y - b)2] = e - √[(x - c)2 + (y - d)2]

(x - a)2 + (y - b)2 = e2 - 2 e √[(x - c)2 + (y - d)2] + (x - c)2 + (y - d)2

(x - a)2 + (y - b)2 - e2 - (x - c)2 - (y - d)2 = - 2 e √[(x - c)2 + (y - d)2]

[(x - a)2 + (y - b)2 - e2 - (x - c)2 - (y - d)2]2 = 4 e2[(x - c)2 + (y - d)2]

With all of the radicals removed, we now expand.

( + x2 - 2 a x + a2 + y2 - 2 b y + b2 - e2 - x2 + 2 c x - c2 - y2 + 2 y d - d2 )2 = 4 e2 (x - c)2 - 4 e2 (y - d)2

( - 2 a x + a2 - 2 b y + b2 - e2 + 2 c x - c2 + 2 y d - d2 )2 = 4 e2 (x2 - 2 c x + c2) - 4 e2 (y2 - 2 d y + d2)

- 2 a x (- 2 a x + a2 - 2 b y + b2 - e2 + 2 c x - c2 + 2 d y - d2)
+ a2 (- 2 a x + a2 - 2 b y + b2 - e2 + 2 c x - c2 + 2 d y - d2)
- 2 b y (- 2 a x + a2 - 2 b y + b2 - e2 + 2 c x - c2 + 2 d y - d2)
+ b2 (- 2 a x + a2 - 2 b y + b2 - e2 + 2 c x - c2 + 2 d y - d2)
- e2 (- 2 a x + a2 - 2 b y + b2 - e2 + 2 c x - c2 + 2 d y - d2)
+ 2 c x (- 2 a x + a2 - 2 b y + b2 - e2 + 2 c x - c2 + 2 d y - d2)
- c2 (- 2 a x + a2 - 2 b y + b2 - e2 + 2 c x - c2 + 2 d y - d2)
+ 2 y d (- 2 a x + a2 - 2 b y + b2 - e2 + 2 c x - c2 + 2 d y - d2)
- d2 (- 2 a x + a2 - 2 b y + b2 - e2 + 2 c x - c2 + 2 d y - d2)
= 4 e2 (x2 - 2 c x + c2) + 4 e2 (y2 - 2 d y + d2)

+ 4 a2 x2 - 2 a3 x + 4 a b x y - 2 a b2 x + 2 a e2 x - 4 a c x2 + 2 a c2 x - 4 a d x y + 2 a d2 x
+ 4 a b x y - 2 a2 b y + 4 b2 y^y - 2 b3 y + 2 b e2 y - 4 b c x y + 2 b c2 y - 4 b d y2 + 2 b d2 y
- 4 a c x2 + 2 a2 c x - 4 b c x y + 2 b2 c x - 2 c e2 x + 4 c2 x2 - 2 c3 x + 4 c d x y - 2 c d2 x
- 4 a d x y + 2 a2 d y - 4 b d y2 + 2 b2 d y - 2 d e2 y + 4 c d x y - 2 c2 d y + 4 d2 y2 - 2 d3 y
- 2 a3 x + a4 - 2 a2 b y + a2 b2 - a2 e2 + 2 a2 c x - a2 c2 + 2 a2 d y - a2 d2
- 2 a b2 x + a2 b2 - 2 b3 y + b4 - b2 e2 + 2 b2 c x - b2 c2 + 2 b2 d y - b2 d2
+ 2 a c2 x - a2 c2 + 2 b c2 y - b2 c2 + c2 e2 - 2 c3 x + c4 - 2 c2 d y + c2 d2
+ 2 a d2 x - a2 d2 + 2 b d2 y - b2 d2 + d2 e2 - 2 c d2 x + c2 d2 - 2 d3 y + d4
+ 2 a e2 x - a2 e2 + 2 b e2 y - b2 e2 + e4 - 2 c e2 x + c2 e2 - 2 d e2 y + d2 e2
= 4 e2 x2 - 8 c e2 x + 4 c2 e2 + 4 e2 y2 - 8 d e2 y + 4 d2 e2

Now we can simplify by grouping like terms, factoring, etc. and separate y from the rest of the equation.

+ 4 a2 x2 - 8 a c x2 + 4 c2 x2 - 4 e2 x2
+ 4 b2 y2 - 8 b d y2 + 4 d2 y2 - 4 e2 y2
+ 4 a2 c x - 4 a3 x - 4 a b2 x + 4 a c2 x + 4 a d2 x + 4 a e2 x + 4 b2 c x - 4 c3 x - 4 c d2 x + 4 c e2 x
- 4 a2 b y + 4 a2 d y + 4 b2 d y - 4 b3 y + 4 b c2 y + 4 b d2 y + 4 b e2 y - 4 c2 d y - 4 d3 y + 4 d e2 y
+ 8 a b x y - 8 a d x y - 8 b c x y + 8 c d x y
+ a4 + b4 + c4 + d4 + e4 + 2 a2 b2 - 2 a2 c2 - 2 a2 d2 - 2 a2 e2 - 2 b2 c2 - 2 b2 d2 - 2 b2 e2 + 2 c2 d2 + 2 c2 e2 + 2 d2 e2
- 4 c2 e2 - 4 d2 e2
= 0

+ 4 x2 (a2 - 2 a c + c2 - e2)
+ 4 y2 (b2 - 2 b d + d2 - e2)
+ 4 x(a2 c - a3 - a b2 + a c2 + a d2 + a e2 + b2 c - c3 - c d2 + c e2)
- 4 y(a2 b - a2 d - b2 d + b3 - b c2 - b d2 - b e2 + c2 d + d3 - d e2)
+ 8 x y (a b - a d - b c + c d)
+ (a2 + b2 - c2 - d2 - e2)2
- 4 e2 ( c2 + d2)
= 0

+ 4 x2 ((a - c)2 - e2)
+ 4 y2 ((b - d)2 - e2)
+ 4 x((c - a) (a2 + b2 - c2 - d2 ) + e2 (a + c))
- 4 y((b - d) (a2 + b2 - c2 - d2 ) - e2 (b + d))
+ 8 x y (a - c)(b - d)
+ (a2 + b2 - c2 - d2 - e2)2 - 4 e2 ( c2 + d2)
= 0

+ 4 x2 ((a - c)2 - e2)
+ 4 y2 ((b - d)2 - e2)
+ 4 x((c - a) (a2 + b2 - c2 - d2 ) + e2 (a + c))
- 4 y((b - d) (a2 + b2 - c2 - d2 ) - e2 (b + d) - 2 x (a - c)(b - d))
+ (a2 + b2 - c2 - d2 - e2)2 - 4 e2 ( c2 + d2)
= 0

+ 4 x2 ((a - c)2 - e2)
+ 4 y2 ((b - d)2 - e2)
+ 4 x((c - a) (a2 + b2 - c2 - d2) + e2 (a + c))
- 4 y((b - d) (a2 + b2 - c2 - d2 - 2 x (a - c)) - e2 (b + d))
+ (a2 + b2 - c2 - d2 - e2)2 - 4 e2 ( c2 + d2)
= 0

We now have y separated in such a way that we can solve it as a quadratic equation, which we can do as follows:

Let:
u ≡ 4 ((b - d)2 - e2)
v ≡ - 4 ((b - d) (a2 + b2 - c2 - d2 - 2 x (a - c)) - e2 (b + d))
w ≡ 4 x2 ((a - c)2 - e2) + 4 x((c - a) (a2 + b2 - c2 - d2) + e2 (a + c)) + (a2 + b2 - c2 - d2 - e2)2 - 4 e2 ( c2 + d2)

Then:
u y2 + v y + w = 0

And so:
y =
-v ± √(v2 - 4 u w)

2 u


y =
4
(b - d)
a2 + b2 - c2 - d2
- 2 x (a - c)
- e2 (b + d)
±
 
  -4
(b - d)
a2 + b2 - c2 - d2
- 2 x (a - c)
- e2 (b + d)
 
2
- 4 [4 ((b - d)2 - e2)]
4 x2 ((a - c)2 - e2)
+ 4 x((c - a) (a2 + b2 - c2 - d2) + e2 (a + c))
+ (a2 + b2 - c2 - d2 - e2)2
- 4 e2 ( c2 + d2)
 
1/2

2 [4 ((b - d)2 - e2)]


y =
4
(b - d)
a2 + b2 - c2 - d2
- 2 x (a - c)
- e2 (b + d)
±
  16
(b - d)
a2 + b2 - c2 - d2
- 2 x (a - c)
- e2 (b + d)
2
- 16 ((b - d)2 - e2)
4 x2 ((a - c)2 - e2)
+ 4 x((c - a) (a2 + b2 - c2 - d2) + e2 (a + c))
+ (a2 + b2 - c2 - d2 - e2)2
- 4 e2 ( c2 + d2)
 
1/2

8 ((b - d)2 - e2)


y =
(b - d)
a2 + b2 - c2 - d2
- 2 x (a - c)
- e2 (b + d)
±
 
(b - d)
a2 + b2 - c2 - d2
- 2 x (a - c)
- e2 (b + d)
2
- ((b - d)2 - e2)
4 x2 ((a - c)2 - e2)
+ 4 x((c - a) (a2 + b2 - c2 - d2) + e2 (a + c))
+ (a2 + b2 - c2 - d2 - e2)2
- 4 e2 ( c2 + d2)
 
1/2

2 ((b - d)2 - e2)

Depending on your browser, that final equation may be somewhat difficult to read (thank-you underdeveloped HTML math notation). In lieu of that, here is the same final result as an image:
ellipse equation

I'm quite happy with this, but still need to work out an equation of similar complexity. What I ultimately want to do with this is to create a C function that can draw an ellipse given the focal points and the total distance ("e" in the solution above). In order to do that effectively, I'll need to know the minimum and maximum x values of the ellipse. One way to get this would be to take the contents of the radical, set them as being equal to zero, and solve for x. That then is my next objective; hopefully there will be an update soon.

More Blogs