How to draw a rectangle and placed node by clicking on them using MATLAB

3 views (last 30 days)
draw a rectangle and placed node by clicking on them using matlab

Answers (1)

Kartik Pontula
Kartik Pontula on 12 Jul 2023
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.

Categories

Find more on Graphics Object Properties in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!