How do I convert this Nicomachus's Triangle to one with edges?
Something like this?
triangleForm[t : {_List ..} /; Depth@t == 3] :=
Show[Graphics[{
MapIndexed[Text[#, {#2 - #/2, -#} & @@ #2] &, t, {2}],
Red,
Line[{{1/2, -1}, {1 - #/2, -#}, {#/2, -#}, {1/2, -1}}] & @ Length[t]
}],
TextStyle -> FontSize -> 18]
triangleForm[t]
Or probably more like:
triangleForm[t : {_List ..} /; Depth@t == 3] :=
Show[Graphics[{
MapIndexed[Text[#, {#2 - #/2, -#} & @@ #2] &, t, {2}],
Red,
Line[{{1/2, 0}, {-#/2, -# - 1/2}, {1 + #/2, -# - 1/2}, {1/2, 0}}] & @ Length[t]
}],
TextStyle -> FontSize -> 18]
triangleForm[t]
With automatic image size:
r = 9;
t = Table[2^(n - k) 3^k, {n, 0, r}, {k, 0, n}];
triangleForm[t : {_List ..} /; Depth@t == 3] :=
Show[Graphics[{
MapIndexed[Text[#, {#2 - #/2, -#} & @@ #2] &, t, {2}],
Red,
Line[{{1/2, 0}, {-#/2, -# - 1/2}, {1 + #/2, -# - 1/2}, {1/2, 0}}] & @ Length[t]
}],
TextStyle -> FontSize -> 16,
ImageSize -> (Length@t + 1) * First@Rasterize[Style[t[[-1, -1]], 16], "RasterSize"]
]
triangleForm[t]
Another variation following a comment below:
r = 8;
t = Table[2^(n - k) 3^k, {n, 0, r}, {k, 0, n}];
triangleForm[t : {_List ..} /; Depth@t == 3] :=
Show[Graphics[{
Red,
(Line /@ Join[#, Riffle @@@ Partition[#, 2, 1]]) &@
Table[{(1 - i + 2 j - r)/2, i - r - 1}, {i, 0, r}, {j, i, r}],
MapIndexed[Text[Panel[#, FrameMargins -> 0], {#2 - #/2, -#} & @@ #2] &, t, {2}]
}],
TextStyle -> FontSize -> 18
]
triangleForm[t]
Here's an alternative way to create the latter picture in Mr.Wizard's answer using Graph
:
elem[n_, k_] := 2^(n - k) 3^k
Block[{e, r},
r = 7;
vertices = Flatten[Table[e[n, k], {n, 0, r}, {k, 0, n}]];
edges = Flatten[{
Table[e[n, k] \[UndirectedEdge] e[n + 1, k], {n, 0, r - 1}, {k, 0, n}],
Table[e[n, k] \[UndirectedEdge] e[n + 1, k + 1], {n, 0, r - 1}, {k, 0, n}],
Table[e[n, k] \[UndirectedEdge] e[n, k + 1], {n, 1, r}, {k, 0, n - 1}]}];
coords = Flatten[Table[{-n/2 + k, r - n}, {n, 0, r}, {k, 0, n}], 1];
Graph[vertices, edges, VertexCoordinates -> coords,
EdgeStyle -> Directive[Thick, Red],
VertexShapeFunction -> ({Black,
Inset[Framed[Style[#2 /. e -> elem, 12, FontFamily -> "Helvetica"],
Background -> White, RoundingRadius -> 3], #1]} &)]
]
For $r \in \{2 \ldots 10\}$, you can use GraphData[{"TriangularGrid", r}]
to get a graph with the desired structure:
GraphData["TriangularG*"]
{{"TriangularGrid", 2}, {"TriangularGrid", 3}, {"TriangularGrid", 4}, {"TriangularGrid", 5}, {"TriangularGrid", 6}, {"TriangularGrid", 7}, {"TriangularGrid", 8}, {"TriangularGrid", 9}, {"TriangularGrid", 10}}
For example,
GraphData[{"TriangularGrid", 4}]
We can use it to make a function to add labels and other options:
ClearAll[tF, labelingF, tGG]
tF = Flatten@Table[2^(n - k) 3^k, {n, 0, #}, {k, 0, n}] &;
labelingF = AssociationThread[Range @ Length @ #, #] & @ tF[#] &;
tGG = Module[{tg = GraphData[{"TriangularGrid", #}], lbl = labelingF[#]},
SetProperty[tg,
{VertexShapeFunction -> (Text[Framed[Style[lbl @ #2, Black],
RoundingRadius -> 5, FrameStyle -> Gray, Background -> White], #] &), ##2}]] &;
Examples:
Row[tGG[#, EdgeStyle -> Red, ImageSize -> 300] & /@ {3, 5, 7}, Spacer[10]]