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

3 ビュー (過去 30 日間)
Sushree Patra
Sushree Patra 2018 年 10 月 31 日
回答済み: Kartik Pontula 2023 年 7 月 12 日
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 Center および File ExchangeGraphics Object Properties についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by