Skip to main content
6 of 12
deleted 25 characters in body
Dominic van Essen
  • 36.4k
  • 2
  • 22
  • 60

R, 270 265 252 bytes

function(s,`~`=rbind,m=matrix){n=nchar(s)
s=el(strsplit(s,''))
p=m(0,2,n^2)
while(T<=n){a=4*F+1
p[,T+1:a-1]=c(F:-F,(-F:F)[-1])~1:a-2*F-1
p[,T+a+1:(a+2)-1]=F+1~(b=2*F+1):-b
T=T+2*a+2
F=F+1}
p=p[,1:n]-min(p)+1
m=m(" ",n,n)
m[t(p)]=s
n=apply(m,1,cat,"
")}

Try it online!

Works by constructing the coordinates for each letter, then using this to put the letters into an empty matrix.

Commented:

triangle=
function(s){n=nchar(s)          # n is the number of letters
s=el(strsplit(s,''))            # first split the string into individual letters
p=matrix(,2,n^2)                # initialize p as a 2-row matrix to hold the coordinates
                                # (with plenty of columns so that we've enough to go all 
                                # the way round the outermost triangle)
                                # now, F is the current loop, starting at 0
while(T<=n){                    # T is the current letter index
a=4*F+1                         # a=the size of the 'arch' (number of letters going up & over)
p[,T+1:a-1]=                    # set the coordinates for the arch letters...
  rbind(                        # ...(rbind combines rows for y & x coordinates)...
    c(F:-F,(-F:F)[-1]),         # ...to y = F..-F, and then -F+1..F (so: up & then down again)
    1:a-2*F-1)                  # ...and x = across the arch from -2*F to +2*F
a=a+2                           # a=now the width of the base = size of arch + 2
p[,T+a+1:a-3]=                  # now set the coordinates of the base letters...
  rbind(                        #
    F+1,                        # ... base y = row F+1
    (b=2*F+1):-b)               # ... and x = goes (backwards) from 2*F+1..-2*F-1
T=T+2*a-2                       # update the current letter index
F=F+1}                          # increment the loop
p=p[,1:n]                       # delete any excess coordinates
p=p-min(p)+1                    # re-zero the coordinates to remove negatives
m=matrix(" ",b<-max(p),b)       # create a new matrix filled with " "
m[t(p)]=s                       # and fill it with the letters at the right positions
n=apply(m,1,cat,"               # finally, print each row
")}
Dominic van Essen
  • 36.4k
  • 2
  • 22
  • 60