# How to find the center of an (scaled) ellipse?

This question is an extension of How to find the center of an ellipse?.

The solution there works well, but in Javascript the floating point calculations are not that accurate. The workaround is to "scale up" all the values so that many of the digits are before the decimal point. To do this I modified the algo mentioned below, but it is not working. It works if I set $multiplier$ to $1$. $multiplier$ is the factor which decides by how much I will be scaling up the values. In my tests I have set it to 300.

1. Given points $P_0$, $P_1$ on the ellipse of radii $r_x$ and $r_y$. The major-axis of the ellipse makes $\alpha$ angle on x-axis. I now need to find the centers ($C_1$, $C_2$) of this ellipse (there can be two ellipses).
2. I apply rotation transform of $-\alpha$.
3. I then scale it by $multiplier/r_x$ (along x-axis) and $multiplier/r_y$ (along y-axis).
4. The above transforms give a circle of radius $multiplier$.
5. Now I find the new values of $P_0$ and $P_1$ after all these transforms. Let's call them $R_0$ and $R_1$.
6. Both these points form a chord on the circle we got on step 4. The perpendicular bisector of that chord will pass through the circle's center. This is the point I seek.
7. Let $c$ represent half of the length of the chord. Let $s$ be the length of the perpendicular from circle's center to the chord's mid-point. So, the relation will be $s = \sqrt{multiplier^2 - c^2}$. For brevity, let's ignore the cases where $c>=multiplier$.
8. I now translate the chord's midpoint to origin by translating the circle by $[-(R_0+R_1)/2]$.
9. After this I rotate the circle by $\pi/2$. This will align $R_0$ with the center of the circle when we were in step 8.
10. Now I can get the circle's center if scale in such a way that $R_0$ coincides with that location. For this I need to scale chord's half-length from $c$ to $s$. For this I apply scaling transform of $s/c$.
11. Now let us call $S_0$ the new value of $R_0$.
12. When I "un-transform" $S_0$'s value then we get the ellipse's center. To "un-transform" it I use combined transform matrix till step 3 and find its inverse.

Note: I have left out the part to find the second ellipse's center.

-
"too many steps" + "not enough precision" = "loss of accuracy" – J. M. Aug 4 '11 at 17:14
Also: there are ways to evaluate expressions that are less numerically sound than others. Sometimes, what you think is an "optimization" may well be an inaccurate way of computing an intermediate quantity, whose error will eventually swamp your end result. – J. M. Aug 4 '11 at 17:16
I'm going to ask a side question here: what problem are you trying to solve? The specific data that you're working with (two points on the ellipse and an angle) seems so unusual that I'm wondering what its providence is, and whether there's an easier answer to your problem lurking somewhere in the background. – Steven Stadnicki Aug 4 '11 at 17:17
Also, as another note: how are you performing your rotation by &alpha;? That would seem to require either floating point itself (in which case, why bother scaling and doing the rest as integers?), or another scaling step that you're omitting here. Either way, it feels like while this problem is potentially solvable, the clunkiness suggests that the whole approach is off somewhere. – Steven Stadnicki Aug 4 '11 at 17:19
Particularly: if you look at whuber's solution in the answer to your previous question, he computes the quantity $\sqrt{1-c^2}$; that form would be inaccurate if $c\approx 1$. I'll try to think of a way to mitigate that... – J. M. Aug 4 '11 at 17:21