Results 1 to 8 of 8

Thread: Sierpinski triangle approximation .... using L-System :D

  1. #1
    Super Moderator Petr Schreiber's Avatar
    Join Date
    Aug 2005
    Location
    Brno - Czech Republic
    Posts
    7,129
    Rep Power
    732

    Sierpinski triangle approximation .... using L-System :D

    Hi,

    here is little example of one of possible approaches to create [wiki=Sierpinski_triangle]Sierpinski triangle[/wiki].
    I used my beloved [wiki=L-system]L-Systems[/wiki] for the job.

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


    Bye,
    Petr
    Attached Images Attached Images
    Attached Files Attached Files
    Learn 3D graphics with ThinBASIC, learn TBGL!
    Windows 10 64bit - Intel Core i5-3350P @ 3.1GHz - 16 GB RAM - NVIDIA GeForce GTX 1050 Ti 4GB

  2. #2
    thinBasic author ErosOlmi's Avatar
    Join Date
    Sep 2004
    Location
    Milan - Italy
    Age
    57
    Posts
    8,779
    Rep Power
    10

    Re: Sierpinski triangle approximation .... using L-System :D

    Great code Petr!
    Big present.
    Thanks


    PS: attached a thinBasic bundled executable for those wanting to test without installing thinBasic
    Attached Files Attached Files
    www.thinbasic.com | www.thinbasic.com/community/ | help.thinbasic.com
    Windows 10 Pro for Workstations 64bit - 32 GB - Intel(R) Xeon(R) W-10855M CPU @ 2.80GHz - NVIDIA Quadro RTX 3000

  3. #3
    thinBasic MVPs
    Join Date
    May 2007
    Location
    UK
    Posts
    1,427
    Rep Power
    159

    Re: Sierpinski triangle approximation .... using L-System :D

    I look at these triangles back when I did the fern, curves and mandelbrot.

    Nice work.
    Home Desktop : Windows 7 - Intel Pentium (D) - 3.0 Ghz - 2GB - Geforce 6800GS
    Home Laptop : WinXP Pro SP3 - Intel Centrino Duo - 1.73 Ghz - 2 GB - Intel GMA 950
    Home Laptop : Windows 10 - Intel(R) Core(TM) i5-4210U CPU @ 1.70GHz, 2401 Mhz, 2 Core(s), 4 Logical Processor(s) - 4 GB - Intel HD 4400
    Work Desktop : Windows 10 - Intel I7 - 4 Ghz - 8GB - Quadro Fx 370

  4. #4
    thinBasic MVPs kryton9's Avatar
    Join Date
    Nov 2006
    Location
    Naples, Florida & Duluth, Georgia
    Age
    67
    Posts
    3,869
    Rep Power
    404

    Re: Sierpinski triangle approximation .... using L-System :D

    That is really a neat demo. Much to study when the time comes in this one as in so many other fine examples!!
    Acer Notebook: Win 10 Home 64 Bit, Core i7-4702MQ @ 2.2Ghz, 12 GB RAM, nVidia GTX 760M and Intel HD 4600
    Raspberry Pi 3: Raspbian OS use for Home Samba Server and Test HTTP Server

  5. #5

    Re: Sierpinski triangle approximation .... using L-System :D

    This is another way to create a Sierpinski triangle.

    [code=thinbasic]
    ' -- 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[/code]
    Attached Images Attached Images
    Operating System: Windows 10 Home 64-bit
    CPU: Intel Celeron N4000 CPU @ 1.10GHz
    Memory: 4.00GB RAM
    Graphics: Intel UHD Graphics 600

  6. #6
    thinBasic MVPs
    Join Date
    May 2007
    Location
    UK
    Posts
    1,427
    Rep Power
    159

    Re: Sierpinski triangle approximation .... using L-System :D

    Same script using display lists to get more pixels.

    [code=thinbasic]
    ' -- 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

    [/code]
    Home Desktop : Windows 7 - Intel Pentium (D) - 3.0 Ghz - 2GB - Geforce 6800GS
    Home Laptop : WinXP Pro SP3 - Intel Centrino Duo - 1.73 Ghz - 2 GB - Intel GMA 950
    Home Laptop : Windows 10 - Intel(R) Core(TM) i5-4210U CPU @ 1.70GHz, 2401 Mhz, 2 Core(s), 4 Logical Processor(s) - 4 GB - Intel HD 4400
    Work Desktop : Windows 10 - Intel I7 - 4 Ghz - 8GB - Quadro Fx 370

  7. #7
    Super Moderator Petr Schreiber's Avatar
    Join Date
    Aug 2005
    Location
    Brno - Czech Republic
    Posts
    7,129
    Rep Power
    732

    Re: Sierpinski triangle approximation .... using L-System :D

    Matthew, Abraxas!,

    thanks a lot for new versions!

    Just a few sidenotes - better to use TBGL_ShowWindow right after TBGL_CreateWindowEx, and also:
    [code=thinbasic]
    ' -- Left corner, middle, right corner
    array assign x = 320, 0, 640

    ' -- Top point, left point, right point
    array assign y = 480, 0, 0
    [/code]

    can be done as less "wordy"
    [code=thinbasic]
    ' -- Left corner, middle, right corner
    x(1) = 320, 0, 640

    ' -- Top point, left point, right point
    y(1) = 480, 0, 0
    [/code]

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


    Petr
    Learn 3D graphics with ThinBASIC, learn TBGL!
    Windows 10 64bit - Intel Core i5-3350P @ 3.1GHz - 16 GB RAM - NVIDIA GeForce GTX 1050 Ti 4GB

  8. #8

    Re: Sierpinski triangle approximation .... using L-System :D

    Here is a Sierpinski snowflake.

    [code=thinbasic]
    ' -- 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[/code]
    Attached Images Attached Images
    Operating System: Windows 10 Home 64-bit
    CPU: Intel Celeron N4000 CPU @ 1.10GHz
    Memory: 4.00GB RAM
    Graphics: Intel UHD Graphics 600

Similar Threads

  1. Triangle
    By peter in forum Sources, Templates, Code Snippets, Tips and Tricks, Do you know ...
    Replies: 9
    Last Post: 23-07-2009, 22:53

Members who have read this thread: 0

There are no members to list at the moment.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •