how to get the contour/shape of a representation in 2D?

Hello! I have two sets of x and y data that I plot. Following the representation, the result is the following figure.
When I use the plot function for representation, to connect the dots, I get the following result:
But I would be interested in the points being connected so as to create a closed surface, such as a contour or a shape, like in the following image:
Is there a matlab function that could help me in this regard?
I also attach the data in .mat.
Thank you very much!
!! Edit:
I just find out about the matlab function boundary and i use it and obtain the results from the figure. But I am not satisfied with the result because I would like all the points to be connected in one representation. And here, two point are left behind.

 Akzeptierte Antwort

Star Strider
Star Strider am 16 Mär. 2022
Another approach —
LD1 = load('x.mat');
x = LD1.x;
LD2 = load('y.mat');
y = LD2.y;
cntrd = mean([x(:) y(:)]); % Centroid
angls = atan2(y(:)-cntrd(2), x(:)-cntrd(1)); % Angles Of Each Point W.R.T. Centroid
rads = hypot(y(:)-cntrd(2), x(:)-cntrd(1)); % Distances To Each Point From Centroid
angrad = sortrows([angls rads],1); % Sort Angles & Radii By Angles To Produce Smooth Boundary
angrad = [angrad; angrad(1,:)]; % Close Boundary
xc = (angrad(:,2).*cos(angrad(:,1)))+cntrd(1); % Boundary X-Coordinates
yc = (angrad(:,2).*sin(angrad(:,1)))+cntrd(2); % Boundary Y-Coordinates
figure
scatter(x, y, 10, 'k', 'filled') % Plot Points
hold on
plot(xc, yc, '-r') % Plot Connecting Lines
hold off
grid
axis equal
This should be reasonably robust, although if two points are essentially collinear with respect to their orientation from the centroid (have the same angle, so not all the angles are unique), it could have problems. The solution for that would be to ‘nudge’ the centroid so that all the angles are unique.
.

8 Kommentare

I like this solution best. It's what I would have done.
@Image Analyst — Thank you!
Thank you very much! You helped me a lot!
As always, my pleasure!
@Star Strider and if i want to extend this approach for a 3D representation? It will work? I tried to addapt it but I did not succed.
I attach the x,y,z coordinates of some points i want to display and after to conect them. Can you guide me or help me with the good approach?
Thank you!
I am not certain what result you want.
To connect the points as a volume, the boundary function is the best option.
To simply connect them with a closed line in 3D, a revision of my previous code works, sorting the ‘angls’ vector, and using that sort index to order the points:
LD1 = load('x_c.mat');
x_c = LD1.x_c;
LD2 = load('y_c.mat');
y_c = LD2.y_c;
LD3 = load('z_c.mat');
z_c = LD3.z_c;
c_mtx = [x_c y_c z_c]; % Combine Column Vectors Into Matrix
cntrd = mean(c_mtx); % Centroid
angls = atan2(c_mtx(:,2)-cntrd(2), c_mtx(:,1)-cntrd(1)); % Angles Of Each Point W.R.T. Centroid
[angssort,idx] = sort(angls); % Sort 'c_mtx'
c_mtxs = [c_mtx(idx,:); c_mtx(idx(1),:)]; % Close Curve
figure
plot3(c_mtxs(:,1), c_mtxs(:,2), c_mtxs(:,3))
grid on
view(145,5)
xlabel('x')
ylabel('y')
zlabel('z')
figure
plot3(c_mtxs(:,1), c_mtxs(:,2), c_mtxs(:,3), 'LineWidth',1.5)
hold on
stem3(c_mtxs(:,1), c_mtxs(:,2), c_mtxs(:,3), 'filled', 'MarkerSize',2.5)
patch([xlim flip(xlim)], [[1 1]*min(ylim) [1 1]*max(ylim)], zeros(4,1), [1 1 1]*0.8, 'FaceAlpha',0.5)
hold off
grid on
view(145,5)
xlabel('x')
ylabel('y')
zlabel('z')
The first figure —
Using view here rotates the camera angle to demonstrate that the curve is closed.
The second figure shows the relationships of the points in a bit more detail, and in relation to the ‘z’ axis plane.
This may not be robust to other sets of 3D points, however given the orientation of the points in this problem, it works and produces a closed curve.
.
@Star Strider thank you very much. It is clear now for me. You helped me a lot!
As always, my pleasure!

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (2)

Torsten
Torsten am 16 Mär. 2022

0 Stimmen

[k,~] = convhull(P);
plot(P(k,1),P(k,2))
with P being your points as an nx2 matrix.

2 Kommentare

Thank you for the answer, but I don't think it helps me much. It remains points that are not interconnected in the representation.
Torsten
Torsten am 16 Mär. 2022
Bearbeitet: Torsten am 16 Mär. 2022
If you order the points in x,y arrays in the sequel they should be connected by lines, MATLAB will plot correctly, e.g.
x = [1 2 4 1 ]
y = [3 6 8 3 ]
plot(x,y)

Melden Sie sich an, um zu kommentieren.

Point #8 is close to the previous one and you have to break it into 2 parts and combine them manually.
Of course, if the code is used for another combination of points, it does not work.
load('x.mat');
load('y.mat');
x1 = x(1:9); y1 = y(1:9); % Separate at point #8 & #9
x2 = x(8:end); y2 = y(8:end);
k1 = boundary(x1',y1');
k2 = boundary(x2',y2');
pgon1 = polyshape(x1(k1),y1(k1));
pgon2 = polyshape(x2(k2),y2(k2));
pgon = union(pgon1,pgon2); % Union 2 polygons
plot(pgon);
hold on
plot(x,y,'r*')

Kategorien

Mehr zu Contour Plots finden Sie in Hilfe-Center und File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by