Drexel dragonThe Math ForumDonate to the Math Forum



Search All of the Math Forum:

Views expressed in these public forums are not endorsed by Drexel University or The Math Forum.


Math Forum » Discussions » Software » comp.soft-sys.matlab

Topic: create polygon coordinates
Replies: 5   Last Post: Jan 20, 2013 10:40 AM

Advanced Search

Back to Topic List Back to Topic List Jump to Tree View Jump to Tree View   Messages: [ Previous | Next ]
Roger Stafford

Posts: 5,888
Registered: 12/7/04
Re: create polygon coordinates
Posted: Jan 20, 2013 12:50 AM
  Click to see the message monospaced in plain text Plain Text   Click to reply to this topic Reply

"Jessica" wrote in message <kdfbfg$smd$1@newscl01ah.mathworks.com>...
> 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;
max(abs(z))
plot(x,y)
axis equal

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.)

Roger Stafford



Point your RSS reader here for a feed of the latest messages in this topic.

[Privacy Policy] [Terms of Use]

© Drexel University 1994-2014. All Rights Reserved.
The Math Forum is a research and educational enterprise of the Drexel University School of Education.