Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

Thread Subject:
create polygon coordinates

Subject: create polygon coordinates

From: Jessica

Date: 19 Jan, 2013 18:06:08

Message: 1 of 6

I have coordinates such as this:

384 360 60 69

that represent the left, top, width, and height of an ellipse. How can I turn these values into a list that specifies a polygon?

I would like to use this polygon list to find out whether particular X and Y coordinates fall inside this ellipse:

k_inside1= inpolygon(X_Coordinate, Y_Coordinate, X_Polygon3, Y_Polygon3);

Thanks!

Subject: create polygon coordinates

From: Roger Stafford

Date: 19 Jan, 2013 19:14:08

Message: 2 of 6

"Jessica" wrote in message <kdenag$nth$1@newscl01ah.mathworks.com>...
> I have coordinates such as this:
>
> 384 360 60 69
>
> that represent the left, top, width, and height of an ellipse. How can I turn these values into a list that specifies a polygon?
>
> I would like to use this polygon list to find out whether particular X and Y coordinates fall inside this ellipse:
>
> k_inside1= inpolygon(X_Coordinate, Y_Coordinate, X_Polygon3, Y_Polygon3);
>
> Thanks!
- - - - - - - - -
  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.

  However, here are n vertices of a polygon approximating an ellipse. Let the ellipse be defined by the equation

 (x-x0)^2/a^2 + (y-y0)^2/b^2 = 1

Then the vertices are given by:

 t = linspace(0,2*pi,n); % <-- You choose n
 x = a*cos(t)+x0;
 y = b*sin(t)+y0;

  Note: The spacing along the polygon is not uniform here. To do that would require the use of elliptic integrals.

Roger Stafford

Subject: create polygon coordinates

From: Jessica

Date: 19 Jan, 2013 23:50:08

Message: 3 of 6

"Roger Stafford" wrote in message <kdera0$71t$1@newscl01ah.mathworks.com>...
> "Jessica" wrote in message <kdenag$nth$1@newscl01ah.mathworks.com>...
> > I have coordinates such as this:
> >
> > 384 360 60 69
> >
> > that represent the left, top, width, and height of an ellipse. How can I turn these values into a list that specifies a polygon?
> >
> > I would like to use this polygon list to find out whether particular X and Y coordinates fall inside this ellipse:
> >
> > k_inside1= inpolygon(X_Coordinate, Y_Coordinate, X_Polygon3, Y_Polygon3);
> >
> > Thanks!
> - - - - - - - - -
> 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.
>
> However, here are n vertices of a polygon approximating an ellipse. Let the ellipse be defined by the equation
>
> (x-x0)^2/a^2 + (y-y0)^2/b^2 = 1
>
> Then the vertices are given by:
>
> t = linspace(0,2*pi,n); % <-- You choose n
> x = a*cos(t)+x0;
> y = b*sin(t)+y0;
>
> Note: The spacing along the polygon is not uniform here. To do that would require the use of elliptic integrals.
>
> Roger Stafford

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?

Subject: create polygon coordinates

From: Roger Stafford

Date: 20 Jan, 2013 02:00:09

Message: 4 of 6

"Jessica" wrote in message <kdfbfg$smd$1@newscl01ah.mathworks.com>...
> "Roger Stafford" wrote in message <kdera0$71t$1@newscl01ah.mathworks.com>...
> > 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.
> ......
> 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?
- - - - - - - - -
  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.)

  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

 (x-x2)^2/(x2-x1)^2 + (y-y1)^2/(y2-y1)^2 - 1 = 0

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:

 test = (((x-x2).^2/(x2-x1)^2 + (y-y1).^2/(y2-y1)^2) < 1);

Each element of 'test' will be true or false if the corresponding x,y pair is or is not inside the ellipse, respectively.

  If you have defined the ellipse in terms of its center (x0,y0), and its two semi-major and semi-minor axes, a and b, and it is again aligned with the x and y axes, the test would be:

 test = (((x-x0).^2/a^2 + (y-y0).^2/b^2 - 1) < 0);

  If the ellipse were defined in terms of some quadratic expression such as

 A*x^2 + B*x*y + C*y^2 + D*x + E*y + F = 0

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

 test = ((A*x.^2 + B*x.*y + C*y.^2 + D*x + E*y + F) < 0);

Roger Stafford

Subject: create polygon coordinates

From: Roger Stafford

Date: 20 Jan, 2013 05:50:09

Message: 5 of 6

"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

Subject: create polygon coordinates

From: Jessica

Date: 20 Jan, 2013 15:40:08

Message: 6 of 6

"Roger Stafford" wrote in message <kdg0ig$6sa$1@newscl01ah.mathworks.com>...
> "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

Thank you very much for all of these suggestions!

Tags for this Thread

No tags are associated with this thread.

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

Contact us