I'm working on converting data that is represented as an ellipse into a unit circle. I currently have a least squares implementation of obtaining the offset xo and yo, angle, and major and minor axis as shown here.
I'm working on some equations for then converting that data back into a unit circle. Here is my code
def ellipse_to_unit_circle(x,y,gr=0):
a = fitEllipse(x[gr:-gr],y[gr:-gr])
center = ellipse_center(a)
phi = ellipse_angle_of_rotation(a)
axes = ellipse_axis_length(a)
angles = np.arctan2(y,x)
phi2 = ellipse_angle_of_rotation2(a)
print(center)
print(axes)
denom_x = (-axes[0]*np.cos(angles)*np.cos(phi) +
axes[1]*np.sin(angles)*np.sin(phi))
denom_y = (axes[0]*np.cos(angles)*np.sin(phi) +
axes[1]*np.sin(angles)*np.cos(phi))
xx = (x-center[0])*np.cos(angles)/denom_x
yy = (y-center[1])*np.sin(angles)/denom_y
return [xx,yy]
I'll try my best to convert to a more readable form
for an ellipse with $$major = a$$ $$minor = b$$ $$offset= x_0,y_0$$ $$angle=\theta$$ $$angle rotated =\phi$$
$$ x = a\cos(\theta)\cos(\phi)+ b\sin(\theta)\sin(\phi) + x_o $$ $$ y = -a\cos(\theta)\sin(\phi)+ b\sin(\theta)\cos(\phi) + x_o $$ thus to find a circle I would convert to as follows
$$ \frac{x_{measured} - x_0}{a\cos(\theta)\cos(\phi) + b\sin(\theta)\sin(\phi)} = 1$$ $$ \frac{y_{measured} - y_0}{-a\cos(\theta)\sin(\phi) + b\sin(\theta)\cos(\phi)} = 1$$
then multiply by cos(theta) for x, sin(theta) for y to obtain the unit circle Additionally theta is calculated as arctan(y/x)
Here is the code to my example and a picture
if __name__ == '__main__':
arc = 2
R = np.arange(0,arc*np.pi, 0.01)
xr = 2
yr = 9
x_offset = 2 + 1*np.random.rand(len(R))
y_offset = 4 + 1*np.random.rand(len(R))
phi_offset =0 #np.pi/4
x = xr*np.cos(R)
y = yr*np.sin(R)
x = x*np.cos(phi_offset)+y*np.sin(phi_offset) + x_offset
y = -x*np.sin(phi_offset)+y*np.cos(phi_offset) + y_offset
xxx,yyy = ellipse_to_unit_circle(x,y)
from pylab import *
plot(x,y, color = 'purple', label='initial set')
plot(xxx,yyy, color = 'green', label='converted to unit circle')
legend(loc='upper left')
show()
First is there some problem mathematically from this conversion?
Second this equation creates singularities, is there an elegant way to deal with them?