View Full Version : Example: Section 5.2 (page 41), Plotting 2D Functions
Petr, has two improved and optimized versions further along in this thread. Please refer to those.
Original Post, outdated now!
I'm going to need help on this one. My plot is not coming out like the one in the book, picture 5.3 on page 42.
' Example 5.25 Plotting 2D Functions
' From Stan Blank's Book:
' "Python Programming in OpenGL
' "A Graphical Approach to Programming
' Converted by Kent Sarikaya
' Last modified: February 25, 2010
' thinBasic does not use GLUT, we use instead tbgl
Uses "TBGL"
' Handle for our window
Local hWnd As DWord
' Create and show window
hWnd = TBGL_CreateWindowEx("Function Plotter", 400, 400, 32, %TBGL_WS_WINDOWED Or %TBGL_WS_CLOSEBOX)
TBGL_ShowWindow
' Init OpenGl, like gluOrtho2D in example code
TBGL_RenderMatrix2D( -5, 5, -5, 5 )
' Set background from default black to white
TBGL_BackColor(255,255,255)
' Resets status of all keys
TBGL_ResetKeyState()
Local x,y As Single
' Main loop
While TBGL_IsWindow(hWnd)
TBGL_ClearFrame
TBGL_Color(0,0,0)
TBGL_PointSize 3
For x = -5 To 5 Step 0.1
y = x*x
' Like glBegin(GL_POINTS) in example code
TBGL_BeginPoly(%GL_POINTS)
TBGL_Vertex(x,y)
TBGL_EndPoly
Next
TBGL_DrawFrame
' ESCAPE key to exit application
If TBGL_GetWindowKeyState(hWnd, %VK_ESCAPE) Then Exit While
Wend
TBGL_DestroyWindow
'Python source page 41
'# PyFunc.py
'# Plotting functions
'from OpenGL.GL import *
'from OpenGL.GLU import *
'from OpenGL.GLUT import *
'from numpy import *
'import sys
'def init():
' glClearColor(1.0, 1.0, 1.0, 1.0)
' gluOrtho2D(-5.0, 5.0, -5.0, 5.0)
'def plotfunc():
' glClear(GL_COLOR_BUFFER_BIT)
' glColor3f(0.0, 0.0, 0.0)
' glPointSize(3.0)
' For x In arange(-5.0, 5.0, 0.1):
' y = x*x
' glBegin(GL_POINTS)
' glVertex2f(x, y)
' glEnd()
' glFlush()
'def main():
' glutInit(sys.argv)
' glutInitDisplayMode(GLUT_SINGLE|GLUT_RGB)
' glutInitWindowPosition(50,50)
' glutInitWindowSize(400,400)
' glutCreateWindow("Function Plotter")
' glutDisplayFunc(plotfunc)
' init()
' glutMainLoop()
'main()
'# End of program
Petr Schreiber
26-02-2010, 10:14
Just little problem Kent,
try to use this:
TBGL_RenderMatrix2D( -5, -5, 5, 5 )
instead of what you have in code.
TBGL_RenderMatrix2D has lower left corner specified first, and then top right one.
You will see the plot will look the same as in the book.
I must GOTO school now, will be back by night,
have fun!
Petr
Very punny Petr :D
I didn't think GOTO was legal anymore?
Fantastic work, both of you! I am hoping to get some studying in over the weekend so that I can start contributing a bit.
This is great... thanks!
Stan
...I must GOTO school now, will be back by night,
have fun!
Petr
ErosOlmi
26-02-2010, 16:14
I didn't think GOTO was legal anymore?
In thinBasic (even if it is a Basic language) it is VERY ILLEGAL :D
ErosOlmi
26-02-2010, 16:47
In this example if you resize TBGL window you lose you plotting.
Problem not related to the exercise but would be nice to have it right.
Ciao
Eros
Petr Schreiber
26-02-2010, 17:37
Eros,
good point!
This can be easily fixed by putting the TBGL_RenderMatrix2D inside the loop.
I also added one more tweak - it is slightly more efficient to call the TBGL_BeginPoly/TBGL_EndPoly just once and then fill it with vertices in the loop:
' Example 5.25 Plotting 2D Functions
' From Stan Blank's Book:
' "Python Programming in OpenGL
' "A Graphical Approach to Programming
' Converted by Kent Sarikaya, minor tweaks by Petr Schreiber
' Last modified: February 25, 2010
' thinBasic does not use GLUT, we use instead tbgl
Uses "TBGL"
' Handle for our window
Local hWnd As DWord
' Create and show window
hWnd = TBGL_CreateWindowEx("Function Plotter", 400, 400, 32, %TBGL_WS_WINDOWED Or %TBGL_WS_CLOSEBOX)
TBGL_ShowWindow
' Set background from default black to white
TBGL_BackColor(255,255,255)
' Set rendering color to black
TBGL_Color(0,0,0)
' Set point size to 3
TBGL_PointSize 3
' Resets status of all keys
TBGL_ResetKeyState()
Local x,y As Single
' Main loop
While TBGL_IsWindow(hWnd)
TBGL_ClearFrame
' Init OpenGl, like gluOrtho2D in example code
TBGL_RenderMatrix2D( -5, -5, 5, 5 )
' Like glBegin(GL_POINTS) in example code
TBGL_BeginPoly(%GL_POINTS)
For x = -5 To 5 Step 0.1
y = x*x
TBGL_Vertex(x,y)
Next
TBGL_EndPoly
TBGL_DrawFrame
' ESCAPE key to exit application
If TBGL_GetWindowKeyState(hWnd, %VK_ESCAPE) Then Exit While
Wend
TBGL_DestroyWindow
'Python source page 41
'# PyFunc.py
'# Plotting functions
'from OpenGL.GL import *
'from OpenGL.GLU import *
'from OpenGL.GLUT import *
'from numpy import *
'import sys
'def init():
' glClearColor(1.0, 1.0, 1.0, 1.0)
' gluOrtho2D(-5.0, 5.0, -5.0, 5.0)
'def plotfunc():
' glClear(GL_COLOR_BUFFER_BIT)
' glColor3f(0.0, 0.0, 0.0)
' glPointSize(3.0)
' For x In arange(-5.0, 5.0, 0.1):
' y = x*x
' glBegin(GL_POINTS)
' glVertex2f(x, y)
' glEnd()
' glFlush()
'def main():
' glutInit(sys.argv)
' glutInitDisplayMode(GLUT_SINGLE|GLUT_RGB)
' glutInitWindowPosition(50,50)
' glutInitWindowSize(400,400)
' glutCreateWindow("Function Plotter")
' glutDisplayFunc(plotfunc)
' init()
' glutMainLoop()
'main()
'# End of program
Petr
Petr Schreiber
26-02-2010, 17:51
Here is version which uses rendering callback,
just like in GLUT:
' Example 5.25 Plotting 2D Functions
' From Stan Blank's Book:
' "Python Programming in OpenGL
' "A Graphical Approach to Programming
' Converted by Kent Sarikaya, minor tweaks by Petr Schreiber
' Last modified: February 25, 2010
' thinBasic does not use GLUT, we use instead tbgl
Uses "TBGL"
' -- Function where program begins
Function TBMain()
' Handle for our window
Local hWnd As DWord
' Create and show window
hWnd = TBGL_CreateWindowEx("Function Plotter", 400, 400, 32, %TBGL_WS_WINDOWED Or %TBGL_WS_CLOSEBOX)
TBGL_ShowWindow
Init()
' Resets status of all keys
TBGL_ResetKeyState()
' Define "Callback" to be fired + that it should be fired each 40ms
TBGL_BindPeriodicFunction(hWnd, "PlotFunc", 40)
' -- Once the command below is executed, further script execution
' -- is halted and only periodic calling of the bound function is performed
TBGL_ProcessPeriodicFunction(hWnd)
' -- Close window, if we have quite periodic function
TBGL_DestroyWindow
End Function
Sub Init()
' Set background from default black to white
TBGL_BackColor(255,255,255)
' Set rendering color to black
TBGL_Color(0,0,0)
' Set point size to 3
TBGL_PointSize 3
End Sub
Sub PlotFunc()
' -- Which window does call?
Local hWnd As DWord = TBGL_CallingWindow
Local x,y As Single
TBGL_ClearFrame
' Init OpenGl, like gluOrtho2D in example code
TBGL_RenderMatrix2D( -5, -5, 5, 5 )
' Like glBegin(GL_POINTS) in example code
TBGL_BeginPoly(%GL_POINTS)
For x = -5 To 5 Step 0.1
y = x*x
TBGL_Vertex(x,y)
Next
TBGL_EndPoly
TBGL_DrawFrame
' ESCAPE key to disable callback
If TBGL_GetWindowKeyState(hWnd, %VK_ESCAPE) Then
TBGL_UnBindPeriodicFunction( hWnd )
Exit Sub
End If
End Sub
'Python source page 41
'# PyFunc.py
'# Plotting functions
'from OpenGL.GL import *
'from OpenGL.GLU import *
'from OpenGL.GLUT import *
'from numpy import *
'import sys
'def init():
' glClearColor(1.0, 1.0, 1.0, 1.0)
' gluOrtho2D(-5.0, 5.0, -5.0, 5.0)
'def plotfunc():
' glClear(GL_COLOR_BUFFER_BIT)
' glColor3f(0.0, 0.0, 0.0)
' glPointSize(3.0)
' For x In arange(-5.0, 5.0, 0.1):
' y = x*x
' glBegin(GL_POINTS)
' glVertex2f(x, y)
' glEnd()
' glFlush()
'def main():
' glutInit(sys.argv)
' glutInitDisplayMode(GLUT_SINGLE|GLUT_RGB)
' glutInitWindowPosition(50,50)
' glutInitWindowSize(400,400)
' glutCreateWindow("Function Plotter")
' glutDisplayFunc(plotfunc)
' init()
' glutMainLoop()
'main()
'# End of program
Petr
ErosOlmi
26-02-2010, 18:25
I would say TBGL_BindPeriodicFunction function is great.
Agreed!
Nice job!
Stan
I would say TBGL_BindPeriodicFunction function is great.
Thanks Petr, I was hoping that you will show us optimizations and to fix things like my plot not being the same.
So thanks for helping and showing them when you can. That bind function command is really neat. Another new one I had no idea that existed.
On page 44, exercise 2... how would you write a program to find the roots. I did a search on the web for what a root is, its been many decades since any math for me in school, so I totally forgot how to do these. I read you solve the equation for it equaling zero, but none of the descriptions made sense to me in how they were factoring the equation.
Here is what I looked at:
http://www.intmath.com/Equations-of-Higher-Degree/3_Factors-Roots.php
I am lost when I read this:
The cubic polynomial f(x) = 4x3 − 3x2 − 25x − 6 has degree 3 (since the highest power of x that appears is 3).
This polynomial can be factored (using Scientific Notebook or similar software) and written as
4x3 − 3x2 − 25x − 6 = (x − 3)(4x + 1)(x + 2)
So we see that a 3rd degree polynomial has 3 roots.
The associated polynomial equation is formed by setting the polynomial equal to zero:
f(x) = 4x3 − 3x2 − 25x − 6 = 0
In factored form, this is:
(x − 3)(4x + 1)(x + 2) = 0 How in the world did they get this from the line above? And how would you even code something to do that?
I still don't understand the description I put above.
But in reading more sites, I think I got the idea somewhat.
So example 1a: y = x2 - 2
A root is a solution to a problem. It is when the equation evaluates to zero.
So y = x2 - 2 we subtract y from both sides:
y-y = x2 -2 -y this then becomes 0 = x2 -2 -y
So this last equation is the solution.
Now what, do you just put values in and see which ones work?
Just sort of guessing, I came up with:
x=2 , y =2 : x^2 -2 -y : 2^2 -2 -2 : 4 - 2 - 2 : 2 - 2 = 0
I guess this becomes the brute force method :)
Hi Kent,
You are correct that a root is when the value of the function, f(x) = 0. That would occur when the function crosses the x axis. For linear functions, the solution is relatively easy (as long as the function is not parallel to the x axis). For quadratic functions, the quadratic formula works well. For higher order functions, things get a bit more complicated and although there exists a cubic formula, it's fairly complicated. You can use Calculus and Newton's Method for a numeric solution and there are other tricks of the trade...
But what I usually suggest to my students is to graph the function and have the code look for a change in sign. When a function goes from + to - or - to +, it has crossed the x axis and real root exists nearby. This won't find complex roots, but it will approximate real roots. The smaller the step increment, the more closely you can approximate the root... I even had a student write a program that when it found a sign change, it kept going back and forth, decreasing the interval between sign changes, until it found the root to the precision of the declared variable type.
Does that help any?
Cheers,
Stan
I still don't understand the description I put above.
But in reading more sites, I think I got the idea somewhat.
So example 1a: y = x2 - 2
A root is a solution to a problem. It is when the equation evaluates to zero.
So y = x2 - 2 we subtract y from both sides:
y-y = x2 -2 -y this then becomes 0 = x2 -2 -y
So this last equation is the solution.
Now what, do you just put values in and see which ones work?
Just sort of guessing, I came up with:
x=2 , y =2 : x^2 -2 -y : 2^2 -2 -2 : 4 - 2 - 2 : 2 - 2 = 0
I guess this becomes the brute force method :)
Thanks Stan for further explanation. Perhaps as I go through the book a light bulb will light up, still in the dark, but at least I got an idea that if we see a plot cross the X axis, that means a root exists.
That's the idea, Kent. The power of the function tells us how many roots the equation has. If x^5 is the largest power, then there are 5 roots. However, some of these roots may not be real roots... in that case, the function would not cross the x axis at those roots. Only if the function crosses the x axis will we have a real root.
Later in the text we'll create a graph (using Eros's iComplex library) for the function f(x) = x^3 - 1 and we'll find the roots of this function using Newton's Method and the complex plane. The function has only one real root, but it also has two complex roots and using Eros's library, we can visualize them. It's really neat AND pleasing to the eye AND it's a fractal (and I have to learn how to write the script in ThinBasic).
Cheers,
Stan
Thanks Stan for further explanation. Perhaps as I go through the book a light bulb will light up, still in the dark, but at least I got an idea that if we see a plot cross the X axis, that means a root exists.
Looking forward to your example Stan. Have fun learning thinBasic. I am still learning each day. Petr helped me with a tough problem I ran into last night. Hopefully now I can finish the program for posting soon.
I'm trying... I have yet to shed my Python and FreeBasic thinking, but I'm working on it!
I want to use the new iComplex module Eros added... I'm going to spend time tonight working on something.
Cheers,
Stan
Looking forward to your example Stan. Have fun learning thinBasic. I am still learning each day. Petr helped me with a tough problem I ran into last night. Hopefully now I can finish the program for posting soon.
If you copy and paste equations from Stan's book into the plotter, there is something in pdf that puts in a strange character for " - ", it looks like |, but thicker in the pasted version. Just erase the strange character and re-enter " - " without the quotes. This so far only seems to happen with " - " and not with " + ".