Author Topic: 3 color mod of Sierpinski non recursive line triangle by Rick3137  (Read 2358 times)

B+

  • Guest
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:

Code: [Select]
'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.
« Last Edit: May 25, 2016, 03:37:06 PM by B+ »

ZXDunny

  • Guest
Re: 3 color mod of Sierpinski non recursive line triangle by Rick3137
« Reply #1 on: May 26, 2016, 12:20:35 AM »
Wow, excellent. Though I'm a little disappointed that it's a brute-force approach (basically recursion unrolled) rather than algorithmic, but hey, who cares? :)

D.

B+

  • Guest
Re: 3 color mod of Sierpinski non recursive line triangle by Rick3137
« Reply #2 on: May 26, 2016, 05:40:37 AM »
Hi D,

What is your brute force rating for this one, I stumbled upon while looking for rnd points version:

Code: [Select]
' sier2.bas found in SmallBASIC old (discontinued) FTLK example files
' From "Fractals" by Hans Lauwerier
' Heavily modified for SB :-)
' Method 2 to draw Sierpinski Triangle

'see if I can get a bigger triangle set drawn, yes!
m45 = 180
y100 = 400
x80 = 320

c = 7 'QB color 7, 8, 9, 10, 11, 12 ends on red for 6 labels drawn <=== EDIT Levels, 6 Levels drawn, not labels
p = 5 'this sets how many levels small to go (0 based, 0 will draw 1 inner triangle
dim T(p) : a = SQR(3)
for m = 0 to p
  for n = 0 to 3 ^ m - 1
    n1 = n
    for l = 0 to m - 1
      T(l) = n1 mod 3 : n1 = n1 \ 3
    next l
    x = 0 : y = 0
    for k = 0 to m - 1
      x = x + cos((4 * T(k) + 1) * PI / 6) / 2 ^ k
      y = y + sin((4 * T(k) + 1) * PI / 6) / 2 ^ k
    next k
    u1 = m45 * (x + a / 2 ^ (m + 1)) + x80
    u2 = m45 * (x - a / 2 ^ (m + 1)) + x80
    v1 = m45 * (y - 1 / 2 ^ (m + 1)) + y100
    v2 = m45 * (y + 1 / 2 ^ m) + y100
    x = m45 * x + x80
    y = m45 * y + y100
    line u1, v1,  x, v2 COLOR c
    line  x, v2, u2, v1 COLOR c
    line u2, v1, u1, v1 COLOR c
  next n
  c = c + 1
next m
beep
pause


It is coloring each level separately, you can control how many levels with p. Does use an array.
« Last Edit: May 26, 2016, 02:53:49 PM by B+ »

Rick3137

  • Guest
Re: 3 color mod of Sierpinski non recursive line triangle by Rick3137
« Reply #3 on: May 26, 2016, 09:42:48 PM »
 Nice find, Mark . That one's hard to beat.

B+

  • Guest
Re: 3 color mod of Sierpinski non recursive line triangle by Rick3137
« Reply #4 on: May 27, 2016, 03:27:54 PM »
Hard to beat?

Rick your first doodlebot was more flexible and able to draw Sierpinski at any angle. From it, I was inspired to find an even more flexible method to draw Sierpinski inside any 3 points.

I was just looking at non recursive coding methods to compare with elegance of a recursive one, like doodlebot v1 or mine inspired by it!

In my not so humble opinion, this http://retrogamecoding.org/board/index.php?topic=453.0  beats all methods discussed recently, shown in an app to demo it's amazingly flexible properties.

Flexible, easy to use, easy to understand code... can you understand how Hans Lauwerier Method works? and that one is more rigid than the first version of doodlebot.
« Last Edit: May 27, 2016, 04:10:15 PM by B+ »