User Tools

Site Tools


supp:octave:contrib:arrow:start

Rotate Vectors and Draw Arrows

arrow.m

Draw vector arrow.

arrow.m
## Copyright (C) 2011 rolf.becker
## 
## This program is free software; you can redistribute it and/or modify
## it under the terms of the GNU General Public License as published by
## the Free Software Foundation; either version 2 of the License, or
## (at your option) any later version.
## 
## This program is distributed in the hope that it will be useful,
## but WITHOUT ANY WARRANTY; without even the implied warranty of
## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
## GNU General Public License for more details.
## 
## You should have received a copy of the GNU General Public License
## along with Octave; see the file COPYING.  If not, see
## <http://www.gnu.org/licenses/>.
 
## arrow
 
## Author: rolf.becker <rolf.becker@NB-RB>
## Created: 2011-04-04
 
 
function [ ret ] = arrow (p1,p2,scal,sty,lw)
% usage: arrow (p1,p2,s, style)
% plots 2D vector arrow from p1 to p2 (arrow head)
% s: scale factor for arrow tipp hands in units, e.g. s=1
% style: 0: open arrow head, 1: closed arrow head, 2: filled
 
if size(p1)(1)==1; p1=p1'; endif;
if size(p2)(1)==1; p2=p2'; endif;
 
phi = pi/4; % angle of arrow
 
%s=0.1; % scale factor
dp=p2-p1;
l=norm(dp);
 
if l!=0
 
L=[ cos(phi/2) -sin(phi/2) ; sin(phi/2) cos(phi/2)]; % turn left
R=[ cos(-phi/2) -sin(-phi/2) ; sin(-phi/2) cos(-phi/2)]; % turn right
 
dr=R*dp/l*scal; % turn unit vector of dp left and scale with s
dl=L*dp/l*scal; % turn unit vector of dp left and scale with s
 
myhold = ishold;
 
plot([p1(1) p2(1)],[p1(2) p2(2)],"linewidth",lw); % plot line between p1 and p2
 
hold on;
 
if ((sty == 0) | (sty == 1))
	plot([p2(1)-dl(1) p2(1)],[p2(2)-dl(2) p2(2)],"linewidth",lw); % plot arrow line between p2 and p2 - dl 
	plot([p2(1)-dr(1) p2(1)],[p2(2)-dr(2) p2(2)],"linewidth",lw); % plot arrow line between p2 and p2 - dr
endif
 
if (sty == 1) % close arrow head
	plot([p2(1)-dr(1) p2(1)-dl(1)],[p2(2)-dr(2) p2(2)-dl(2)],"linewidth",lw);
 
 elseif (sty == 2) % fill arrow head
	A=[p2 p2-dl p2-dr];
	patch(A(1,:),A(2,:),'b',"linewidth",lw);
endif
 
if myhold == 0; hold off; endif
 
endif
 
endfunction

arrow_test.m

Draw three vector arrows connecting points.

arrowtest.m
## Copyright (C) 2011 rolf.becker
## 
## This program is free software; you can redistribute it and/or modify
## it under the terms of the GNU General Public License as published by
## the Free Software Foundation; either version 2 of the License, or
## (at your option) any later version.
## 
## This program is distributed in the hope that it will be useful,
## but WITHOUT ANY WARRANTY; without even the implied warranty of
## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
## GNU General Public License for more details.
## 
## You should have received a copy of the GNU General Public License
## along with Octave; see the file COPYING.  If not, see
## <http://www.gnu.org/licenses/>.
 
## arrow_test
 
## Author: rolf.becker <rolf.becker@NB-RB>
## Created: 2011-04-04
 
function [ ret ] = arrow_test ()
P0=[0 0]';
P1=[1 1]';
P2=[-1 1]';
P3=[-1 -1]';
 
scale=0.1;
style=2;
lw=2;
 
arrow(P0,P0+P1,scale,style,lw)
hold on
arrow(P0+P1,P0+P1+P2,scale,style,lw)
arrow(P0+P1+P2,P0+P1+P2+P3,scale,style,lw)
hold off
 
axis equal
axis square
grid on
 
endfunction

turn.m

turn.m
## Copyright (C) 2018 rolf.becker
## 
## This program is free software; you can redistribute it and/or modify
## it under the terms of the GNU General Public License as published by
## the Free Software Foundation; either version 2 of the License, or
## (at your option) any later version.
## 
## This program is distributed in the hope that it will be useful,
## but WITHOUT ANY WARRANTY; without even the implied warranty of
## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
## GNU General Public License for more details.
## 
## You should have received a copy of the GNU General Public License
## along with Octave; see the file COPYING.  If not, see
## <http://www.gnu.org/licenses/>.
 
## turn
 
## Author: rolf.becker <rolf.becker@hsrw.eu>
## Created: 2018-11-12
 
function [ ret ] = turn ()
	scl = 0.4;
	lw  = 3;
	sty = 2;
	n   = 16;
 
	Z  = [0 0]';
	P1 = [2 0]';
	P2 = [0 1]';
	M  = 2*[1 1]';
 
	phi=2*pi/n;
	R=( [cos(phi) -sin(phi) ; sin(phi) cos(phi)] );
 
	for i=0:n
		arrow(Z,M,scl,sty,lw)
		axis([-5 5 -5 5])
		axis square
		hold on
		arrow(M,M+P1,scl,sty,lw)
		arrow(M,M+P2,scl,sty,lw)
		hold off
		grid on
		set(gca,"fontsize",24,"xtick",-5:5,"ytick",-5:5)
%		title("Rotating Vectors");
		xlabel("x");
		ylabel("y");
 
		M=R*M;
		P1=R*P1;
		P2=R*P2;
		sleep(0.1);
	endfor
endfunction
supp/octave/contrib/arrow/start.txt · Last modified: 2018/11/12 16:13 by admin