http://www.mathworks.com/matlabcentral/newsreader/view_thread/325905
MATLAB Central Newsreader  create polygon coordinates
Feed for thread: create polygon coordinates
enus
©19942014 by MathWorks, Inc.
webmaster@mathworks.com
MATLAB Central Newsreader
http://blogs.law.harvard.edu/tech/rss
60
MathWorks
http://www.mathworks.com/images/membrane_icon.gif

Sat, 19 Jan 2013 18:06:08 +0000
create polygon coordinates
http://www.mathworks.com/matlabcentral/newsreader/view_thread/325905#895774
Jessica
I have coordinates such as this:<br>
<br>
384 360 60 69<br>
<br>
that represent the left, top, width, and height of an ellipse. How can I turn these values into a list that specifies a polygon?<br>
<br>
I would like to use this polygon list to find out whether particular X and Y coordinates fall inside this ellipse:<br>
<br>
k_inside1= inpolygon(X_Coordinate, Y_Coordinate, X_Polygon3, Y_Polygon3);<br>
<br>
Thanks!

Sat, 19 Jan 2013 19:14:08 +0000
Re: create polygon coordinates
http://www.mathworks.com/matlabcentral/newsreader/view_thread/325905#895776
Roger Stafford
"Jessica" wrote in message <kdenag$nth$1@newscl01ah.mathworks.com>...<br>
> I have coordinates such as this:<br>
> <br>
> 384 360 60 69<br>
> <br>
> that represent the left, top, width, and height of an ellipse. How can I turn these values into a list that specifies a polygon?<br>
> <br>
> I would like to use this polygon list to find out whether particular X and Y coordinates fall inside this ellipse:<br>
> <br>
> k_inside1= inpolygon(X_Coordinate, Y_Coordinate, X_Polygon3, Y_Polygon3);<br>
> <br>
> Thanks!<br>
        <br>
It seems a shame to approximate an ellipse with a polygon to see if points lie inside it, when a direct test with the simple equation of the ellipse would accomplish the same thing with much better accuracy and simplicity.<br>
<br>
However, here are n vertices of a polygon approximating an ellipse. Let the ellipse be defined by the equation<br>
<br>
(xx0)^2/a^2 + (yy0)^2/b^2 = 1<br>
<br>
Then the vertices are given by:<br>
<br>
t = linspace(0,2*pi,n); % < You choose n<br>
x = a*cos(t)+x0;<br>
y = b*sin(t)+y0;<br>
<br>
Note: The spacing along the polygon is not uniform here. To do that would require the use of elliptic integrals.<br>
<br>
Roger Stafford

Sat, 19 Jan 2013 23:50:08 +0000
Re: create polygon coordinates
http://www.mathworks.com/matlabcentral/newsreader/view_thread/325905#895783
Jessica
"Roger Stafford" wrote in message <kdera0$71t$1@newscl01ah.mathworks.com>...<br>
> "Jessica" wrote in message <kdenag$nth$1@newscl01ah.mathworks.com>...<br>
> > I have coordinates such as this:<br>
> > <br>
> > 384 360 60 69<br>
> > <br>
> > that represent the left, top, width, and height of an ellipse. How can I turn these values into a list that specifies a polygon?<br>
> > <br>
> > I would like to use this polygon list to find out whether particular X and Y coordinates fall inside this ellipse:<br>
> > <br>
> > k_inside1= inpolygon(X_Coordinate, Y_Coordinate, X_Polygon3, Y_Polygon3);<br>
> > <br>
> > Thanks!<br>
>         <br>
> It seems a shame to approximate an ellipse with a polygon to see if points lie inside it, when a direct test with the simple equation of the ellipse would accomplish the same thing with much better accuracy and simplicity.<br>
> <br>
> However, here are n vertices of a polygon approximating an ellipse. Let the ellipse be defined by the equation<br>
> <br>
> (xx0)^2/a^2 + (yy0)^2/b^2 = 1<br>
> <br>
> Then the vertices are given by:<br>
> <br>
> t = linspace(0,2*pi,n); % < You choose n<br>
> x = a*cos(t)+x0;<br>
> y = b*sin(t)+y0;<br>
> <br>
> Note: The spacing along the polygon is not uniform here. To do that would require the use of elliptic integrals.<br>
> <br>
> Roger Stafford<br>
<br>
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?

Sun, 20 Jan 2013 02:00:09 +0000
Re: create polygon coordinates
http://www.mathworks.com/matlabcentral/newsreader/view_thread/325905#895788
Roger Stafford
"Jessica" wrote in message <kdfbfg$smd$1@newscl01ah.mathworks.com>...<br>
> "Roger Stafford" wrote in message <kdera0$71t$1@newscl01ah.mathworks.com>...<br>
> > It seems a shame to approximate an ellipse with a polygon to see if points lie inside it, when a direct test with the simple equation of the ellipse would accomplish the same thing with much better accuracy and simplicity.<br>
> ......<br>
> 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?<br>
        <br>
