Date: Jan 19, 2013 9:00 PM
Author: Roger Stafford
Subject: Re: create polygon coordinates
"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