Generate inner values of y for each x based on n

I have the following code:
clear all
clc
n = 4; % number of divisions on the boundary
x1 = linspace(0,2,n);
y1_x1 = sqrt(1-( (x1.^2)/4 ) );
y2_x1 = -sqrt(1-( (x1.^2)/4 ) );
x2 = linspace(-2,0,n);
y1_x2 = sqrt(1-( (x2.^2)/4 ) );
y2_x2 = -sqrt(1-( (x2.^2)/4 ) );
x = [x1 x1 x2 x2];
y = [y1_x1 y2_x1 y1_x2 y2_x2];
scatter(x,y)
Now, is there a way to generate the inner values of y with respect to x?
For example, as we can see in the plot, an ellipse is formed, but the points are on the boundaries, I want points within the ellipse as well, such that it uses the values of x and y already calculated and connects the y values to x like at x = 0, y = 1.0000,0.9428,0.7454,0 and at next x on both positive and negative side (x = +-0.6667), y = 0.9428,0.7454,0 so on and so forth till x = +-2
But most importantly, it all stays connected, so that all I change is the value of n and the inner points are generated.
Thanks

 Accepted Answer

clc; clear all ;
clear all
clc
n = 4; % number of divisions on the boundary
x1 = linspace(0,2,n);
y1_x1 = sqrt(1-( (x1.^2)/4 ) );
y2_x1 = -sqrt(1-( (x1.^2)/4 ) );
x2 = linspace(-2,0,n);
y1_x2 = sqrt(1-( (x2.^2)/4 ) );
y2_x2 = -sqrt(1-( (x2.^2)/4 ) );
x = [x1 x1 x2 x2];
y = [y1_x1 y2_x1 y1_x2 y2_x2];
scatter(x,y)
% Arrange ellipe points properly
xCenter = mean(x);
yCenter = mean(y);
angles = atan2d(y - yCenter, x - xCenter);
[sortedAngles, sortOrder] = sort(angles);
x = x(sortOrder);
y = y(sortOrder);
[X,Y] = meshgrid(linspace(min(x),max(x)),linspace(min(y),max(y))) ;
Z = zeros(size(X)) ;
idx = inpolygon(X,Y,x,y) ;
Z(idx) = 1 ;
pcolor(X,Y,Z)

4 Comments

I am not sure if this does it, because this code gives points inside and outside the ellipse, only points inside the ellipse are needed
You have it already.
clc; clear all ;
clear all
clc
n = 4; % number of divisions on the boundary
x1 = linspace(0,2,n);
y1_x1 = sqrt(1-( (x1.^2)/4 ) );
y2_x1 = -sqrt(1-( (x1.^2)/4 ) );
x2 = linspace(-2,0,n);
y1_x2 = sqrt(1-( (x2.^2)/4 ) );
y2_x2 = -sqrt(1-( (x2.^2)/4 ) );
x = [x1 x1 x2 x2];
y = [y1_x1 y2_x1 y1_x2 y2_x2];
% Arrange ellipe points properly
xCenter = mean(x);
yCenter = mean(y);
angles = atan2d(y - yCenter, x - xCenter);
[sortedAngles, sortOrder] = sort(angles);
x = x(sortOrder);
y = y(sortOrder);
[X,Y] = meshgrid(linspace(min(x),max(x)),linspace(min(y),max(y))) ;
Z = zeros(size(X)) ;
idx = inpolygon(X,Y,x,y) ;
Z(idx) = 1 ;
plot(X(idx),Y(idx),'.r')
Thanks a lot for the clarification
Is it possible to use the values of X and Y that you generated in a delaunay connection?
Also, is it possible to reduce or increase the points within the ellipse?

Sign in to comment.

More Answers (0)

Categories

Find more on Programming in Help Center and File Exchange

Products

Release

R2022b

Asked:

on 25 Nov 2022

Commented:

on 25 Nov 2022

Community Treasure Hunt

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

Start Hunting!