35 views (last 30 days)

Show older comments

I have a number of points represented by an X a Y and an angle component, and I need to define an elliptical region around them, I currently do that like this:

x1 = 5;

y1 = 8;

radius = 8;

eccentricity = .7;

angle = pi/4;

x2 = x1 + radius * cos(angle);

y2 = y1 + radius * sin(angle);

a = 1/2*sqrt((x2-x1)^2+(y2-y1)^2);

b = a*sqrt(1-eccentricity^2);

and I plot it by finding some elements of that ellipse

t = linspace(0,2*pi);

X = a*cos(t);

Y = b*sin(t);

w = atan2(y2-y1,x2-x1);

x = (x1+x2)/2 + X*cos(w) - Y*sin(w);

y = (y1+y2)/2 + X*sin(w) + Y*cos(w);

here x1 and y1 represent the position of the red dot, and angle represents its angle. Then I define x2 and y2 to be another point at the other side of the ellipse to the desired direction in order to form the ellipse rotated as I want it to.

Is this a good way of doing this or is there a potentially better way of defining an ellipse around a point with a specific direction (note that the eccentricity and other variables are not all that important and we can set them as we wish)?

Another way that worked for me was finding the distance of each point in the ellipse from its center given an angle

a = 5;

e = .7;

theta = -pi : 0.05 : pi;

r = (a*(1-e^2)) ./ (1-e.*(cos(theta)));

x=r.*cos(theta);

y=r.*sin(theta);

so if we plot x and y here we will get an ellipse but it will not be rotated.

Secondly I need to find if some other point with its own x and y position is inside this particular ellipse and I would like to do that without having to use inpolygon that needs a big number of points in the ellipse to work. That is why I would prefer having a formula for the ellipse that passing it another point would return <1 for points inside that ellipse and >1 otherwise.

Jan
on 7 May 2018

Edited: Jan
on 7 May 2018

The most easy approach is defining the ellipse by the two foci F1 and F2. Then an ellipse is the set of points, which sum of distances to F1 and F2 is 2*a, where a is the semi-major axis length. Convert this to the polar form to draw the ellipse, but use this to determine points inside:

P = [x, y]; % A point in 2D

dist = norm(P - F1) + norm(P - F2);

inside = (dist <= 2*a);

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!