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

조회 수: 1 (최근 30일)
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 CenterFile Exchange에서 Graph and Network Algorithms에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by