Homework 3

DUE: Thursday, 24 September, in class

Create an interactive program with dynamically-added, moving objects.

The basic behavior is: whenever the user clicks a mouse button, or moves the mouse with a button pressed, a new object appears at the mouse location. The object should have random color and/or movement.

The motion should have some discernable pattern to it - the direction could change over time; the objects could start following the mouse; they could orbit the center of the window; etc.

Other possible features: use different mouse buttons to add different shapes (or ones with different behavior); animate the objects colors; have objects disappear after a certain amount of time; ...






Randomness

We often would like the behavior of a program to change or be unpredictable - to not be exactly the same every time we run it.

In other cases, randomness helps eliminate unwanted artifacts, such as obvious, excessively regular or repeated patterns. These include patterns in the appearance and in the movement of objects.






Randomness

Things that can be randomized include:






Random Number Functions

Randomness is added to programs by using random numbers. These are generated by random number functions.

Python has a package called random.
Two of the functions it provides are:

random()
Returns a random floating point number between 0 and 1.
uniform(a,b)
Returns a random floating point number between a and b.





Random Number Functions

In C, the standard random number functions are:

random()
Returns a random integer between 0 and RAND_MAX
drand48()
Returns a random float between 0 and 1





Distributions

Random numbers can come in different distributions - how frequently the various numbers occur.

e.g. rolling a fair die many times will yield roughly the same number of 1s, 2s, 3s, 4s, 5s, and 6s.
A loaded die can produce one number more frequently.

random() & uniform() return uniform distributions - each possible number is equally likely to be returned.
If you call the function a large number of times, then you can expect each possible number to be returned about (but not exactly) the same number of times.
(Flipping a coin 1,000,000 times, you would expect to get roughly 500,000 heads and 500,000 tails.)

This plot is from calling int(random() * 100) 100,000 times. It shows how many times each of the possible values (from 0 to 99) was returned.






Distributions

Sometimes we want a different distribution.

A common distribution is the bell curve (or gaussian distribution). In this distribution, one small range of numbers is returned more frequently than others, with values further from the center of the distribution returned less and less frequently.






Gaussian Distribution

The Python function random.gauss(center, deviation) returns random numbers in a gaussian distribution.

The first argument (center) is the central number that the return values will be clustered around. The second argument (deviation) is the standard deviation of the distribution - this measures how broad the bell curve is; roughly 2/3 of all returned values will be within +/- deviation of the center value.

Example: a gaussian distribution lets you place objects randomly, but clustered about a center.

UniformGaussian





shuffle

Python's random.shuffle(list) randomly re-orders a list (in place).

>>> letters = ['a', 'b', 'c', 'd', 'e', 'f']
>>> letters
['a', 'b', 'c', 'd', 'e', 'f']
>>> random.shuffle(letters)
>>> letters
['f', 'e', 'd', 'c', 'b', 'a']
>>> random.shuffle(letters)
>>> letters
['c', 'b', 'a', 'd', 'e', 'f']





Classes

Classes are a central feature of object-oriented programming (OOP)

A class contains both data and functions to operate on that data

Classes are used to encapsulate related data & code - parts of the program that support one specific aspect of the program

Objects are "instances" of a class






Python Classes

A class in Python is created with the class command

Functions in a Python class always have as their first argument a pointer to the object calling the function (typically named self)

One important function is the "constructor", which in Python is always named __init__

class Circle:
    def __init__(self, x, y, radius, color):
        self.x = x
        self.y = y
        self.radius = radius
        self.color = color
    def draw(self):
        glColor3fv(self.color)
        glBegin(GL_TRIANGLE_FAN)
        glVertex2f(self.x, self.y)
        for i in range(0,30):
          glVertex2f(self.x + cos(radians(i*12.0)) * radius, 
                     self.y + sin(radians(i*12.0)) * radius)
        glEnd()
        
c = Circle(0, 0, 5, [1,0,0])


Creative Commons License
This document is by Dave Pape, and is released under a Creative Commons License.