# Generate a certain colorful triangle

I want to get a colorful triangle like this: I hope to get a triangle with any number of layers. This is my current method. Actually, I'm not very content with these graph theory functions, since I have to use Quiet to mute the error information.

pointPair = Subsets[{{0, 0}, {1, Sqrt}, {2, 0}}, {2}];
midPoint[{a_, b_}, {c_, d_}, n_] :=
Transpose[{Subdivide[a, c, n], Subdivide[b, d, n]}]
layers = 8;(*Control the layers*)
poly = Polygon /@
FindClique[
Quiet[NearestNeighborGraph[
Level[RegionIntersection @@@
Subsets[Line /@
Transpose /@
MapAt[Reverse,
Subsets[midPoint[##, layers] & @@@ pointPair, {2}], {2,
2}], {2}], {3}]]], {3}, All];
Graphics[Transpose[{RandomColor[Length[poly]], poly}]]


I am late to see this question but here is a solution closely based on my answer to Creating a Sierpinski gasket with the missing triangles filled in.

tri[n_] :=
Table[{2 j - i, Sqrt i}, {i, 0, n}, {j, i, n}] //
Partition[Riffle @@ #, 3, 1] & /@ Partition[#, 2, 1] &


Example of use:

Map[{RandomColor[], Polygon@#} &, tri, {2}] // Graphics ## A different approach

For some reason I found this problem unusually interesting so that even after "solving" it I was thinking about it. It occurred to me that the total number of triangles is $n^2$ therefore I wanted to make a function that could generate these from a call to Array rather than Table. (The latter permits non-rectangular indices as used in my first method.)

My method is to reflect the triangles that fall outside of target back inside. fn[n_] := Array[fn, {n, n}]

fn[i_, j_] /; j > i := fn[j, i + 1, -1]

fn[x_, y_, s_: 1] :=
{ 2 x - y + {0, 1, 2}, Sqrt {y, s + y, y} }\[Transpose] // Polygon

Map[{RandomColor[], #} &, fn, {2}] // Graphics • Note: by design every triangle is generated separately which is not as efficient as my first approach which generates entire rows in one operation.

Keeping the coloration separate allows some interesting flexibility. Coloring sequentially provides a pleasing effect due to the order of generation.

Module[{i = 0},
Map[{ColorData["Rainbow"][i++/144], #} &, fn, {2}] // Graphics
] Color based on the array coordinates:

Array[{Hue[##/400, #/7, #2/7], fn @ ##} &, {7, 7}] // Graphics – yode
Jul 25, 2016 at 16:23
• @yode I am glad you like it. :-) Jul 25, 2016 at 21:21
• Both look good, but the latter is much slower...
– DPF
Jul 26, 2016 at 7:09
• @DPF Thank you. I am not surprised regarding speed; the first one was written with efficiency in mind, while the second one is simply pursuing a particular idea. Jul 26, 2016 at 7:12

I guess something like this:

With[{n = 7},
BlockRandom[SeedRandom["triangles"];
Graphics[Table[{RandomColor[],
RegularPolygon[{Sqrt (j + i - 1),
3 j + Boole[EvenQ[i]]}/2,
{1, (-1)^i π/6}, 3]},
{i, 2 n - 1}, {j, n - Quotient[i, 2]}]]]] This question is not a bit hard:

mat = {{1, 0}, {1/2, Sqrt/2}};
draw[n_] :=
Graphics[Table[{RandomColor[],
Triangle[{{i + n + 1 - #, j + n + 1 - #}, {i, j + 1}, {i + 1,
j}}.mat]}, {i, n}, {j, # - i}] & /@ {n, n + 1}];
draw


Code is easy, check it by yourself~

• small error: {i, 8} should be {i, n} Jul 24, 2016 at 11:42
• @m_goldberg thanks！
– Wjx
Jul 24, 2016 at 12:44
• Here is a slight variation of your code: draw[n_] := Graphics[Table[{RandomColor[], Triangle[{{i, j} + 1 - #, {i, j + 1}, {i + 1, j}}.mat]}, {i, n}, {j, n + # - i}] & /@ {0, 1}]. Or using an additional Table iterator instead of Map: draw[n_] := Graphics @ Table[{RandomColor[], Triangle[{{i, j} - z, {i, j + 1}, {i + 1, j}}.mat]}, {z, -1, 0}, {i, n}, {j, n + 1 + z - i}] Aug 1, 2016 at 5:11

Anothor way by NestList

randomTriPlot[n_] := Module[{next},
next[polys_] :=
Join[Map[# + {-1, -Sqrt} &,
polys, {2}], {MapAt[# - 2 Sqrt &,
polys[[-1]], {1, 2}], # + {1, -Sqrt} & /@ polys[[-1]]}];
(*get coordinate of the next layer by translate this layer*)
Flatten@
Map[Polygon,
NestList[next, N@{{{0, 0}, {-1, -Sqrt}, {1, -Sqrt}}},
n - 1], {2}] // Graphics[Thread[{RandomColor[Length@#], #}]] &
]
randomTriPlot

• 你是“无影冬瓜”么？感觉名字好像啊…
– Wjx
Jul 24, 2016 at 13:52
• @Wjx Yes, you got it:)
– xyz
Jul 24, 2016 at 14:12
• Xie xie gua ge. :)
– yode
Jul 25, 2016 at 5:16

Using the trick in this answer to use MeshFunctions and Dynamic MeshShading with random colors:

coloredTriangles = ParametricPlot[{x, y Sqrt Min[x, 2 - x]}, {x, 0, 2}, {y, 0, 1},
MeshFunctions -> {Sqrt # + #2 &, #2 - Sqrt # &, #2 &},
Mesh -> # - 1, Exclusions -> None, ImageSize -> 200,

Row[coloredTriangles /@ {3, 4, 6, 8}, Spacer] 