Rick answered my request for non recursive Sierpinski line triangle yesterday with code that was not only non recursive, it also didn't use arrays. This impressed me and inspired me to write up this modification:
'3 color mod of Sierpinski non recursive line triangle by Rick.sdlbas [B+=MGA] post 2016-05-25 SdlBasic
'MGA edit of Sierpinski non recursion by rick3137 posted 2016-05-24
'MGA added allot of mods!
common XMAX = 500, YMAX = 500
common X, Y, LEVEL
common PI = aCos(-1), A60 = PI / 3, A120 = 2 * A60, AMAX = 2 * PI - .01 '<== last is for FOR loop
common RED = 0xFF0000, WHITE = 0xFFFFFF, BLUE = 0x0000FF
setdisplay( xmax, ymax, 32, 1)
autoback(-2)
sub SierpinskiLineTriangle(size)
size2 = size * sin(A60)
for cnt4 = 1 to 3
if LEVEL = 4 then : ink (RED) : end if
if cnt4 = 2 then
if LEVEL = 4 then : ink (BLUE) : end if
x = x + size * 4.5 : y = y + size2 * 7
end if
if cnt4 = 3 then
if LEVEL = 4 then : ink (WHITE) : end if
x = x - size * 7.5 : y = y - size2
end if
for cnt3 = 1 to 3
if LEVEL = 3 then : ink (RED) : end if
if cnt3 = 2 then
if LEVEL = 3 then : ink (BLUE) : end if
x = x + size * 2.5 : y = y + size2 * 3
end if
if cnt3 = 3 then
if LEVEL = 3 then : ink (WHITE) : end if
x = x - size * 3.5 : y = y - size2
end if
for cnt2 = 1 to 3
if LEVEL = 2 then : ink (RED) : end if
if cnt2 = 2 then
if LEVEL = 2 then : ink (BLUE) : end if
x = x + size * 1.5 : y = y + size2
end if
if cnt2 = 3 then
if LEVEL = 2 then : ink (WHITE) : end if
x = x - size * 1.5 : y = y - size2
end if
'inner most, smallest triagnles
for i = 1 to 3
if LEVEL = 1 then : ink (RED) : end if
if i = 2 then
if LEVEL = 1 then : ink (BLUE) : end if
x = x + size
end if
if i = 3 then
if LEVEL = 1 then : ink (WHITE) : end if
x = x - size/2 : y = y - size2
end if
if key(27) then : end : end if
'--------------------------- draw triangle here to save extra calls to another sub
for Ang = 0 to AMAX step A120 'step 0 , 120, 240 but not 360
dx = cos(Ang) * size
dy = sin(Ang) * size
xx = x + dx
yy = y - dy
line( x, y, xx, yy)
x = xx : y = yy
next
'------------------------- end of draw triangle
next
next
next
next
end sub
ctr = 0
while key(27) = 0
for LEVEL = 0 to 4
x=10 : y=ymax-10 : start = 30
cls
if LEVEL = 0 then
ctr +=1 : ctr =ctr mod 3
select case ctr
case 0 : ink(BLUE)
case 1 : ink(RED)
case 2 : ink(WHITE)
end select
end if 'color for whole trangle
SierpinskiLineTriangle(start)
ink(0xFFFFFF)
text( 10, 0, 14, "Hold esc to exit..." )
screenswap
wait(150)
next
wend
end
This is dynamic, so screenshots wouldn't do code justice but it cycles through red, white and blue triangles at different levels (sizes) of Sierpinski triangle without changing overall size.