Path: news.mathworks.com!not-for-mail
From: <HIDDEN>
Newsgroups: comp.soft-sys.matlab
Subject: Re: create polygon coordinates
Date: Sun, 20 Jan 2013 05:50:09 +0000 (UTC)
Organization: The MathWorks, Inc.
Lines: 41
Message-ID: <kdg0ig$6sa$1@newscl01ah.mathworks.com>
References: <kdenag$nth$1@newscl01ah.mathworks.com> <kdera0$71t$1@newscl01ah.mathworks.com> <kdfbfg$smd$1@newscl01ah.mathworks.com>
Reply-To: <HIDDEN>
NNTP-Posting-Host: www-03-blr.mathworks.com
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 8bit
X-Trace: newscl01ah.mathworks.com 1358661009 7050 172.30.248.48 (20 Jan 2013 05:50:09 GMT)
X-Complaints-To: news@mathworks.com
NNTP-Posting-Date: Sun, 20 Jan 2013 05:50:09 +0000 (UTC)
X-Newsreader: MATLAB Central Newsreader 1187260
Xref: news.mathworks.com comp.soft-sys.matlab:786892

"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