Date: Jan 20, 2013 12:50 AM
Author: Roger Stafford
Subject: Re: create polygon coordinates
"Jessica" wrote in message <firstname.lastname@example.org>...
> Thanks for the tip! Do you mind explaining how I could test whether a coordinate falls within an ellipse using just the equation of an ellipse and not converting it to polygon coordinates?
- - - - - - - - - -
Just in case you decide to use the general expression
A*x^2+B*x*y+C*y^2+D*x+E*y+F = 0
for defining an ellipse, you can use the following code to generate n points along it. For B not equal to zero this is an ellipse whose major and minor axes are not aligned with the x and y axes.
% Calculate various useful parameters
x0 = (B*E-2*C*D)/(4*A*C-B^2);
y0 = (B*D-2*A*E)/(4*A*C-B^2);
f = (A*E^2-B*D*E+C*D^2)/(4*A*C-B^2)-F;
d = sqrt((A-C)^2+B^2);
a = sqrt(2*f/(A+C+d));
b = sqrt(2*f/(A+C-d));
t2 = 1/2*atan2(B,A-C);
s = sin(t2); as = a*s; bs = b*s;
c = cos(t2); ac = a*c; bc = b*c;
% Generate the ellipse
t = linspace(0,2*pi,n); % <-- You choose n
x = x0 + ac*cos(t) - bs*sin(t);
y = y0 + as*cos(t) + bc*sin(t);
% Test it against the original expression and plot it
z = A*x.^2+B*x.*y+C*y.^2+D*x+E*y+F;
In order to ensure a valid ellipse with the above expression, the following inequalities for its coefficients have been assumed:
A > 0,
C > 0,
4*A*C > B^2, and
A*E^2-B*D*E+C*D^2 > F*(4*A*C-B^2).
The third of these makes it an ellipse, as opposed to a parabola or hyperbola. The fourth prevents it from being a degenerate single-point ellipse or non-existent. (Also if they are not true, some of the above square roots produce imaginary values.)