Results 1 to 4 of 4

Thread: Example: Section 6.4 (page 129), The Barnsley Fern

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

    Example: Section 6.4 (page 129), The Barnsley Fern

    [code=thinbasic]
    ' Example: Section 6.4 (page 129), The Barnsley Fern

    ' From Stan Blank's Book:
    ' "Python Programming in OpenGL
    ' "A Graphical Approach to Programming

    ' Converted by Michael Clease
    ' Last modified: February 26, 2010

    ' thinBasic does not use GLUT, we use instead tbgl
    Uses "TBGL"

    ' Handle for our window
    Local hWnd As DWord
    Local Width As Long
    Local Height As Long
    Local x,y As Single
    Local xx,yy As Single
    Local a,b,c As Single
    Local d,e,f As Single
    Local v As Single
    Local n As DWord
    '# Initial values of width And height
    width = 600
    height = 600
    '
    ' Create and show window
    hWnd = TBGL_CreateWindowEx("The Barnsley Fern", Width, Height, 32, %TBGL_WS_WINDOWED Or %TBGL_WS_CLOSEBOX)
    TBGL_ShowWindow

    ' Set Foreground from default White to Green
    TBGL_Color(76,152,51)
    ' Set background from default black to white
    TBGL_BackColor(255,255,255)
    ' Init OpenGl, like gluOrtho2D in example code
    TBGL_RenderMatrix2D( -3, 0, 3, 10.5 )
    ' Resets status of all keys
    TBGL_ResetKeyState()
    TBGL_PointSize 1

    ' Main loop
    While TBGL_IsWindow(hWnd)
    x = -1.5
    y = 0.75
    TBGL_ClearFrame ' glClear(GL_COLOR_BUFFER_BIT)

    For n = 0 To 100000
    v = Rnd
    If v >= 0 And v <= 0.8000 Then
    a = 0
    b = 1.6
    c = -2.5*M_PI/180
    d = -2.5*M_PI/180
    e = 0.85
    f = 0.85
    ' TBGL_Color(255,0,0)
    ElseIf v > 0.8000 And v <= 0.8050 Then
    a = 0
    b = 0
    c = 0*M_PI/180
    d = 0*M_PI/180
    e = 0
    f = 0.16
    ' TBGL_Color(0,255,0)
    ElseIf v > 0.8050 And v <= 0.9025 Then
    a = 0
    b = 1.6
    c = 49*M_PI/180
    d = 49*M_PI/180
    e = 0.3
    f = 0.34
    ' TBGL_Color(0,0,255)
    ElseIf v > 0.9025 And v <= 1.0 Then
    a = 0
    b = 0.44
    c = 120*M_PI/180
    d = -50*M_PI/180
    e = 0.3
    f = 0.37
    ' TBGL_Color(255,0,255)
    EndIf
    xx = x
    yy = y
    x = e * xx * Cos(c) - f * yy * Sin(d) + a
    y = e * xx * Sin(c) + f * yy * Cos(d) + b
    If n > 10 Then
    TBGL_BeginPoly(%GL_POINTS)
    TBGL_Vertex(x,y)
    TBGL_EndPoly
    EndIf
    Next
    TBGL_DrawFrame

    ' ESCAPE key to exit application
    If TBGL_GetWindowKeyState(hWnd, %VK_ESCAPE) Then Exit While
    Wend

    TBGL_DestroyWindow


    'Section 6.4 The Barnsley Fern
    'Exercise 9 In the previous section asked you To remember the plant-like
    'structures that were plotted when you added And modified the lines of code listed In the
    'problem. We are now going To expand On the ideas introduced In the Chaos Game
    'section To produce an graphics plot that raises the “plant-like” descriptor To a New level.
    'In Section 6.3 we used a simple rule With a random choice of vertices To produce the
    'Sierpinski Gasket attractor. In THIS section we are going To enlarge our scope And use a
    'combination of 4 sets of PARAMETERS, each one assigned a different probability of being
    'Selected At random. Once a parameter Set has been randomly chosen we’ll apply the
    'Selected PARAMETERS To our rules Or equations. The result of THIS iterated Function system
    'is both surprising And beautiful!
    'Here is the program listing For THIS section.
    '# PyBarnsleyFern.py
    'from OpenGL.GL import *
    'from OpenGL.GLU import *
    'from OpenGL.GLUT import *
    'from random import *
    'from numpy import *
    'import sys
    '# Globals For Window width And height
    'Global width
    'Global height
    '# Initial values of width And height
    'width = 600
    'height = 600
    'def init():
    '# White background
    'glClearColor(1.0, 1.0, 1.0, 0.0)
    '# Green Plot… it IS a Fern
    'glColor3f(0.3, 0.6, 0.2)
    '# Set the projection matrix... our "view"
    'glMatrixMode(GL_PROJECTION)
    'glLoadIdentity()
    '# Set the plot Window range
    'gluOrtho2D(-3.0, 3.0, 0.0, 10.5)
    '# Set the matrix For the object we are drawing
    'glMatrixMode(GL_MODELVIEW)
    'glLoadIdentity()
    'def plotfunc():
    '# Choose an initial point... Any point
    '# You can Randomize THIS If you wish
    'x = -1.5
    'y = 0.75
    'glClear(GL_COLOR_BUFFER_BIT)
    '# Plot 100000 points. THIS Number is very large.
    '# Feel free To experiment With smaller values.
    'For n In range(0,100000):
    '# n allows us To reject the first few points
    '# To give the attractor a chance To Do its “thing”
    '# Choose a random Value Between 0 And 1 And
    '# Then Select a Set of PARAMETERS based On THIS Value.
    'v = random()
    'If v >= 0 And v <= 0.8000:
    'a = 0
    'b = 1.6
    'c = -2.5*Pi/180
    'd = -2.5*Pi/180
    'e = 0.85
    'f = 0.85
    '#glColor3f(1.0, 0.0, 0.0)
    'elif v > 0.8000 And v <= 0.8050:
    'a = 0
    'b = 0
    'c = 0*Pi/180
    'd = 0*Pi/180
    'e = 0
    'f = 0.16
    '#glColor3f(0.0, 1.0, 0.0)
    'elif v > 0.8050 And v <= 0.9025:
    'a = 0
    'b = 1.6
    'c = 49*Pi/180
    'd = 49*Pi/180
    'e = 0.3
    'f = 0.34
    '#glColor3f(0.0, 0.0, 1.0)
    'elif v > 0.9025 And v <= 1.0:
    'a = 0
    'b = 0.44
    'c = 120*Pi/180
    'd = -50*Pi/180
    'e = 0.3
    'f = 0.37
    '#glColor3f(1.0, 0.0, 1.0)
    '# Save the old values of x And y so we can Iterate
    '# those values according To the chosen PARAMETERS
    '# And rules.
    'xx = x
    'yy = y
    '# Apply the PARAMETERS To the rule equations
    'x = e * xx * Cos(c) - f * yy * Sin(d) + a
    'y = e * xx * Sin(c) + f * yy * Cos(d) + b
    '# Start plotting after the 10th point
    'If n > 10:
    'glBegin(GL_POINTS)
    'glVertex2f(x,y)
    'glEnd()
    'glFlush()
    'def keyboard(key, x, y):
    '# Allows us To quit by pressing 'Esc' or 'q'
    'If key == chr(27):
    'sys.Exit()
    'If key == "q":
    'sys.Exit()
    'def main():
    'Global width
    'Global height
    'glutInit(sys.argv)
    'glutInitDisplayMode(GLUT_RGB|GLUT_SINGLE)
    'glutInitWindowPosition(100,100)
    'glutInitWindowSize(width,height)
    'glutCreateWindow("The Chaos Game... Fern!")
    'glutDisplayFunc(plotfunc)
    'glutKeyboardFunc(keyboard)
    'init()
    'glutMainLoop()
    'main()
    '#End of Program
    [/code]
    Attached Files Attached Files
    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

  2. #2
    Member sblank's Avatar
    Join Date
    Feb 2010
    Location
    Wayne City, Illinois
    Posts
    80
    Rep Power
    23

    Re: Example: Section 6.4 (page 129), The Barnsley Fern

    Very nice... I enjoy the shifting frond patterns!

    Stan


  3. #3
    thinBasic MVPs kryton9's Avatar
    Join Date
    Nov 2006
    Location
    Naples, Florida & Duluth, Georgia
    Age
    68
    Posts
    3,865
    Rep Power
    405

    Re: Example: Section 6.4 (page 129), The Barnsley Fern

    That is amazing to think about how to come up with the code for that one!
    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

  4. #4
    Member sblank's Avatar
    Join Date
    Feb 2010
    Location
    Wayne City, Illinois
    Posts
    80
    Rep Power
    23

    Re: Example: Section 6.4 (page 129), The Barnsley Fern

    Yea... that would be Michael Barnsley... he came up with an algorithm to create fractal forgeries of almost anything. His book "Fractals Everywhere" is a bit difficult to understand... the math get a little dense at times, but his fern algorithm is really good.

    Stan

    Quote Originally Posted by kent sarikaya
    That is amazing to think about how to come up with the code for that one!

Similar Threads

  1. Example: Section 5.4 (page 41), Combined with Exercise 1 (page 44)
    By kryton9 in forum ThinBASIC programming in OpenGL/TBGL
    Replies: 2
    Last Post: 26-02-2010, 20:46

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
  •