Grátis
433 pág. ## Pré-visualização | Página 13 de 50

```* may be
necessary instead of Numeric. In either case, you may have to install the library before you can
run this program.
3 Several exercises will require changes in the domain and range in gluOrtho2D
43
“let x take on all values from -5.0 to +5.0, stepping by 0.1”.4 The next line, y = x*x
takes each value for x in the specified range, squares that value, and stores the result in
the variable y.5 As expected, the glVertex2f(x,y) command plots both the x and y
points for us.6 As in the previous program, we close glBegin() with glEnd() and
glFlush() causes the graphics (heretofore stored only in the computer's memory) to
be “flushed” to the screen. The def main() function containing the GLUT statements
is very similar to those in the program in the previous chapter.

Before we do some exercises based on this program example, let's add some
coordinate axes for visual reference.7 Directly below the glPointSize(3.0)

glBegin(GL_LINES)
glVertex2f(-5.0, 0.0)
glVertex2f(5.0, 0.0)
glVertex2f(0.0, 5.0)
glVertex2f(0.0, -5.0)
glEnd()

Now when the program is executed, we see the graph of the function y = x2 in a
more familiar setting as shown in Figure 5.4.

Figure 5.4

The GL_LINES parameter in the new glBegin() statement takes pairs of points
in glVertex2f(x,y) format and draws a straight line between those points. In this
example, the x-axis is drawn first from glVertex2f(-5.0,0.0) to
glVertex2f(5.0,0.0) and the y-axis is drawn from glVertex2f(0.0,5.0) to
glVertex2f(0.0,-5.0). You might try plotting the function using a different color for
contrast.

4 Actually, both the range and arange commands stop one step less than the higher number.
In this case, 4.9 is the last value for x. You might try replacing the "0.1" step with "0.01"?
5 You could also type “y = x**2”. Recall how we cubed a number in the “Super-3” program?
6 Remember that all indented lines after the for statement are included in the loop.
7 As in Exercise 10 in the previous section.
44
Here are some comments on representing functions using computer statements.
If we want to graph the function f(x) = 2x3 - 3x2 + x - 5, we simply can't type the equation
into Python as it appears in our textbook. We must translate the function into a form that
is understood by Python. Unless you are using a computer algebra system such as
Mathematica, all such equations require translation regardless of the computer language
you are using. In this case, we translate f(x) = 2x3 - 3x2 + x - 5 as follows:

y = 2*x**3 - 3*x**2 + x - 5

This isn't too difficult. Notice that we must specifically tell Python to multiply and
raise x to a power. We can't type 2x and expect Python to understand that this means
"2 times x". In Python, 2x is written 2*x. As long as you keep in mind that every
operation must be specifically typed and that the order of arithmetic must be strictly
obeyed, then you shouldn't have too many problems.

One final note: In Python (or in most languages), the "=" sign has a couple of
meanings. First, the "=" sign may act as an assignment statement. The equation x =
x + 1 has a nonsensical meaning in algebra. How can x equal itself plus one? In
Python, this statement is translated: Add one to x and then assign, store, or place the
new value back into x. In other words, the new value of x will be the old value of x plus
one. This creates a counting device! So, anytime Python encounters an "=" sign, it will
evaluate the right hand side of the "=" sign first and then assign that value to the variable
on the left side of the "=" sign. The second use of an "=" sign is in comparisons such as
an if statement. In this usage, we employ a "==" sign to distinguish it from a simple
assignment statement. As an example, we might say: if x == 3: meaning that if x
equals 3, then we'll do something important.8 If you forget and use "=" in an if
statement rather than "==", the Python interpreter will generate an error message.
Exercises

1) Graph the following equations. Don't forget to translate the equations into Python!

a) y = x2 - 2
b) y = x3 - 3x - 1
c) y = x4 - 5x3 + x2 - 3x - 1
d) y = sin(x)
e) y = sin(3x)
f) y = sin(x/3)
g) y = cos(x)

2) How would you identify the roots (if they exist) of the functions in Exercise 1?

3) Experiment with the glPointSize() statement. Does this statement apply only to
the points plotted or does it apply to the x and y axis lines as well?

8 The idea of exact equality introduces the possibility of some interesting side-effects. Floating
point values are represented by binary numbers (base 2) internally, so if we pose the conditional
if x == 2:, which is an integer with an exact binary representation and then check to see if
x == 2.0:, which is a floating point value with a (perhaps) inexact representation, we may not
get the expected result. This is only an example, but please keep this idea in mind for later use.
45
4) Replace GL_POINTS with GL_LINES in the glBegin() statement above the
function plot section of the program. What happens? Why does this happen? What
about glPointSize() mentioned in exercise 2?

5) Replace GL_LINES with GL_LINE_STRIP in exercise 3. What happens?

6) Experiment with GL_LINES and glLineWidth(2.0) in place of glPointSize().9
Try various line widths, including decimal values.

7) Change GL_LINES back to GL_POINTS. Experiment with the arange command.
Change the arange(-5.0, 5.0, 0.1) to a arange(-10.0, 10.0, 0.01).
You may have to edit the gluOrtho2D command in the init() function for this
change to display properly. Now try a step (in arange) of 0.001 followed by a step
of 0.0001. The smaller the step, the nicer the plot… but it can take a bit longer to
draw because you are plotting more points. You can also modify the step size while
using GL_LINES for a smoother or coarser solid curve.

8) See if you can plot multiple functions in the same graphics window. You might
consider something like this in def plotfunc():

for x in arange(-5.0 ,5.0, 0.01):
y = x*x
a = x + 1
glBegin(GL_POINTS)
glVertex2f(x, y)
glVertex2f(x, a)
glEnd()

which should give you a result something like Figure 5.5. Try some other functions
and see where points of intersection occur.

Figure 5.5

9 Remember, glLineWidth only works with lines. glPointSize works only with points!
46
9) Plot y = sin(x) and y = cos(x) on the same graph. Use different colors for
each function and also include coordinate axes for reference. What do you notice
about the two function plots? How are sin and cos related? How are they
different?

10) Building on Exercise 8, can you draw a circle? The equation for a circle is:

x2 + y2 = r2 or y = sqrt(r*r – x*x) or y = sqrt(r**2 – x**2)

You can't enter the traditional implicit equation for a circle as shown in the first
equation above. You MUST use the second or third explicit form, y = sqrt(r*r –
x*x) or y = sqrt(r**2 – x**2). But how would you go about
accomplishing the feat of drawing a complete circle? A circle is NOT a function
solution. Hint:

r = 1.0
for x in arange(-1.0, 1.0, 0.01):
y = sqrt(r**2 – x**2)
glBegin(GL_POINTS)
glVertex2f(x, y)
# do we need another glVertex2f statement here?
glEnd()

Notice that I've added an r = 1.0 statement above the glBegin. Why?
Also notice that I've changed the for loop from:

for x in arange(-5.0, 5.0, 0.01):

to:

for```   