WEEK04: computer graphics, using objects
---------------------------------------------------------------
 W: more graphics...

LAB4: due next week
QUIZ 2 this Friday

FROM LAST WEEK:

"""
bizzbuzz
J. Knerr -- Fall 2010
"""

#####################################################################
def main():
  """
  print numbers from 1->100,
  bizz if evenly divisible by 3, buzz if 5, bizzbuzz if 3 and 5
  """
  start = 1
  end = 100

  for i in range(start,end+1):
    if i%3==0 and i%5==0:
      print "bizzbuzz (%d)" % i
    elif i%3==0:
      print "bizz (%d)" % i
    elif i%5==0:
      print "buzz (%d)" % i
    else:
      print "%d" % i

#####################################################################
main()

DIFFERENCE BETWEEN imports

$ python
>>> dir()
['__builtins__', '__doc__', '__name__', '__package__']
>>> import math
>>> dir()
['__builtins__', '__doc__', '__name__', '__package__', 'math']
>>> print pi   
Traceback (most recent call last):
  File "", line 1, in 
NameError: name 'pi' is not defined
>>> print math.pi 
3.14159265359
>>> 


$ python
>>> from math import *
>>> dir()
['__builtins__', '__doc__', '__name__', '__package__', 'acos', 'acosh', 'asin', 'asinh', 'atan', 'atan2', 'atanh', 'ceil', 'copysign', 'cos', 'cosh', 'degrees', 'e', 'exp', 'fabs', 'factorial', 'floor', 'fmod', 'frexp', 'fsum', 'hypot', 'isinf', 'isnan', 'ldexp', 'log', 'log10', 'log1p', 'modf', 'pi', 'pow', 'radians', 'sin', 'sinh', 'sqrt', 'tan', 'tanh', 'trunc']
>>> print pi
3.14159265359
>>> 


- see /home/jk/inclass/graphics1.py and graphics2.py for same
  program written with "import graphics" vs "from graphics import *"


OBJECT TERMINOLOGY:

 object: a combination of data and methods, all in one thing
 data: info about the object (like radius, color)
 method: functions to access or modify the object data
 constructor: special function called when object is created
 instance: constructed objects (ex: c = Circle(p,20)...c is an
             instance of the Circle class)

BULLSEYE PROGRAM:

- can you write a program to draw this??



note use of width and height, how color changes for each ring, how circles created in the for loop all use the same variable name (c), how the radius decreases with each iteration of the for loop from graphics import * def main(): width = 500 height = 500 w = GraphWin("bullseye",width,height) w.setBackground("gray40") # center point cp = Point(width*0.5,height*0.5) nrings = 4 maxradius = width*0.5 r = maxradius dr = maxradius / nrings # draw nring rings, each time decreasing the radius for i in range(nrings): c = Circle(cp, r) # make the ring color alternate if i % 2 == 0: c.setFill("red") else: c.setFill("white") c.draw(w) r = r - dr # wait for mouse click to close the window ptext = Point(width*0.5,10) t = Text(ptext, "Click mouse to quit.") t.draw(w) w.getMouse() w.close() main()