CSC111 Lab 11 Solution Program 2014

From dftwiki3
Revision as of 16:19, 17 April 2014 by Thiebaut (talk | contribs) (Created page with "--~~~~ ---- <source lang="python"> # Solution program for Lab 11 # from graphicsH import GraphicsWindow from random import seed from random import randrange MAXWIDTH = 800 ...")
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to: navigation, search

--D. Thiebaut (talk) 16:19, 17 April 2014 (EDT)


# Solution program for Lab 11
# 
from graphicsH import GraphicsWindow
from random import seed
from random import randrange

MAXWIDTH = 800
MAXHEIGHT = 600

class Circle1:
    def __init__( self, x, y, diameter, color ):
        self._x      = x
        self._y      = y
        self._diameter= diameter
        self._color  = color   # list of 3 ints between 0 and 255

    def draw( self, canvas ):
        canvas.setFill( self._color[0], self._color[1], self._color[2] )            
        canvas.drawOval( self._x, self._y, self._diameter,
                            self._diameter )

class Circle2:
    def __init__( self, x, y, diameter, color ):
        self._x       = x 
        self._y       = y 
        self._diameter= diameter
        self._color  = color   # list of 3 ints between 0 and 255

    def draw( self, canvas ):
        canvas.setFill( self._color[0], self._color[1], self._color[2] )            
        canvas.drawOval( self._x-self._diameter//2,
                         self._y-self._diameter//2,
                         self._diameter,
                         self._diameter )

class Wheel:
    def __init__( self, x, y, diameter ):
        self._c1 = Circle2( x, y, diameter, ( 0,0,0 ) )
        self._c2 = Circle2( x, y, diameter/2, ( 200, 200, 200 ) )

    def draw( self, canvas ):
        self._c1.draw( canvas )
        self._c2.draw( canvas )
        
class Car:
    def __init__( self, x, y, width, height, color ):
        self._x       = x 
        self._y       = y 
        self._width   = width
        self._height  = height
        self._color   = color   # list of 3 ints between 0 and 255

        # build the body
        self._body = Rectangle( x, y, width, height, color )
        
        # build the wheels
        self._w1 = Wheel( x + width//4, y + height, width//5 )
        self._w2 = Wheel( x + 3*width//4, y + height, width//5 )
        
    def draw( self, canvas ):
        self._body.draw( canvas )
        self._w1.draw( canvas )
        self._w2.draw( canvas )
    
class Rectangle:
    def __init__( self, x, y, width, height, color ):
        self._x      = x
        self._y      = y
        self._width  = width
        self._height = height
        self._color  = color   # list of 3 ints between 0 and 255

    def draw( self, canvas ):
        canvas.setFill( self._color[0], self._color[1], self._color[2] )            
        canvas.drawRect( self._x, self._y, self._width, self._height )


def Problem1( canvas ):

    canvas.setFill( 255, 0, 0  )    # full red
    canvas.drawRect( 100, 100, 300, 200 )
    canvas.setFill( 200, 100, 50 )
    canvas.drawRect( 400, 300, 50, 50 )

def Rects500( canvas ):

    for i in range( 500 ):
        x = randrange( MAXWIDTH * 3//4 )
        y = randrange( MAXHEIGHT * 3//4 )
        w = randrange( MAXWIDTH // 2 )
        h = randrange( MAXHEIGHT // 2 )
        r = randrange( 255 )
        g = randrange( 255 )
        b = randrange( 255 )
    
        canvas.setFill( r, g, b  ) 
        canvas.drawRect( x, y, w, h )   

def Problem2( canvas ):

    canvas.setFill( 255, 0, 0  )    # full red
    canvas.drawOval( 100, 100, 300, 200 )
    canvas.setFill( 200, 100, 50 )
    canvas.drawOval( 400, 300, 50, 50 )

def Problem3( canvas ):
    diameter = 30
    for x in range( 0, MAXWIDTH, diameter ):
        for y in range( 0, MAXHEIGHT, diameter ):
            r = randrange( 255 )
            g = randrange( 255 )
            b = randrange( 255 )
            canvas.setFill( r, g, b )
            canvas.drawOval( x, y, diameter, diameter )

def twoRectObjects( canvas ):
    rect1 = Rectangle( 100, 100, 500, 20, (255, 0, 0 ) )
    rect1.draw( canvas )
    
    rect2 = Rectangle( 200, 200, 50, 200, (165, 76, 89 ) )
    rect2.draw( canvas )

def fiveHundredRectObjects( canvas ):
    for i in range( 500 ):
        x = randrange( MAXWIDTH*3//4 )
        y = randrange( MAXHEIGHT *3//4 )
        w = randrange( MAXWIDTH // 2 )
        h = randrange( MAXHEIGHT // 2 )
        red   = randrange( 256 )
        green = randrange( 256 )
        blue  = randrange( 256 )
        rect = Rectangle( x, y, w, h, (red, green, blue ) )
        rect.draw( canvas )
        
def coveringCircleObjects( canvas ):
    diameter = 30
    for x in range( 0, MAXWIDTH, diameter ):
        for y in range( 0, MAXHEIGHT, diameter ):
            r = randrange( 255 )
            g = randrange( 255 )
            b = randrange( 255 )
            c = Circle1( x, y, diameter, (r, g, b) )
            c.draw( canvas )            
    
            
def main():
    seed()
    win = GraphicsWindow(MAXWIDTH, MAXHEIGHT)
    canvas = win.canvas()
    
    #Problem1( canvas )
    #Rects500( canvas )
    #Problem2( canvas )
    #Problem3( canvas )
    #twoRectObjects( canvas )
    #fiveHundredRectObjects( canvas )
    #coveringCircleObjects( canvas )
    
    #c = Circle2( 100, 100, 200, (255, 0, 0) )
    #c.draw( canvas )

    #w = Wheel( 100, 100, 200 )
    #w.draw( canvas )

    c = Car( 100,200, 400, 150, ( 255, 255, 0 ) )
    c.draw( canvas )
    
    win.wait()
    win.close()
    
main()