I am 100% new to mathematica and I don't really understand if I am doing this correctly. I have some most code except the calculation of the color. I will do that once I know I am doing this correctly. I am trying to make sure it is correct and if am not I need some direction as to what I am doing wrong. I added comments if that helps. Anything helps!
(* Triangle vertices *)
v1 = {-2.0, -2.0};
v2 = {2.0, -2.0};
v3 = {0.0, 2.0};
(* xy-space to plot *)
c1 = {1, 0, 0};
c2 = {0, 1, 0};
c3 = {0, 0, 1};
I am trying to Compute the barycentric coordiantes of a 2D point xy
based on triangle vertices v1
, v2
, v3
and that is what I need help on because I'm not sure that I got it right.
(* Input: xy = {x, y} and global v1, v2, v2 each as {x,y} *)
(* Output: uvw = {u, v, w}, the barycentric coordinates *)
(* The entries in the linear system set-up below need set. I Determine what
values in function *)
computeBaryCoords[xy_] := (
mat = {{v1[[1]], v2[[1]], v3[[1]]}, {v1[[2]], v2[[2]], v3[[2]]}, {v1, v2, v3}};
rhs = {xy[[1]], xy[[2]], mat};
uvw = LinearSolve[mat, rhs];
Return[uvw]
);
computeColor[uvw_] := Return[ (* going to calculate colors *)];
range = 4;
plotTriangle =
Graphics[
{EdgeForm[Thick], White, Polygon[{v1, v2, v3}],
PointSize[0.05],
RGBColor[c1], Point[v1],
RGBColor[c2], Point[v2],
RGBColor[c3], Point[v3]},
Axes -> True,
PlotRange -> {{-range, range}, {-range, range}}];
Manipulate[(
uvw = computeBaryCoords[xy];
rgb = computeColor[uvw];
plotPoint =
Graphics[
{RGBColor[rgb[[1]], rgb[[2]], rgb[[3]]], PointSize[0.08], Point[xy]}];
plotBaryCoords =
Graphics[
{Black
Text[Style["Barycentric Coordinates: ", FontSize -> 15], {0, 4}, Scaled[{1, 1}]],
Text[Style[NumberForm[uvw, 2], FontSize -> 15], {4, 4}, Scaled[{1, 1}]]}];
Show[{plotTriangle, plotPoint, plotBaryCoords}]),
{{xy, {0, 0}}, Locator}]
Comments
Post a Comment