The Math Forum

Search All of the Math Forum:

Views expressed in these public forums are not endorsed by NCTM or The Math Forum.

Math Forum » Discussions » Software » comp.soft-sys.matlab

Notice: We are no longer accepting new posts, but the forums will continue to be readable.

Topic: create polygon coordinates
Replies: 5   Last Post: Jan 20, 2013 10:40 AM

Advanced Search

Back to Topic List Back to Topic List Jump to Tree View Jump to Tree View   Messages: [ Previous | Next ]
Roger Stafford

Posts: 5,929
Registered: 12/7/04
Re: create polygon coordinates
Posted: Jan 19, 2013 9:00 PM
  Click to see the message monospaced in plain text Plain Text   Click to reply to this topic Reply

"Jessica" wrote in message <kdfbfg$smd$>...
> "Roger Stafford" wrote in message <kdera0$71t$>...
> > 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

Point your RSS reader here for a feed of the latest messages in this topic.

[Privacy Policy] [Terms of Use]

© The Math Forum at NCTM 1994-2018. All Rights Reserved.