PDA

View Full Version : Example: Section 6.6 (page 148), Predator-prey Relationships



Michael Clease
27-02-2010, 03:23
' Example: Section 6.6 (page 148), Predator-prey Relationships

' 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 Double
Local a,b,c As Double
Local e,dt As Double
Local v, n As Double
'# Initial values of width And height
width = 400
height = 400
'
' Create and show window
hWnd = TBGL_CreateWindowEx("Predator-prey Relationships", 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( 0, 0, 10, 6 )
' Resets status of all keys
TBGL_ResetKeyState()
TBGL_PointSize 1

dt = 0.001
' Main loop
While TBGL_IsWindow(hWnd)

a = 0.7
b = 0.5
c = 0.3
e = 0.2
x = 0.5
y = 0.5

TBGL_ClearFrame

For n = 0 To 10 Step 0.0001
'# predator-prey equations
x = x + (a*x - b*x*y)*dt
y = y + (-c*y + e*x*y)*dt
TBGL_BeginPoly(%GL_POINTS)
TBGL_Color(255,0,0)
TBGL_Vertex(n,x)
TBGL_Color(0,0,255)
TBGL_Vertex(n,y)
TBGL_Color(0,0,0)
TBGL_Vertex(x,y)
TBGL_EndPoly
Next
TBGL_DrawFrame

dt += 0.0001
If dt > 0.005 Then dt = 0.001

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

TBGL_DestroyWindow

kryton9
27-02-2010, 04:56
This is a neat one in that you get quite a complex looking sequence with short easy to follow code. I like the name of the example too.