The answer to that depends on your method of defining the ellipse. (I could not quite understand the method you first mentioned: "the left, top, width, and height of an ellipse" with "384 360 60 69". The first two quantities ought to be pairs of x,y coordinates.)<br>
<br>
If your ellipse is aligned with the x and y axes and you have the coordinates of its leftmost point and its topmost point, (x1,y1) and (x2,y2), respectively, then its center would be at (x2,y1) and its equation would be<br>
<br>
(xx2)^2/(x2x1)^2 + (yy1)^2/(y2y1)^2  1 = 0<br>
<br>
If you have for example two vectors x and y with paired coordinates to be tested for lying inside the ellipse, the test would then be:<br>
<br>
test = (((xx2).^2/(x2x1)^2 + (yy1).^2/(y2y1)^2) < 1);<br>
<br>
Each element of 'test' will be true or false if the corresponding x,y pair is or is not inside the ellipse, respectively.<br>
<br>
If you have defined the ellipse in terms of its center (x0,y0), and its two semimajor and semiminor axes, a and b, and it is again aligned with the x and y axes, the test would be:<br>
<br>
test = (((xx0).^2/a^2 + (yy0).^2/b^2  1) < 0);<br>
<br>
If the ellipse were defined in terms of some quadratic expression such as<br>
<br>
A*x^2 + B*x*y + C*y^2 + D*x + E*y + F = 0<br>
<br>
and if A > 0, C > 0, B^2 < 4*A*C (and if F were not too large) then the test would be the obvious<br>
<br>
test = ((A*x.^2 + B*x.*y + C*y.^2 + D*x + E*y + F) < 0);<br>
<br>
Roger Stafford

Sun, 20 Jan 2013 05:50:09 +0000
Re: create polygon coordinates
http://www.mathworks.com/matlabcentral/newsreader/view_thread/325905#895795
Roger Stafford
"Jessica" wrote in message <kdfbfg$smd$1@newscl01ah.mathworks.com>...<br>
> 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?<br>
         <br>
Just in case you decide to use the general expression<br>
<br>
A*x^2+B*x*y+C*y^2+D*x+E*y+F = 0<br>
<br>
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.<br>
<br>
% Calculate various useful parameters<br>
x0 = (B*E2*C*D)/(4*A*CB^2);<br>
y0 = (B*D2*A*E)/(4*A*CB^2);<br>
f = (A*E^2B*D*E+C*D^2)/(4*A*CB^2)F;<br>
d = sqrt((AC)^2+B^2);<br>
a = sqrt(2*f/(A+C+d));<br>
b = sqrt(2*f/(A+Cd));<br>
t2 = 1/2*atan2(B,AC);<br>
s = sin(t2); as = a*s; bs = b*s;<br>
c = cos(t2); ac = a*c; bc = b*c;<br>
<br>
% Generate the ellipse<br>
t = linspace(0,2*pi,n); % < You choose n<br>
x = x0 + ac*cos(t)  bs*sin(t);<br>
y = y0 + as*cos(t) + bc*sin(t);<br>
<br>
% Test it against the original expression and plot it<br>
z = A*x.^2+B*x.*y+C*y.^2+D*x+E*y+F;<br>
max(abs(z))<br>
plot(x,y)<br>
axis equal<br>
<br>
In order to ensure a valid ellipse with the above expression, the following inequalities for its coefficients have been assumed:<br>
<br>
A > 0,<br>
C > 0,<br>
4*A*C > B^2, and<br>
A*E^2B*D*E+C*D^2 > F*(4*A*CB^2).<br>
<br>
The third of these makes it an ellipse, as opposed to a parabola or hyperbola. The fourth prevents it from being a degenerate singlepoint ellipse or nonexistent. (Also if they are not true, some of the above square roots produce imaginary values.)<br>
<br>
Roger Stafford

Sun, 20 Jan 2013 15:40:08 +0000
Re: create polygon coordinates
http://www.mathworks.com/matlabcentral/newsreader/view_thread/325905#895826
Jessica
"Roger Stafford" wrote in message <kdg0ig$6sa$1@newscl01ah.mathworks.com>...<br>
> "Jessica" wrote in message <kdfbfg$smd$1@newscl01ah.mathworks.com>...<br>
> > 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?<br>
>          <br>
> Just in case you decide to use the general expression<br>
> <br>
> A*x^2+B*x*y+C*y^2+D*x+E*y+F = 0<br>
> <br>
> 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.<br>
> <br>
> % Calculate various useful parameters<br>
> x0 = (B*E2*C*D)/(4*A*CB^2);<br>
> y0 = (B*D2*A*E)/(4*A*CB^2);<br>
> f = (A*E^2B*D*E+C*D^2)/(4*A*CB^2)F;<br>
> d = sqrt((AC)^2+B^2);<br>
> a = sqrt(2*f/(A+C+d));<br>
> b = sqrt(2*f/(A+Cd));<br>
> t2 = 1/2*atan2(B,AC);<br>
> s = sin(t2); as = a*s; bs = b*s;<br>
> c = cos(t2); ac = a*c; bc = b*c;<br>
> <br>
> % Generate the ellipse<br>
> t = linspace(0,2*pi,n); % < You choose n<br>
> x = x0 + ac*cos(t)  bs*sin(t);<br>
> y = y0 + as*cos(t) + bc*sin(t);<br>
> <br>
> % Test it against the original expression and plot it<br>
> z = A*x.^2+B*x.*y+C*y.^2+D*x+E*y+F;<br>
> max(abs(z))<br>
> plot(x,y)<br>
> axis equal<br>
> <br>
> In order to ensure a valid ellipse with the above expression, the following inequalities for its coefficients have been assumed:<br>
> <br>
> A > 0,<br>
> C > 0,<br>
> 4*A*C > B^2, and<br>
> A*E^2B*D*E+C*D^2 > F*(4*A*CB^2).<br>
> <br>
> The third of these makes it an ellipse, as opposed to a parabola or hyperbola. The fourth prevents it from being a degenerate singlepoint ellipse or nonexistent. (Also if they are not true, some of the above square roots produce imaginary values.)<br>
> <br>
> Roger Stafford<br>
<br>
Thank you very much for all of these suggestions!