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.
- 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).
- I apply rotation transform of $-\alpha$.
- I then scale it by $multiplier/r_x$ (along x-axis) and $multiplier/r_y$ (along y-axis).
- The above transforms give a circle of radius $multiplier$.
- 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$.
- 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.
- 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$.
- I now translate the chord's midpoint to origin by translating the circle by $[-(R_0+R_1)/2]$.
- 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.
- 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$.
- Now let us call $S_0$ the new value of $R_0$.
- 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.
Math.sqrt((1-c)*(1+c))
(skip the scaling for now!) and report back. – J. M. ain't a mathematician Aug 04 '11 at 17:50