필터 지우기
필터 지우기

How can I find all points inside a ellipse

조회 수: 36 (최근 30일)
azdoud youssef
azdoud youssef 2017년 12월 29일
편집: Deepu Kumar 2021년 7월 30일
I have the bounds of an ellipse in x- and y-coordinates. My end goal is to get a matrix of all x-values and a corresponding matrix of all y-values for the points in/out that ellipse. If possible, I would also like to color in the specified ellipse on a 2d plot. Does anyone know which commands/functions I can use to do this? Most of what I have seen simply confirms whether input values lie in/out an ellipse. Thank you!
such that a sample of the ellipse is represented as :
xCenter = 15;
yCenter = 10;
xRadius = 1.5;
yRadius = 80;
theta = 0 : 0.01 : 2*pi;
x = xRadius * cos(theta) + xCenter;
y = yRadius * sin(theta) + yCenter;
plot(x, y, 'LineWidth', 3);
  댓글 수: 1
Walter Roberson
Walter Roberson 2017년 12월 29일
There are an infinite number of points inside (or on) an ellipse. Even if you confine yourself to discrete x values, there would be an infinite number of y values, corresponding to the infinite density of the chord. To do anything useful, you need to either work with a symbolic formula or else discretize both x and y.

댓글을 달려면 로그인하십시오.

채택된 답변

Star Strider
Star Strider 2017년 12월 29일
‘My end goal is to get a matrix of all x-values and a corresponding matrix of all y-values for the points in/out that ellipse.’
There would be an infinite number of them, since you have defined an infinite 2D space. Your computer (or any computer) likely cannot handle it.
‘If possible, I would also like to color in the specified ellipse on a 2d plot’
That’s more tractable. See the patch (link) and related functions (such as area and fill, linked to at the end of that page) to create your plot. Remember to specify axis equal to avoid distorting your ellipse plot.
  댓글 수: 2
azdoud youssef
azdoud youssef 2017년 12월 30일
sorry I did not mention that I'm talking about a specific number of points for example
% 100 point
xq = rand(1,100); % Random X-Coordinates
yq = rand(1,100); % Random X-Coordinates
Star Strider
Star Strider 2017년 12월 30일
If you want to know the points inside and outside the ellipse, and there are a specific number of them, use the inpolygon (link) function. I know that you mentioned that you apparently do not want to use it, however it is the most efficient option.
Otherwise, if you want the area of the ellipse, you can use the trapz function twice (once in each dimension), or create an anonymous function representation for your ellipse and use integral2, or for a vector valued function, integral once in each dimension.

댓글을 달려면 로그인하십시오.

추가 답변 (2개)

azdoud youssef
azdoud youssef 2017년 12월 30일
clear ,close all;
xCenter = 15;
yCenter = 10;
xRadius = 3.5;
yRadius = 8;
theta = 0 : 0.01 : 2*pi;
x = xRadius * cos(theta) + xCenter;
y = yRadius * sin(theta) + yCenter;
xq=rand(1,3000)*100;
yq=rand(1,3000)*100;
in = inpolygon(xq,yq,x,y);
hold on
plot(xq(in), yq(in), 'ro');
plot(xq(~in), yq(~in), 'b*');
plot(x, y, 'LineWidth', 1);
axis square;
xlim([0 20]);
ylim([0 20]);
grid on;
% Draw lines along the axes
hold on;
line([xCenter, xCenter], [yCenter - yRadius, yCenter + yRadius], ...
'LineWidth', 4, 'Color', [1,0,0]);
line([xCenter - xRadius, xCenter + xRadius], [yCenter, yCenter], ...
'LineWidth', 4, 'Color', [1,0,0]);
% Enlarge figure to full screen.
set(gcf, 'Units', 'Normalized', 'OuterPosition', [0 0 1 1]);
  댓글 수: 2
Star Strider
Star Strider 2017년 12월 30일
That looks like it is doing what you want it to.
Waqar Khan
Waqar Khan 2021년 3월 18일
@azdoud youssef how did you get the values from ellipse, Could you please share the code or give me suggestions.

댓글을 달려면 로그인하십시오.


Deepu Kumar
Deepu Kumar 2021년 7월 30일
편집: Deepu Kumar 2021년 7월 30일
I think this is what you were asking.
In place of xCenter and yCenter, you can choose what ever the values that you want.
Similarly, the limits of xRadius and yRadius is of your choice.
xCenter = 0;
yCenter = 0;
xRadius = 1.41631:-(1.41631)/100:0;
yRadius = 0.53:-(0.53)/100:0;
theta = 0 : (2*pi)/100 : 2*pi;
for i=1:length(xRadius)
xx=xRadius(i);
yy=yRadius(i);
for j=1:length(yRadius)
x(i,j) = xx * cos(theta(j)) + xCenter;
y(i,j) = yy * sin(theta(j)) + yCenter;
end
end
plot(x, y, 'LineWidth', 3);

카테고리

Help CenterFile Exchange에서 Numerical Integration and Differentiation에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by