Search All of the Math Forum:
Views expressed in these public forums are not endorsed by
NCTM or The Math Forum.



Re: create polygon coordinates
Posted:
Jan 19, 2013 9:00 PM


"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
(xx2)^2/(x2x1)^2 + (yy1)^2/(y2y1)^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 = (((xx2).^2/(x2x1)^2 + (yy1).^2/(y2y1)^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 semimajor and semiminor axes, a and b, and it is again aligned with the x and y axes, the test would be:
test = (((xx0).^2/a^2 + (yy0).^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



