Short Mathematica and Matlab code snippets for "Transformations and Projections" (excluding the codes in chapter 3). ------ Chapter 1 ------ Page 23 d2r=Pi/180; Table[Round[N[16384*Sin[i*d2r]]], {i,0,90}] Page 28 t=Table[ArcTan[2.^{-i}], {i,0,15}]; (* arctans in radians *) d=1; x=2.1; y=0.34; z=46. Degree; Do[{Print[i,", ",x,", ",y,", ",z,", ",d], xn=x+y d 2^{-i}, yn=y-x d 2^{-i}, zn=z-d t[[i+1]], d=Sign[zn], x=xn, y=yn, z=zn}, {i,0,14}] Print[0.60725x,", ",0.60725y] Page 50 tm=Sqrt[x^2+y^2]; a={{x/tm,-y/tm,0},{y/tm,x/tm,0},{0,0,1}}; b={{z,0,Sqrt[1-z^2]},{0,1,0},{-Sqrt[1-z^2],0,z}}; c={{Cos[t],-Sin[t],0},{Sin[t],Cos[t],0},{0,0,1}}; FullSimplify[a.b.c.Transpose[b].Transpose[a] /. x^2+y^2->1-z^2] Page 51 (This is Matlab code) n=3; A=[.5774,-.5774,-.5774; .5774,.7886,-.2115; .5774,-.2115,.7886] % Rotation from 1,1,1 to x-axis Q=eye(n); for j=1:n-1, for i=n:-1:j+1, T=eye(n); D=sqrt(A(j,j)^2+A(i,j)^2); cos=A(j,j)/D; sin=A(i,j)/D; T(j,j)=cos; T(j,i)=sin; T(i,j)=-sin; T(i,i)=cos; T A=T*A; Q=Q*T'; end; end; Q A Page 51 (This is Matlab code) T1=[0.7071,0,0.7071; 0,1,0; -0.7071,0,0.7071]; T2=[0.8165,0.5774,0; -0.5774,0.8165,0; 0,0,1]; T3=[1,0,0; 0,0.9660,0.2587; 0,-0.2587,0.9660]; p=[1;1;1]; a=T1*p b=T2*a c=T3*b ------ Chapter 4 ------ Page 149 (* hemispherical fisheye projection *) Clear[k, n, P, Q, L] k=10; n=50; scal[q_]:=(k Tan[ArcTan[q/k]/2])/q; P=Table[{Random[Real,{-10.,10.}], Random[Real,{-10., 10.}]},{n}]; Q=Table[Sqrt[P[[i]].P[[i]]], {i, n}]; L=Table[Line[{P[[i]], scal[Q[[i]]] P[[i]]}], {i, n}]; Show[Graphics[L], Graphics[Circle[{0, 0}, 10]], Graphics[Point[{0, 0}]], AspectRatio -> 1] Page 151 k = 10; angl = {22.5, 45., 67.5, 89.}; k Tan[angl Degree] k Tan[angl/2 Degree] Page 157 k = 10; n = 50; scal[q_] := (k Tan[ArcTan[q/k]/2])/q; P = Table[{Random[Real, {-10.,10.}], Random[Real, {-10.,10.}]}, {n}]; x = -5; y = 5; (* Location of viewer *) Pt = P - Table[{x, y}, {n}]; Q = Table[Sqrt[Pt[[i]].Pt[[i]]], {i, n}]; L = Table[Line[{P[[i]]+{x, y}, (scal[Q[[i]]] P[[i]])+{x, y}}], {i, n}]; Show[Graphics[L], Graphics[Circle[{0, 0}, k]], Graphics[{AbsolutePointSize[5], Point[{0, 0}]}], Graphics[{AbsolutePointSize[5], Point[{x, y}]}], AspectRatio -> Automatic, PlotRange -> All] Page 195 k=10.; Table[k z/(z+k), {z,0,100,5}] Table[%[[i+1]]-%[[i]], {i,1,20}] Table[Point[{%%[[i]],0}], {i,1,21}]; Show[Graphics[%]] Page 196 l=20.; r=0.1; Table[l(1-(z r/(z+l))), {z,0,100,5}] Table[%[[i]]-%[[i+1]], {i,1,20}] Table[Line[{{i, 17}, {i, %%[[i]]}}], {i,1,21}] Show[Graphics[%]] ----------- Answers to exercises --------- Page 251 t14=2^14; Print["(x*=",(8192-(2 14189.))/t14,",y*=",(14189.+(2 8192))/t14,")"] Print["(x*=",Cos[60 Degree]-2. Sin[60 Degree], ",y*=",Sin[60 Degree]+2. Cos[60 Degree], ")"] Pages 251-252 t14=2^14; Print["(x*=",(2845.-(2 16135.))/t14,",y*=",(16135.+(2 2845.))/t14, ")"] Print["(x*=",Cos[80 Degree]-2. Sin[80 Degree], ",y*=",Sin[80 Degree]+2. Cos[80 Degree], ")"] Pages 267-268 (* exercise for hemispherical fisheye projection *) k=1; scal[q_]:=(k Tan[ArcTan[q/k]/2])/q; {scal[1.],scal[10.], scal[100.], scal[1000.], scal[10000.]}