A maior rede de estudos do Brasil

433 pág.
Apostila PyOpenGL

Pré-visualização | Página 12 de 50

out15 the glPointSize(2.0) command introduced in 
exercise 3. Change the glBegin(GL_POINTS) to glBegin(GL_LINES) and 
plot two different points. What happens? See if you can draw a triangle. 
Uncomment the glPointSize line. Does anything change? Comment this line 
7) Experiment: Replace GL_LINES with GL_LINE_STRIP and plot the points (0.0, 
0.0), (1.0, 1.0), and (-1.0, 1.0). What happens? Now try GL_LINE_LOOP with 
the same three points. Was the result the same? 
8) Place the command glLineWidth(3.0) just below the commented 
glPointSize(2.0) command. Run the program using any of the GL_LINES, 
9) Experiment: Try to draw an equilateral triangle using three different colors, one 
for each side. 
10) Experiment: Try to draw a set of coordinate x and y axes using GL_LINES. 
14 To insert a blank line, you can place the cursor at the end of the line prior to glBegin and 
press Enter. You can also place the cursor at the beginning of glBegin and press Enter. 
15 Using a # at the beginning of a line is a great way to temporarily disable a command. 
11) Using the coordinate axes from Exercise 10 as a reference, see if you can plot 
“mirror” image points simply by changing the signs of your ordered pairs. Here is 
an example def plotpoints(): function that can serve as a model. Note the 
use of variables to “store” the value of the ordered pairs. Make certain that you 
set a background color other than black. Why and where would you do this? 
 def plotpoints(): 
 # First draw x and y axes 
 # Using black as a color 
 glColor3f(0.0, 0.0, 0.0) 
 glVertex2f(-1.0, 0.0) 
 glVertex2f(0.0, 1.0) 
 glVertex2f(0.0, -1.0) 
 # Store an ordered pair in variables 
 x = 0.5 
 y = 0.5 
 # Plot points in bright red 
 glColor3f(1.0, 0.0, 0.0) 
 # Increase the point size 
 # Plot the point 
 glVertex2f(x, y) 
 # Plot the mirror image or reflection of the point 
 # in the x axis. Note the sign change! 
 glVertex2f(x, -y) 
 # End of plotFunc() 
 Figure 5.1 illustrates the simple symmetry from the above code. Note how the 
points at (0.5, 0.5) and (0.5, -0.5) are reflections of each other across the x axis. 
 Figure 5.1 
 Can you reflect across the y axis? How would the signs in the ordered pairs 
change for a y axis reflection? Can you find a way to reflect across the y = x 
diagonal line? You can add a y = x diagonal by adding the following lines of code 
within the glBegin(GL_LINES) section in def plotpoints(): 
 glVertex2f(1.0, 1.0) 
 Here is an example of a symmetry or reflection across the y = x diagonal. 
 Figure 5.2 
Can you reproduce Figure 5.2 yourself? 
Now for the challenge. How many other symmetries or mirror images can you 
create using this idea? Try to predict the outcome before you run the program. 
Symmetry is an interesting and valuable concept in math, science, and computer 
12) Expand the concept of point symmetry and see if you can produce a plot illustrating 
the reflection of a triangle (using GL_LINES) across the x axis. If you are 
successful, try the y axis as well. Finally, for a challenge, reflect the triangle across 
the y = x axis. 
13) Create something on your own by plotting various lines and points in different sizes 
and colors. You may actually create a work of art! Make certain that you THINK 
about and PREDICT the results before you run the program. This is a scientific 
method. You establish a thoughtful (theory) prediction (hypothesis) and you test the 
prediction by running the program (experiment). If the program doesn't behave as 
you expect, then your hypothesis was incorrect and you think about the problem 
some more. Eventually, through thought, prediction, and testing, your program will 
work (probably). 
Note: You may have realized this by now, but the most common errors in programming 
(thus far) involve spelling, capitalization, forgetting to close parentheses. If you generate 
an error in your program and you can't figure out where the error is in the program 
statement, look at the line(s) ABOVE the error. It may be that you have forgotten a 
closing parenthesis somewhere in the line or lines preceding the error.
Section 5.2 Plotting 2D Functions 
 Let's make Python and OpenGL do something a bit more useful. One of the 
tasks that most algebra students dislike is graphing functions.1 With some minor 
modifications, we can use the program in the last section to create our own function 
plotter. We'll start with this code: 
# 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(): 
 glColor3f(0.0, 0.0, 0.0) 
 for x in arange(-5.0, 5.0, 0.1): 
 y = x*x 
 glVertex2f(x, y) 
def main(): 
 glutCreateWindow("Function Plotter") 
# End of program 
1 I didn't like it either! I wish I had Python and OpenGL… or even a computer, "way back then". 
 Save your program and try running it. You should see something like Figure 5.3. 
We are obviously trying to plot a parabola, but how is our program managing such a 
feat? First, you should notice that we've added a "from numpy import *" to our 
import statements.2 This command adds some additional important math statements to 
Python for our use, as we'll see in a moment. In our program listing, we've changed the 
gluOrtho2D command so that the x and y ranges are from -5.0 to +5.0 to enlarge the 
domain and range of the "canvas" on which we are plotting. You may change these 
values as needed for specific plots.3 For example, we may need a range of -50.0 to 50.0 
to see large scale details in a function or we may need a range of -0.25 to 0.25 to view 
intricate details of a complex graph near the origin. gluOrtho2D allows us to employ 
such domain and range options with great flexibility! 
 Figure 5.3 
The major changes in this program are in the display function plotfunc(). The 
background color is white, the plot color is set to black (where and how?), and the size of 
the points has been increased to 3.0 (what line does this?). The "meat" of the program 
is found in the following section: 
 for x in arange(-5.0, 5.0, 0.1): 
 y = x*x 
 glVertex2f(x, y) 
 After glBegin(GL_POINTS), we see a for loop similar to the one used in the 
“Super-3” program. Instead of using range, though, we use arange which allows us 
some freedom to choose how our x axis values are chosen. The range command we 
used previously works only with a list of integers. We need to be able to use decimal 
numbers in order to create smoother plots. Remember the from numpy import * 
statement? That particular module provides us with the arange command used in this 
section of code. The for x in arange(-5.0, 5.0, 0.1): statement translates to 
2 The newest Python Numeric module is called numpy, so from numpy import

Crie agora seu perfil grátis para visualizar sem restrições.