# Help with coding triangular nodes/elements

I'm trying to write a program where it will shoot out the triangle node numbers. For example, in the picture, if we choose to divide this triangle by 5 points, then the program will return out the following:

dat1 = {{0, 1, 6}, {1, 2, 7}, {2, 3, 8}, {3,4, 9}, {4, 5, 10}, {6, 7, 11}, {7, 8, 12}, {8, 9, 13}, {9, 10, 14}, {11, 12, 15}, {12, 13, 16}, {13, 14, 17}, {15, 16, 18}, {16, 17, 19}, {18, 19, 20}}

dat2 = {{1, 7, 6}, {2, 8, 7},{3, 9, 8}, {4, 10, 9}, {7, 12, 11}, {8, 13, 12}, {9, 14, 13}, {12, 16, 15}, {13, 17, 16}, {16, 19, 18}}


So dat1 is the list of the bottom triangles, while dat 2 is the list of the top triangles. So depending on how many points we divide the triangle by (in picture it is 5), then it will return out dat1 and dat2. Any suggestions or help on this?

Step 1 Make vertex

n = 6;
list = NestList[Rest[# + Length[#] - 1] &, Range[n] - 1, n - 1]


{{0, 1, 2, 3, 4, 5}, {6, 7, 8, 9, 10}, {11, 12, 13, 14}, {15, 16, 17}, {18, 19}, {20}}

Step 2 bottom and top functions are made like this.

bottom[f_, l_] := MapThread[List, {Partition[f, 2, 1], l}]
top[f_, l_] := MapThread[List, {f[[2 ;; -2]], Reverse /@ Partition[l, 2, 1]}]


Step 3 You can make triangles

triangle[l_] := Partition[Flatten[l @@@ Partition[list, 2, 1]], 3]


Let's try this.

triangle[bottom]


{{0, 1, 6}, {1, 2, 7}, {2, 3, 8}, {3, 4, 9}, {4, 5, 10}, {6, 7, 11}, {7, 8, 12}, {8, 9, 13}, {9, 10, 14}, {11, 12, 15}, {12, 13, 16}, {13, 14, 17}, {15, 16, 18}, {16, 17, 19}, {18, 19, 20}}

triangle[top]


{{1, 7, 6}, {2, 8, 7}, {3, 9, 8}, {4, 10, 9}, {7, 12, 11}, {8, 13, 12}, {9, 14, 13}, {12, 16, 15}, {13, 17, 16}, {16, 19, 18}}

vp = Flatten[Table[
Transpose[{Range[i], Table[n - i, {i}]}], {i, n, 1, -1}], 1];

Graphics@GraphicsComplex[vp, {EdgeForm[Green],LightGreen,Polygon[triangle[bottom]+1],
Red, (Text[Sequence @@ #, Background -> LightYellow] & /@
Transpose[{Flatten[list], vp}])}]


 n = 6;
dat1=Flatten[Table[ { #[[1, i]] , #[[1, i + 1]], #[[2, i]]} , {i, Length[#[[1]]] - 1}] & /@
Partition[
Table[(n (n + 1) - i (i + 1))/2  + Range[ i ] - 1, {i, n, 1, -1}] , 2, 1],1]


{{0, 1, 6}, {1, 2, 7}, {2, 3, 8}, {3, 4, 9}, {4, 5, 10}, {6, 7, 11}, {7, 8, 12}, {8, 9, 13}, {9, 10, 14}, {11, 12, 15}, {12, 13, 16}, {13, 14, 17}, {15, 16, 18}, {16, 17, 19}, {18, 19, 20}}

dat2 left as an exercise..

• Thanks! If possible, can you give a little explanation on what is going on here? I know there's a few tables being created here, but am confused on what is actually going on here. Commented Aug 22, 2014 at 20:53

Adapting my solution from the related (duplicate?) question Writing loops for triangle elements:

fn2[_, 0] := {}

fn2[s_: 0, n_] := Join[
Array[Thread@{#, # + {1, n}, # + n + 1} &, n, s]\[Transpose] // MapAt[Rest, 2],
fn2[s + n + 1, n - 1],
2
]


Test:

{dat1, dat2} = fn2[5]

{{{0, 1, 6}, {1, 2, 7}, {2, 3, 8}, {3, 4, 9}, {4, 5, 10}, {6, 7, 11}, {7, 8, 12}, {8, 9, 13},
{9, 10, 14}, {11, 12, 15}, {12, 13, 16}, {13, 14, 17}, {15, 16, 18}, {16, 17, 19}, {18, 19, 20}},
{{1, 6, 7}, {2, 7, 8}, {3, 8, 9}, {4, 9, 10}, {7, 11, 12}, {8, 12, 13}, {9, 13, 14},
{12, 15, 16}, {13, 16, 17}, {16, 18, 19}}}


The nodes for the top triangles are given in canonical order rather than the order you gave; if this is a problem it can easily be corrected using e.g.

dat2[[All, {1, 3, 2}]]

{{1, 7, 6}, {2, 8, 7}, {3, 9, 8}, {4, 10, 9}, {7, 12, 11}, {8, 13, 12}, {9, 14, 13},
{12, 16, 15}, {13, 17, 16}, {16, 19, 18}}