How to connect points in a dynamic figure
이전 댓글 표시
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
2016년 1월 20일
Did you look at the existing routines from the File Exchange?
답변 (1개)
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!
카테고리
도움말 센터 및 File 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!
