Math 291: Intro to Mathematica (for Linear Algebra)

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

Contents

- Barycentric Coordinates
- Determining if points are within a triangle and shading using a raster

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

Plotting points with Barycentric coordinates

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

Manipulate[
Module[{λ1,λ2,λ3},
λ1=Min[λ[[1]],1-λ[[2]]];λ2=λ[[2]];λ3=1-λ1-λ2;
r=λ1 v1+λ2 v2+λ3 v3;(*pt in Barycentric coordinates*)
Show[{ListPlot[{v1,v2,v3,v1},Joined->True],Graphics[Point[{r}]]},PlotLabel->{λ1,λ2,λ3}]],
Style["Barycentric Coordinates",Bold,12],Delimiter,{{λ,{1/5,1/3},"{(Subscript[λ, 1]),(Subscript[λ, 2])}"},
{0,0},{1,1}},(*{{λ1,.2,"(Subscript[λ, 1])"},0,1},{{λ2,.3,"(Subscript[λ, 2])"},0,1},{{λ3,.5,"(Subscript[λ, 3])"},0,1},*)
Style["Constraint: (Subscript[λ, 1])+(Subscript[λ, 2])+(Subscript[λ, 3])=1",Bold,12],Delimiter,ControlType->Slider2D,ControlPlacement->Left]

Determining if a point is w/in a triangle using Barycentric coordinates then shading the point using a raster.

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

T={{v1[[1]]-v3[[1]],v2[[1]]-v3[[1]]},{v1[[2]]-v3[[2]],v2[[2]]-v3[[2]]}};
TInv=Inverse[T];
a[x_,y_]:=TInv.(Transpose[{{x,y}-v3}])
λ1[x_,y_]=a[x,y][[1]][[1]];
λ2[x_,y_]=a[x,y][[2]][[1]];
λ3[x_,y_]=1-λ1[x,y]-λ2[x,y];

Manipulate[Graphics[Raster[Table[If[0≤λ1[x,y]≤1&&0≤λ2[x,y]≤1&&0≤λ3[x,y]≤1,
(*inside*){1,0,0}λ1[x,y]+{0,1,0}λ2[x,y]+{0,0,1}λ3[x,y],
(*outside*){0,0,0}],
{y,-4,3,0.01*resolution},{x,-3,6,0.01*resolution}]],Frame->True,FrameTicks->False],{{resolution,8},1,20,1,ImageSize->Small},ControlPlacement->Left]