Part 1:
\begin{align}
x(t) &= \cos(\alpha) \cos(t) - h\sin(\alpha) \sin(t) \\
y(t) &= \sin(\alpha) \cos(t) + h\cos(\alpha) \sin(t)
\end{align}
I don't understand the question for part 2. Are the two point supposed to be on the ellipse? If so, then $h$ and $\alpha$ cannot be determined, because there can multiple ellipses at the origin containing the same two points. (Quick proof: draw a (non-circular) ellipse $E$ at the origin. Rotate it by 30 degrees to get another ellipse $E'$. Now look at $E \cap E'$. It will typically contain four points. If any two of these are called $A$ and $B$, then both $E$ and $E'$ are possible solutions to the "find $h$ and $\alpha$" problem, but the $\alpha$ values for $E$ and $E'$ differ by 30 degrees.
Post-comment remarks
The squared distance from $(x(t), y(t))$ to $(0,0)$ is
\begin{align}
d(t)^2
&= \left[\cos(\alpha) \cos(t) - h\sin(\alpha) \sin(t)\right]^2
+ \left[\sin(\alpha) \cos(t) + h\cos(\alpha) \sin(t)\right]^2 \\
&= \cos^2(\alpha) \cos^2(t) + h^2\sin^2(\alpha) \sin^2(t)
- 2h\cos(\alpha) \cos(t)\sin(\alpha) \sin(t)
+ \sin^2(\alpha) \cos^2(t) + h^2\cos^2(\alpha) \sin^2(t)
+ 2h\cos(\alpha) \cos(t)\sin(\alpha) \sin(t) \\
&= (\cos^2(\alpha) + \sin^2(\alpha)) \cos^2(t) + h^2(\sin^2(\alpha) + \sin^2(t)) \sin^2(t) \\
&= \cos^2(t) + h^2 \sin^2(t)
\end{align}
This varies from $1$ (at $t = 0$) to $h^2$ (at $t = \frac{\pi}{2}$). Hence one (semi-) axis has length $1$ and the other has length $h$. If $h < 1$, then the major (semi-)axis will have length $1$.
Note that at $t = 0$, we have
$$
\begin{align}
x(0) &= \cos(\alpha) \cos(0) - h\sin(\alpha) \sin(0) &= \cos(\alpha) \
y(0) &= \sin(\alpha) \cos(0) + h\cos(\alpha) \sin(0) &= \sin(\alpha)
\end{align}
which is an angle $\alpha$ from the positive $x$-axis, as required.
It's possible that you still don't believe me, so here's some Matlab code:
function y = ellipseTest()
hList = [0.1, 0.3, 0.7]; % three different eccentricities
alphaList = [20, 50, 130] * pi/180; % three different major-axis angles
t = linspace(0, 2*pi, 100); % sample values from 0 to 2pi
colorList = ['r', 'g', 'b'];
figure(gcf);
clf;
hold on
for i = 1:3
h = hList(i); alpha = alphaList(i); color = colorList(i);
x = cos(alpha) * cos(t) - h * sin(alpha) * sin(t);
y = sin(alpha) * cos(t) + h * cos(alpha) * sin(t);
plot(x, y, color)
end
hold off;
set(gca, 'DataAspectRatio', [1 1 1]);
figure(gcf);
and the resulting figure:
I find these pretty compelling.
Note that each pair of ellipses intersects in two points in the first quadrant, so if you picked one of these sets-of-two-points for your part 2 question, you'd find two different ellipses that fit them.
I assume that the variables x
and y
in the program are
arrays of numbers, because without that assumption it makes no sense
to fit an ellipse to them.
Then for each pair of values $(x_i,y_i)$
you compute $\theta_i = \mathrm{atan2}(y_i,x_i).$
This appears to be a fatal flaw, because the angle from
the origin to an $(x,y)$ point after the circle has been distorted
into an ellipse and translated away from the origin
has very little to do with the parameter $\theta$ that you need
in order to make the rest of your formulas work.
If you knew (or could reliably guess)
the angle $\theta$ corresponding to a point on the circle
before all the transformations that create the ellipse were applied,
then I think you could use calculations like the ones you showed
in order to convert the $(x,y)$ coordinates of the point on the ellipse
to which your original point was mapped--but on the other hand
if you know $\theta$ then you can plot the point on the circle without
even looking at the coordinates on the ellipse.
I suggest trying a simple example, such as the ellipse generated
by these parameterized equations:
\begin{align}
x &= 2\cos(\theta) + 5, \\
y &= \sin(\theta) + 2. \\
\end{align}
Plot these points for a few dozen equally-spaced of $\theta$
ranging from $0$ to $2\pi,$
confirm that you get an ellipse,
then give these sets of coordinates to your function
and see if it gives you back a circle.
I will be surprised if the result makes any sense at all.
To do the algorithm correctly, as noted in a comment you just have to
use the information from fitEllipse
to create a matrix that
scales the plane by a factor of $1/a$ parallel to the first axis of the
ellipse and by a factor of $1/b$ parallel to the second axis of the ellipse.
Translate all the points of the ellipse so that they are centered around
the origin, then use the matrix to scale them onto a circle.
Best Answer
"Inclination" here seems to be connected with the angle $i$ at which a cone is intercepted by a plane to produce the ellipse, but I'm not familiar with the term. At any rate the OP says (see Comment above) that the semi-minor axis can be expressed as $b = a \cos i$. In what follows, we assume this has been done.
Confusion arises from the subsequent wording that "the ellipse semi-major and semi-minor axes are not collinear with the X- and Y- axes but are inclined by the position angle." Notation for this position angle is not provided in the Question, so let's supply it here, and in doing so let's refer instead to it as the angle of rotation $\alpha$. Specifically, since the ellipse is "centered on the origin", we define $\alpha \in [-\pi/2,\pi/2]$ to be the angle between the semi-major axis and the positive X-axis.
More concretely: the endpoints of the major axis, centered as they are on the origin, are expressed by:
$$ \pm a (\cos \alpha, \sin \alpha) $$
The first step is then "unrotation" by this angle of rotation, a linear transformation on Cartesian coordinates expressed by the following matrix multiplication:
$$ \begin{bmatrix} \cos \alpha & \sin \alpha \\ -\sin \alpha & \cos \alpha \end{bmatrix} \begin{bmatrix} X \\ Y \end{bmatrix} $$
Clearly applying this transformation to the endpoints of the major axis (see above) will map them to $( \pm a, 0)$, thereby putting both major and minor axes in line with the $X$- and $Y$-coordinate axes, respectively.
In this "standard position" the ellipse can easily be transformed into a circle by scaling the X- or Y-coordinate or both. E.g. the mapping $(X,Y) \rightarrow (bX,aY)$ will produce a circle of radius $ab$ by scaling both coordinates. Combining these operations gives a matrix multiplication:
$$ \begin{bmatrix} X' \\ Y' \end{bmatrix} = \begin{bmatrix} b\cos \alpha & b\sin \alpha \\ -a\sin \alpha & a\cos \alpha \end{bmatrix} \begin{bmatrix} X \\ Y \end{bmatrix} $$ Optional: Further equal scaling of both coordinates by a nonzero factor will change the radius accordingly (but preserve circularity) and might be desirable if a particular radius is desired.
Finally this circle may itself be rotated around the origin through any angle, using a linear transformation similar to the "unrotation" described above. However this step is unnecessary if the goal was merely to map the original ellipse to a circle.