Generate a certain colorful triangle
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[3] i}, {i, 0, n}, {j, i, n}] //
Partition[Riffle @@ #, 3, 1] & /@ Partition[#, 2, 1] &
Example of use:
Map[{RandomColor[], Polygon@#} &, tri[5], {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[3] {y, s + y, y} }\[Transpose] // Polygon
Map[{RandomColor[], #} &, fn[7], {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[12], {2}] // Graphics
]
Color based on the array coordinates:
Array[{Hue[##/400, #/7, #2/7], fn @ ##} &, {7, 7}] // Graphics
I guess something like this:
With[{n = 7},
BlockRandom[SeedRandom["triangles"];
Graphics[Table[{RandomColor[],
RegularPolygon[{Sqrt[3] (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[3]/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[8]
Code is easy, check it by yourself~