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

回答(1 个)

Kartik Pontula
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.

类别

Help CenterFile Exchange 中查找有关 Printing and Saving 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by