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

4 vues (au cours des 30 derniers jours)
Sushree Patra
Sushree Patra le 31 Oct 2018
draw a rectangle and placed node by clicking on them using matlab

Réponses (1)

Kartik Pontula
Kartik Pontula le 12 Juil 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.

Catégories

En savoir plus sur Printing and Saving dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by