CSC111 Lab 9 Solutions 2011

From dftwiki3
Revision as of 14:51, 2 November 2011 by Thiebaut (talk | contribs) (Created page with "--~~~~ ---- =Graphics= <br /> <source lang="python"> from graphics import * def isInside( xc, yc, x1, y1, x2, y2 ): if x1 <= xc <= x2 and y1 <= yc <= y2: return 1...")
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to: navigation, search

--D. Thiebaut 15:51, 2 November 2011 (EDT)


Graphics


from graphics import *


def isInside( xc, yc, x1, y1, x2, y2 ):
    if x1 <= xc <= x2 and y1 <= yc <= y2:
        return 1
    return 0

def main():
    w = 600
    h = 400
    x1 = 20
    y1 = 30
    x2 = w // 3
    y2 = h - y1
    win = GraphWin( "Click me to stop!", w, h )


    c = Circle( Point( w//2, h//4 ), 20 )
    c.setFill( "yellow" )
    c.draw( win )

    r = Rectangle( Point( x1, y1 ), Point( x2, y2 ) )
    r.setWidth(3)
    r.draw( win )

    dirX = 5
    dirY = 3
    
    while True:
        c.move( dirX, dirY )
        xc = c.getCenter().getX()
        yc = c.getCenter().getY()
        if not ( 0 <= xc <= w ): dirX = -dirX
        if not ( 0 <= yc <= h ): dirY = -dirY
        
        if isInside( xc, yc, x1, y1, x2, y2 )== 1:
            # center inside rectangle
            c.setFill( "red" )
        else:
            # center outside rectangle
            c.setFill( "yellow" )

        if win.checkMouse() != None:
            break

    win.close()

main()