How to draw a rectangle and placed node by clicking on them using MATLAB
조회 수: 4 (최근 30일)
이전 댓글 표시
draw a rectangle and placed node by clicking on them using matlab
댓글 수: 0
답변 (1개)
Kartik Pontula
2023년 7월 12일
From what I understand, you would like to draw a rectangle and place nodes by clicking on them. This code accomplishes that task:
% Create a figure window
figure;
% Initialize an empty array to store the nodes
nodes = [];
% Display instructions
disp('Click on the figure to place nodes. Press the Enter key after you are finished.');
% Loop until the user presses Enter
while true
% Wait for a mouse click
[x, y, button] = ginput(1);
% Check if the user pressed ENTER
if isempty(button)
break;
end
% Store the clicked coordinates as a node
nodes = [nodes; x, y];
% Plot the node as a red circle
hold on;
plot(x, y, 'ro');
end
% Draw a rectangle using the nodes
if size(nodes, 1) == 2
rectangle('Position', [nodes(1, 1), nodes(1, 2), nodes(2, 1) - nodes(1, 1), nodes(2, 2) - nodes(1, 2)], 'LineWidth', 2);
end
When you run this code, a figure window will appear, which you can click on to place nodes. Press the Enter key after placing all nodes. The code should draw a rectangle using the first two nodes you clicked.
Let me know if this solves your issue or if you encounter any problems.
댓글 수: 0
참고 항목
카테고리
Help Center 및 File Exchange에서 Printing and Saving에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!