Math 291: Intro to Mathematica (for Linear Algebra)

with dusty wilson, mathematics instructor, highline community college
week 8

Contents

- Rasters and Barycentric Coordinates
- Determining if points are within a triangle and shading using a raster
- Normals to a vertex

$Post:=If[MatrixQ[#],MatrixForm[#],#]&

Vertices for us to play with

v1={1,2};
v2={-2,-3};
v3={5,-2};

Experiment with rasters and use "If" to identify a portion of the raster

x varies from -2 to 5
y varies from -3 to 2

Show[Graphics[{Raster[Table[If[3<x<4&&0<y<1,{0,0,0},{x,y,.5Sin[x+ y]+.5}],{y,-3,2,.1}, {x,-2,5,.1}]]}],Axes->True]

Locate a triangle within a raster using Barycentric coordinates

T={{v1[[1]]-v3[[1]],v2[[1]]-v3[[1]]},{v1[[2]]-v3[[2]],v2[[2]]-v3[[2]]}};
invT=Inverse[T];

temp=invT.({x,y}-v3);
barycoord=AppendTo[temp,1-temp[[1]]-temp[[2]]];

Show[Graphics[{Raster[Table[If[
0≤barycoord[[1]]≤1
&&
0≤barycoord[[2]]≤1
&&
0≤barycoord[[3]]≤1
,{0,0,0},barycoord], {x,-2,5,.1},{y,-3,2,.1}]]}],Axes->True]

Shade a triangle within a raster using Barycentric coordinates

Show[Graphics[{Raster[Table[If[
0≤barycoord[[1]]≤1
&&
0≤barycoord[[2]]≤1
&&
0≤barycoord[[3]]≤1
,{1,0,0}*barycoord[[1]]+{0,1,0}*barycoord[[2]]+{0,0,1}*barycoord[[3]],{0,0,0}], {x,-2,5,.1},{y,-3,2,.1}]]}],Axes->True]

Polygon

pts1={{0,0,0},{1,0,0},{0,2,0},{0,0,3}};

Show[Graphics3D[{{PointSize[Large],Point[pts1]},Table[Polygon[Drop[pts1,{k}]],{k,1,4}]}],Lighting->Automatic]

Average of triangle edges to generate normals to a vertex

For more info: http://en.wikipedia.org/wiki/Vertex_normal

pts1={{0,0,0},{1,0,0},{0,2,0},{0,0,3}};
tmp=Table[(1/3)(3 pts1[[i]]-{1,1,1}.Drop[pts1,{i}]),{i,1,4}];
For[i=1,i≤4,i++,
tmp[[i]]=((tmp[[i]])/(Sqrt[tmp[[i]].tmp[[i]]]))];

arrow=Table[Arrow[{pts1[[i]],pts1[[i]]+tmp[[i]]}],{i,1,4}];

Show[Graphics3D[{arrow,{PointSize[Large],Point[pts1]},Table[Polygon[Drop[pts1,{k}]],{k,1,4}]}],Lighting->Automatic]

Average of surface normals to generate normals to a vertex

For more info: http://en.wikipedia.org/wiki/Vertex_normal

pts1={{0,0,0},{1,0,0},{0,2,0},{0,0,3}};

(*Generates a list of matrices.  Each matrix has rows made up of the vectors pointing away from a single vertex.*)
temp4={};
For[i=1,i≤4,i++,
temp2=pts1[[i]];
temp3=Drop[pts1,{i}];
For[j=1,j≤3,j++,temp3[[j]]-=temp2];
AppendTo[temp4,temp3];
];

(*Generates a list of matrices where the rows of each matrix give the normal vectors of the triangles that share a single vertex*)
temp7={};
For[j=1,j≤4,j++,
temp6={};
For[i=1,i≤3,i++,temp5=Drop[temp4[[j]],{i}];
temp9=Cross[temp5[[1]],temp5[[2]]];
AppendTo[temp6,(temp9/(Sqrt[temp9.temp9]))];
]
AppendTo[temp7,temp6]
]

(*average of surface normals adjacent to the vertex*)
temp8={};
For[i=1,i≤4,i++,
AppendTo[temp8,(1/3){1,1,1}.temp7[[i]]]]

(*graph the vectors*)

arrow=Table[Arrow[{pts1[[i]],pts1[[i]]+temp8[[i]]}],{i,1,4}];

Show[Graphics3D[{arrow,{PointSize[Large],Point[pts1]},Table[Polygon[Drop[pts1,{k}]],{k,1,4}]}],Lighting->Automatic]