How to connect points in a dynamic figure

2 次查看(过去 30 天)
Hi,
I am working on a project that dynamically scans and plots new points to a chart. I need to connect the points that fit specific criteria.
E.g, when the distance between any pair of points is less than or equal to 2, the two points are connected. Now points (0,0) and (0,4) are plotted already. If the next scanned point is (2,0), how shall I connect the point with the other two?
Thanks in advance! :)

回答(1 个)

Daksh
Daksh 2023-2-2
I understand that you wish to connect pairs of dynamically added points with a line to a given plot figure if the Euclidean distance between the added point and any existing points is <=2.
Kindly refer to the code below:
% plotting the original graph
x=[0 0; 0 4];
hold on
for i=1:length(x)
plot(x(i,1),x(i,2), 'or')
end
Now let's assume we dynamically create a new point (0,2) and store it to variable 'y' in workspace, and now we want to connect point y to all existing points in the graph with pairwise Euclidean distance <=2. This is how it can be done:
% New point added
y=[0,2];
x1=y(1);
y1=y(2);
plot(x1,y1, 'ob')
for i=1:length(x)
x2=x(i,1);
y2=x(i,2);
if(sqrt((x1-x2)^2 + (y1-y2)^2) <=2)
plot([x1,x2], [y1,y2], 'b-', 'LineWidth', 2);
end
x(length(x)+1,1)=x1;
x(length(x)+1,2)=y1;
end
hold off
Hope it helps!

类别

Help CenterFile Exchange 中查找有关 2-D and 3-D Plots 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by