Need help modifying a piece of code that allows the user to draw lines
2 visualizzazioni (ultimi 30 giorni)
Mostra commenti meno recenti
Ashfaq Ahamed
il 18 Ago 2015
Commentato: Ashfaq Ahamed
il 20 Ago 2015
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.
0 Commenti
Risposta accettata
Purushottama Rao
il 18 Ago 2015
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
Più risposte (1)
Purushottama Rao
il 18 Ago 2015
Modificato: Purushottama Rao
il 18 Ago 2015
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
0 Commenti
Vedere anche
Categorie
Scopri di più su Graph and Network Algorithms in Help Center e File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!