%% primjer 2. clear, close all, clc %set(hline2,'Marker','.'); axis([0 10 0 10]); title('odaberite tocke vrhova na mnogokuta') hold on br=0; F=1; set(gcf,'WindowButtonDownFcn', ['if F==1;mouse = get(gca,''currentpoint'');,plot(mouse(1,1),mouse(1,2),''go''),',... 'drawnow , set(gca, ''ylim'', [0 10],''Xlim'',[0 10]),',... 'br=br+1, T(1:2,br)=[mouse(1,1);mouse(1,2)],',... 'if br>2, set(pb,''visible'', ''on''), end, end']) pb=uicontrol('style','pushbutton',... 'string','nacrtaj mnogokut', ... 'units','normalized',... 'visible','off',... 'position',[0 0 0.2 0.05], ... 'callback',' h1=patch(T(1,:),T(2,:),''y'');F=0;'); %% primjer 2 sa unaprijed zadanim brojem tocaka f=figure axis([-5, 5,-5,5]) h3=uicontrol('Style','popupmenu',... 'Position',[10 5 50 15] ,... 'String',{'4' '6' '8'},... 'callback',['cla, n= 2+get(h3,''Value'')*2,[x,y]=ginput(n),patch(x,y,''y''), axis([-5, 5,-5,5]), ' ]); %% primjer 3. clear all x = 0:.2:10; y = sin(x); y2 = 2*cos(x); hline1=plot(x,y,'.-'); hold on hline2=plot(x,y2,'r.-'); hit=1; text=uicontrol('style','text',... 'position',[10,5,130,20], ... 'FontWeight', 'bold',... 'FontSize',10,... 'HorizontalAlignment','Center',... 'String', 'odabrali ste :'); text2=uicontrol('style','text',... 'position',[150,5,175,20], ... 'FontWeight', 'bold',... 'FontSize',10,... 'HorizontalAlignment','Center'); text3=uicontrol('style','text',... 'position',[400,5,130,15], ... 'FontWeight', 'bold',... 'FontSize',8,... 'HorizontalAlignment','Center',... 'String','Za izlaz pritisnite tipku'); %set(gcf,'WindowScrollWheelFcn','lim=get(gca,''Ylim'');set(gca,''Ylim'',lim*1.1)'); %zapocinje beskonacnu petlju i ceka klik misa stopit=0; while (stopit==0) w = waitforbuttonpress; if w == 0 hit=1; else stopit=1; set(text3,'String','The end!'); pause(1); close all break end %ceka valjani objekt - liniju 1 %provjera dali je tocka odabrana if gco == hline1 & hit==1 %~isempty(gco) %dohvaca poziciju misa mouse = get(gca,'currentpoint'); %racunamo najblizu tocku, odabranoj tocki [val, pnt] = min( sqrt((get(hline1,'xdata')-mouse(1,1)).^2 ... +(get(hline1,'ydata')-mouse(1,2)).^2)); disp(['funkcija sin x, najbliza je tocka s r.br. ',num2str(pnt)]) hit=0;%ceka slijedeci klik br1=round(1000*mouse(1,1))/1000; br2=round(1000*mouse(1,2))/1000; set(text2,'String',[' (x, y), =( ',num2str(br1),' , ',num2str(br2),' )']); end %ceka valjani objekt - liniju 2 %provjera dali je tocka odabrana if gco == hline2 & hit==1 mouse = get(gca,'currentpoint'); [val, pnt] = min( sqrt( (get(hline2,'xdata')-mouse(1,1)).^2 ... +(get(hline2,'ydata')-mouse(1,2)).^2)); disp(['funkcija 2 cos x, najbliza je tocka s r.br. ',num2str(pnt)]) hit=0; br1=round(1000*mouse(1,1))/1000; br2=round(1000*mouse(1,2))/1000; set(text2,'String',[' (x, y), =( ',num2str(br1),' , ',num2str(br2),' )']); end if gco ~= hline2 & gco ~= hline1 & hit==1 disp('niste odabrali ni jednu funkciju') set(text2,'String',' '); end drawnow end %% primjer 4. clf clear all %set(gcf,'doublebuffer','on') tmax = 2 ; NumControl = 20 ; t = linspace(0, tmax, NumControl) ; tt = linspace(0, tmax) ; %pocinjemo sa tockama na x-osi y = zeros(1,length(t)); cs = spline(t,y); yy = ppval(cs,tt); hold on sp = plot(tt,yy); line1 = plot(t,y,'or'); set(gca, 'ylim', [-90 90]) set(gcf, 'DeleteFcn','stopit=1 ');% kako bismo izbjegli beskonacnu petlju %kad se dogodi klik s misem, spremamo to u pripadnu varijabli set(gcf,'windowbuttondownfcn','mousedown=1;'); set(gcf,'windowbuttonupfcn','mousedown=0;');%clc %make a control to stop the loop uicontrol('style','pushbutton',... 'string','Quit', ... 'position',[0 0 50 20], ... 'callback','stopit=1;'); %zapocinje petlja koja ceka klik misa stopit = 0; mousedown = 0; while (stopit==0) %provjera dali se klik dogodio if mousedown==1 %dohvaca odabranu poziciju mouse = get(gca,'currentpoint'); %racuna najblizu tocku, odabranoj tocki lineX = get(line1,'xdata'); lineY = get(line1,'ydata'); [val, pnt] = min(abs(lineX-mouse(1,1))); %pomice kontrolnu tocku na odabranu poziciju lineY(pnt) = mouse(1,2); set(line1, 'ydata', lineY); %make new spline cs = spline(t,lineY); yy = ppval(cs,tt); set(sp, 'ydata', yy); drawnow mousedown=0; end drawnow end close