Need help modifying a piece of code that allows the user to draw lines

1 回表示 (過去 30 日間)
Ashfaq Ahamed
Ashfaq Ahamed 2015 年 8 月 18 日
コメント済み: Ashfaq Ahamed 2015 年 8 月 20 日
function draw_lines
% Click the left mouse button to define a point
% Drag the mouse to draw a line to the next point and
% left click again
% Right click the mouse to stop drawing
%
figure('WindowButtonDownFcn',@wbdcb)
ah = axes('DrawMode','fast');
axis ([-10 10 -10 10])
%open the file to save the coordinates into
fileID = fopen('coord.txt','w');
function wbdcb(src,evnt)
if strcmp(get(src,'SelectionType'),'normal')
[x,y,str] = disp_point(ah);
%write the coordinates into the file
A=[x,y];
fprintf(fileID,'%6.2f %6.2f\n',A);
hl = line('XData',x,'YData',y,'Marker','.');
text(x,y,str,'VerticalAlignment','bottom');drawnow
set(src,'WindowButtonMotionFcn',@wbmcb)
elseif strcmp(get(src,'SelectionType'),'alt')
set(src,'WindowButtonMotionFcn','')
[x,y,str] = disp_point(ah);
%write the coordinates into the file
A=[x,y];
fprintf(fileID,'%6.2f %6.2f\n',A);
text(x,y,str,'VerticalAlignment','bottom');drawnow
fclose(fileID);
end
function wbmcb(src,evnt)
[xn,yn,str] = disp_point(ah);
xdat = [x,xn];
ydat = [y,yn];
set(hl,'XData',xdat,'YData',ydat);
end
end
function [x,y,str] = disp_point(ah)
cp = get(ah,'CurrentPoint');
x = cp(1,1);y = cp(1,2);
str = ['(',num2str(x,'%0.3g'),', ',num2str(y,'%0.3g'),')'];
end
end
I need to modify this code so that the first line starts from the origin(0,0) and there onwards the user can click on the graph to draw lines. This is a recycled code that I found online so I am not entirely sure how it works. Any help would be much appreciated.

採用された回答

Purushottama Rao
Purushottama Rao 2015 年 8 月 18 日
If you want to see {0,0} before the mouse click, use this code
function draw_lines
% Click the left mouse button to define a point
% Drag the mouse to draw a line to the next point and
% left click again
% Right click the mouse to stop drawing
%
clc
figure('WindowButtonDownFcn',@wbdcb)
k=1;
ah = axes('DrawMode','fast');
axis ([-10 10 -10 10])%open the file to save the coordinates into
fileID = fopen('coord.txt','w');
hl = line('XData',0,'YData',0,'Marker','.');
str=['(',num2str(0),num2str(0),')'];
text(0,0,str,'VerticalAlignment','bottom');drawnow
function wbdcb(src,evnt)
if strcmp(get(src,'SelectionType'),'normal')
[x,y,str] = disp_point(ah); %write the coordinates into the file
A=[x,y];
fprintf(fileID,'%6.2f %6.2f\n',A); hl = line('XData',x,'YData',y,'Marker','.');
text(x,y,str,'VerticalAlignment','bottom');drawnow
set(src,'WindowButtonMotionFcn',@wbmcb)
elseif strcmp(get(src,'SelectionType'),'alt')
set(src,'WindowButtonMotionFcn','')
[x,y,str] = disp_point(ah); %write the coordinates into the file
A=[x,y];
fprintf(fileID,'%6.2f %6.2f\n',A); text(x,y,str,'VerticalAlignment','bottom');drawnow
fclose(fileID);
end
function wbmcb(src,evnt)
[xn,yn,str] = disp_point(ah);
xdat = [x,xn];
ydat = [y,yn];
set(hl,'XData',xdat,'YData',ydat);
end
end
function [x,y,str] = disp_point(ah)
if k==1
x=0;y=0;
k=0;
else
cp = get(ah,'CurrentPoint');
x = cp(1,1);y = cp(1,2);
end
str = ['(',num2str(x,'%0.3g'),', ',num2str(y,'%0.3g'),')'];
end
end
  1 件のコメント
Ashfaq Ahamed
Ashfaq Ahamed 2015 年 8 月 20 日
Thanks a lot Purushottama Rao, but this solves just one part of the problem. It displays the origin (0,0) but I want the first click to be at this point as well but without the user having to click on it manually. Any feedback would be much appreciated

サインインしてコメントする。

その他の回答 (1 件)

Purushottama Rao
Purushottama Rao 2015 年 8 月 18 日
編集済み: Purushottama Rao 2015 年 8 月 18 日
Assign a flag for the first click and reset it afterwards. Use the flag to set the coordinates to {0,0}. Hope this is what you are looking for
function draw_lines
% Click the left mouse button to define a point
% Drag the mouse to draw a line to the next point and
% left click again
% Right click the mouse to stop drawing
%
clc
figure('WindowButtonDownFcn',@wbdcb)
k=1; % Flag for origin
ah = axes('DrawMode','fast');
axis ([-10 10 -10 10])%open the file to save the coordinates into
fileID = fopen('coord.txt','w');
function wbdcb(src,evnt)
if strcmp(get(src,'SelectionType'),'normal')
[x,y,str] = disp_point(ah); %write the coordinates into the file
A=[x,y];
fprintf(fileID,'%6.2f %6.2f\n',A); hl = line('XData',x,'YData',y,'Marker','.');
text(x,y,str,'VerticalAlignment','bottom');drawnow
set(src,'WindowButtonMotionFcn',@wbmcb)
elseif strcmp(get(src,'SelectionType'),'alt')
set(src,'WindowButtonMotionFcn','')
[x,y,str] = disp_point(ah); %write the coordinates into the file
A=[x,y];
fprintf(fileID,'%6.2f %6.2f\n',A); text(x,y,str,'VerticalAlignment','bottom');drawnow
fclose(fileID);
end
function wbmcb(src,evnt)
[xn,yn,str] = disp_point(ah);
xdat = [x,xn];
ydat = [y,yn];
set(hl,'XData',xdat,'YData',ydat);
end
end
function [x,y,str] = disp_point(ah)
if k==1
x=0;y=0;
k=0; % Reset the flag for origin
else
cp = get(ah,'CurrentPoint');
x = cp(1,1);y = cp(1,2);
end
str = ['(',num2str(x,'%0.3g'),', ',num2str(y,'%0.3g'),')'];
end
end
if true
% code
end

カテゴリ

Help Center および File ExchangeGraph and Network Algorithms についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by