PDA

View Full Version : Sierpinski triangle approximation .... using L-System :D



Petr Schreiber
13-02-2008, 20:26
Hi,

here is little example of one of possible approaches to create Sierpinski triangle.
I used my beloved L-Systems for the job.

Code is optimized for clarity, not for performance, so beware ;)


Bye,
Petr

ErosOlmi
13-02-2008, 22:18
Great code Petr!
Big present.
Thanks


PS: attached a thinBasic bundled executable for those wanting to test without installing thinBasic

Michael Clease
13-02-2008, 22:21
I look at these triangles back when I did the fern, curves and mandelbrot.

Nice work.

kryton9
14-02-2008, 02:28
That is really a neat demo. Much to study when the time comes in this one as in so many other fine examples!!

matthew
10-03-2008, 20:20
This is another way to create a Sierpinski triangle. :)



' -- Sierpinski triangle program

' -- thinBasic OpenGL library
uses "TBGL"

' -- Create a window to display our image
dim hWnd as dword
hWnd = tbgl_createwindowex("Sierpinski Triangle", 640, 480, 16, 0)

' -- Declare & initialize any constants & variables here
dim x(3), y(3) as long
dim px, py, corner, i as long

' -- Left corner, middle, right corner
array assign x = 320, 0, 640

' -- Top point, left point, right point
array assign y = 480, 0, 0

' -- Reset status of all keys
tbgl_getasynckeystate(-1)

' -- Display our window on the screen
tbgl_showwindow

' -- TBGL setup
tbgl_rendermatrix2d
tbgl_usedepth 0
tbgl_usedepthmask 0
tbgl_clearframe

' -- Main loop
while tbgl_iswindow(hWnd)

' -- Camera position
tbgl_camera 0, 0, 1, 0, 0, 0

' -- Begin drawing using points
tbgl_beginpoly %GL_POINTS

' -- Draw 10,000 points on the screen
for i = 1 to 10000

' -- Draw our points
tbgl_vertex px, py

' -- Pick a random corner
corner = int(rnd( 1, 3))

' -- Move halfway towards the corner
px = px+(x(corner)-px)/2
py = py+(y(corner)-py)/2

next

' -- Update screen display
tbgl_drawframe

' -- Stop drawing
tbgl_endpoly

' -- Quit if Esc key pressed
if tbgl_getasynckeystate(%VK_ESCAPE) then exit while

wend

tbgl_destroywindow

Michael Clease
10-03-2008, 21:47
Same script using display lists to get more pixels. :)



' -- Sierpinski triangle program

' -- thinBasic OpenGL library
uses "TBGL"

%My_List = 1
%Title = "Sierpinski Triangle"
' -- Create a window to display our image
dim hWnd as dword
Dim WinTitle AS STRING VAlue = %Title

hWnd = tbgl_createwindowex("Sierpinski Triangle", 640, 480, 16, 0)

' -- Declare & initialize any constants & variables here
dim x(3), y(3) as long
dim px, py, corner, i as long

' -- Left corner, middle, right corner
array assign x = 320, 0, 640

' -- Top point, left point, right point
array assign y = 480, 0, 0

' -- Reset status of all keys
tbgl_getasynckeystate(-1)

' -- Display our window on the screen
tbgl_showwindow

' -- TBGL setup
tbgl_rendermatrix2d
tbgl_usedepth 0
tbgl_usedepthmask 0

BuildTriangle

' -- Main loop
while tbgl_iswindow(hWnd)

WinTitle = TBGL_GetFramerate

tbgl_clearframe

' -- Camera position
tbgl_camera 0, 0, 1, 0, 0, 0

' -- Begin drawing using points

TBGL_CallList %My_List
' -- Update screen display
tbgl_drawframe

TBGL_SetWindowTitle( hWnd, WinTitle )

' -- Quit if Esc key pressed
if tbgl_getasynckeystate(%VK_ESCAPE) then exit while

wend

tbgl_destroywindow

Sub BuildTriangle()
' -- Draw 10,000 points on the screen
TBGL_NewList %My_List
TBGL_PushMatrix
tbgl_beginpoly %GL_POINTS
for i = 1 to 100000
' -- Draw our points
tbgl_vertex px, py
' -- Pick a random corner
corner = int(rnd( 1, 3))
' -- Move halfway towards the corner
px = px+(x(corner)-px)/2
py = py+(y(corner)-py)/2
next
TBGL_ENDPOLY
TBGL_PopMatrix

TBGL_EndList
END SUB

Petr Schreiber
10-03-2008, 22:47
Matthew, Abraxas!,

thanks a lot for new versions!

Just a few sidenotes - better to use TBGL_ShowWindow right after TBGL_CreateWindowEx, and also:


' -- Left corner, middle, right corner
array assign x = 320, 0, 640

' -- Top point, left point, right point
array assign y = 480, 0, 0


can be done as less "wordy"


' -- Left corner, middle, right corner
x(1) = 320, 0, 640

' -- Top point, left point, right point
y(1) = 480, 0, 0


I must admit your sources are pretty short, with very good results, I like it!


Petr

matthew
11-03-2008, 00:08
Here is a Sierpinski snowflake. :)



' -- Sierpinski snowflake

' -- thinBasic OpenGL library
uses "TBGL"

' -- Create a window to display our image
dim hWnd as dword
hWnd = tbgl_createwindowex("Sierpinski snowflake", 640, 480, 16, 0)

' -- Display our window
tbgl_showwindow

' -- Declare & initialize any constants & variables here
dim c, r2, r3, x, y, u, v, x0, y0 as single
dim i, ex, sx, sy as long

' -- Variables for our snowflake
c = 0.5 / sqr(3)
r2 = sqr(3) / 2
r3 = sqr(3) / 6

' -- Depth location of snowflake
ex = 400

' -- Horizontal location of snowflake
sx = 115

' -- Vertical location of snowflake
sy = 125

x = 1
y = 0

' -- Reset status of all keys
tbgl_getasynckeystate(-1)

' -- TBGL setup
tbgl_rendermatrix2d
tbgl_usedepth 0
tbgl_usedepthmask 0
tbgl_clearframe

' -- Main loop
while tbgl_iswindow(hWnd)

' -- Camera position
tbgl_camera 0, 0, 1, 0, 0, 0

' Begin drawing using points
tbgl_beginpoly %GL_POINTS

' -- Draw 10,000 points on the screen
for i = 1 to 10000

if rndf(0, 1, 2) > 0.5 then
x0 = 0.5 * x + r3 * y
y0 = r3 * x - 0.5 * y
else
x0 = 0.5 * x - r3 * y + 0.5
y0 = -r3 * x -0.5 * y + r3
endif

x = x0
y = y0

' -- Draw our points on the screen
tbgl_vertex x * ex + sx, sy - y * ex

u = 0.5 * x - r2 * y
v = -r2 * x - 0.5 * y

tbgl_vertex u * ex + sx, sy - v * ex

u = -0.5 * x + r2 * y + 1
v = -r2 * x - 0.5 * y

tbgl_vertex u * ex + sx, sy - v * ex

next

' -- Update screen display
tbgl_drawframe

' -- Stop drawing
tbgl_endpoly

' -- Quit if Esc key is pressed
if tbgl_getasynckeystate(%VK_ESCAPE) then exit while

wend

tbgl_destroywindow