function drawbspline(action) buttonh = findobj(gcbf,'Tag','Curvebutton'); switch(action) case 'draw' makecontrol('stop'); set(buttonh,'String','Erase curve') set(buttonh,'Callback','drawbspline(''erase'')') polyhandle = findobj(gcbf,'Tag','Bpolygon'); set(polyhandle,'ButtonDownFcn','drawbspline(''start'')') pp = [get(polyhandle,'XData'); get(polyhandle,'YData')]; % find the order and knots in UserData for this figure t = get(polyhandle,'UserData'); if isempty(t) k=4; % default: cubic with uniform knots, endpoint mult. 4 t=[zeros(1,k) 1:size(pp,2)-k (size(pp,2)-k+1)*ones(1,k)]; else k=t(1); t = t(2:length(t)); end yy=compcurve(k,t,pp); % function included below axishandle = findobj(gcbf,'Tag','CurveAxes'); axes(axishandle); handle = line(yy(1,:),yy(2,:),'Color','b'); set(handle,'Tag','Bcurve') set(handle,'EraseMode','xor') set(handle,'UserData',[k t]) set(handle,'ButtonDownFcn','drawbspline(''start'')') case 'erase' set(buttonh,'String','Draw B-spline curve') delete(findobj(gcbf,'Tag','Bcurve')) set(buttonh,'Callback','drawbspline(''draw'')') polyhandle = findobj(gcbf,'Tag','Bpolygon'); set(polyhandle,'ButtonDownFcn','') case 'start', currPt = get(gca,'CurrentPoint'); pos =[currPt(1,1); currPt(1,2)]; polyhandle = findobj(gcbf,'Tag','Bpolygon'); pp = [get(polyhandle,'XData'); get(polyhandle,'YData')]; index = find( sum(abs(pp-repmat(pos,1,length(pp)))) < 0.1); if length(index)>0 set(gcbf,'WindowButtonMotionFcn','drawbspline move') set(gcbf,'WindowButtonUpFcn','drawbspline stop') set(gcbf,'UserData',index(1)) end % clear other objects in the picture convexprop clearweak; convexprop clearstrong; makecontrol hidebez; % display(index); case 'move' currPt = get(gca,'CurrentPoint'); pos =[currPt(1,1); currPt(1,2)]; polyhandle = findobj(gcbf,'Tag','Bpolygon'); pp = [get(polyhandle,'XData'); get(polyhandle,'YData')]; index = get(gcbf,'UserData'); pp(:,index(1)) = pos; set(polyhandle,'XData',pp(1,:)) set(polyhandle,'YData',pp(2,:)) curvehandle = findobj(gcbf,'Tag','Bcurve'); t = get(curvehandle,'UserData'); k=t(1); %order t = t(2:length(t)); %knots yy = compcurve(k,t,pp); set(curvehandle,'XData',yy(1,:),'YData',yy(2,:)); case 'stop' set(gcbf,'WindowButtonMotionFcn','') set(gcbf,'WindowButtonUpFcn','') set(gcbf,'UserData',[]) end function y=compcurve(k,t,pp) tmin = t(k); tmax = t(length(t)-k+1); step = (tmax-tmin)/100; y = bspval(t,pp,tmin:step:tmax); %end