Michael Clease
26-02-2010, 22:02
' 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 ' glClear(GL_COLOR_BUFFER_BIT)
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
'from OpenGL.GLUT import *
'from random import *
'from numpy import *
'import sys
'# Initial values of width And height
'width = 400
'height = 400
'def init():
'# White background
'glClearColor(1.0, 1.0, 1.0, 0.0)
'# Set the projection matrix... our "view"
'glMatrixMode(GL_PROJECTION)
'glLoadIdentity()
'# Set the plot Window range
'gluOrtho2D(0,10,0,6)
'# Set the matrix For the object we are drawing
'glMatrixMode(GL_MODELVIEW)
'glLoadIdentity()
'def plotpredprey():
'# habitat And population PARAMETERS
'a = 0.7
'b = 0.5
'c = 0.3
'e = 0.2
'# time increment
'dt = 0.001
'# initial populations
'x = 0.5
'y = 0.5
'glClear(GL_COLOR_BUFFER_BIT)
'For n In arange(0,10, 0.0001):
'# predator-prey equations
'x = x + (a*x - b*x*y)*dt
'y = y + (-c*y + e*x*y)*dt
'glBegin(GL_POINTS)
'# parametric plot
'#glColor3f(0.0,0.0,0.0)
'#glVertex2f(x,y)
'150
'# prey
'glColor3f(1.0, 0.0, 0.0)
'glVertex2f(n,x)
'# predator
'glColor3f(0.0, 0.0, 1.0)
'glVertex2f(n,y)
'glEnd()
'glFlush()
' 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 ' glClear(GL_COLOR_BUFFER_BIT)
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
'from OpenGL.GLUT import *
'from random import *
'from numpy import *
'import sys
'# Initial values of width And height
'width = 400
'height = 400
'def init():
'# White background
'glClearColor(1.0, 1.0, 1.0, 0.0)
'# Set the projection matrix... our "view"
'glMatrixMode(GL_PROJECTION)
'glLoadIdentity()
'# Set the plot Window range
'gluOrtho2D(0,10,0,6)
'# Set the matrix For the object we are drawing
'glMatrixMode(GL_MODELVIEW)
'glLoadIdentity()
'def plotpredprey():
'# habitat And population PARAMETERS
'a = 0.7
'b = 0.5
'c = 0.3
'e = 0.2
'# time increment
'dt = 0.001
'# initial populations
'x = 0.5
'y = 0.5
'glClear(GL_COLOR_BUFFER_BIT)
'For n In arange(0,10, 0.0001):
'# predator-prey equations
'x = x + (a*x - b*x*y)*dt
'y = y + (-c*y + e*x*y)*dt
'glBegin(GL_POINTS)
'# parametric plot
'#glColor3f(0.0,0.0,0.0)
'#glVertex2f(x,y)
'150
'# prey
'glColor3f(1.0, 0.0, 0.0)
'glVertex2f(n,x)
'# predator
'glColor3f(0.0, 0.0, 1.0)
'glVertex2f(n,y)
'glEnd()
'glFlush()