How to connect points in a dynamic figure

1 回表示 (過去 30 日間)
Yan Jiang
Yan Jiang 2016 年 1 月 20 日
回答済み: Daksh 2023 年 2 月 2 日
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 件のコメント
Walter Roberson
Walter Roberson 2016 年 1 月 20 日
Did you look at the existing routines from the File Exchange?

サインインしてコメントする。

回答 (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 Center および File ExchangeBar Plots についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by