Difference between revisions of "CSC111 Homework 9 2018"

From dftwiki3
Jump to: navigation, search
(Problem 2)
 
(10 intermediate revisions by the same user not shown)
Line 2: Line 2:
 
----
 
----
 
<br />
 
<br />
 +
<onlydft>
 
<bluebox>
 
<bluebox>
 
<br />
 
<br />
Line 59: Line 60:
 
=Submission=
 
=Submission=
 
<br />
 
<br />
Remember that you must submit 2 files, the program and a screen capture of your window.  Go to the Moodle page for the submission links.
+
Remember that you must submit 2 files, the program and a screen capture of your window.  Go to the Moodle page for the submission links, and upload your files in the HW 9 PB 2 section.
  
 
<br />
 
<br />
Line 85: Line 86:
 
Do not hesitate to explore the graphics library and generate more sophisticated cars!
 
Do not hesitate to explore the graphics library and generate more sophisticated cars!
 
<br />
 
<br />
<center>[[Image:CSC111FancyCars.png|500px]]</center>
+
<center>[[Image:CSC111FancyCars.png|350px]]&nbsp;&nbsp;[[Image:CSC111CarsAndTruck.gif|350px]]</center>
 +
<br />
 +
<br />
 +
=Problem 3=
 +
<br />
 +
Write a program called '''hw9_3.py''' that loads the following image in a graphic window...
 +
<br />
 +
[[Image:presSmiley.gif|center]]
 +
<br />
 +
<center>pres.gif</center>
 +
<br />
 +
Make sure you name your image "pres.gif" as your program will be tested with the original image, and if you use a different image name, your program will crash and will receive 50/100.
 +
<br />
 +
==Your Assignment==
 +
<br />
 +
Add functions to your program so that the image in the left half of the window is replicated in the right hand side, flipped horizontally, and transformed to black and white.  This will erase the smiley and replaced it with a mirror image of the president.
 +
<br />
 +
[[Image:SmithPresTimes2.gif|center]]
 +
<br />
 +
Your program should also add your name (or names) in the middle of the image, in white, so that it is easy to read.
 +
<br />
 +
;Notes
 +
:* Your program should NOT generate the "Demo Output" message.  It's only there to prevent people from using this image as their output.
 +
:* You may or may not have a vertical black line in the middle of your image.  Either one is fine.
 +
:* Your program should display your name or names instead of the "Mickey Mouse" message.
 +
<br />
 +
Here's an example of how this looks when the program is processing the image:
 +
<br />
 +
<center><videoflash>IGd2Qc5iYtg</videoflash></center>
 
<br />
 
<br />
 
<br />
 
<br />
</showafterdate>
 
  
 +
==Moodle Submission==
 +
<br />
 +
Submit your documented program in the HW 9 PB 3 section of Moodle, along with a screen shot of your output file.
 +
<br />
 +
</onlydft>
 
<!-- ================================================================= -->
 
<!-- ================================================================= -->
 
<!-- ================================================================= -->
 
<!-- ================================================================= -->
Line 392: Line 425:
  
 
</source>
 
</source>
 +
==Problem 3==
 +
<br />
 +
::<source lang="python">
 +
# hw9_3.py
 +
# D. Thiebaut
 +
# Takes an image that is 480x240, with the left half
 +
# containing the photo of a person, and replicates it
 +
# on the right half (where a smiley appears), flips it
 +
# horizontally, and make it black and white.
  
 +
from graphics import *
 +
from random import *
 +
 +
WIDTH  = 480  # width and height for pres.gif
 +
HEIGHT = 240
 +
 +
def copyAndFlip( img, win, srcX, srcY, destX, destY, w, h ):
 +
    '''copy the left half over the right half, and flips it at the
 +
    same time'''
 +
 +
    x2 = destX+w
 +
    for x in range( srcX, srcX+w ):
 +
        y2 = destY
 +
        win.update()
 +
        for y in range( srcY, srcY+h ):
 +
            r,g,b = img.getPixel( x, y )
 +
            img.setPixel( x2, y2, color_rgb( r, g, b ) )
 +
            y2 += 1
 +
        x2 -= 1
 +
 +
def makeBW( img, win, srcX, srcY, w, h ):
 +
    '''change the right side of the image to b&w '''
 +
    for x in range( srcX, srcX+w ):
 +
        win.update()
 +
        for y in range( srcY, srcY+h ):
 +
            r,g,b = img.getPixel( x, y )
 +
            bw = int(0.3 * r + 0.6 * g + 0.11 * b)
 +
            img.setPixel( x, y, color_rgb( bw, bw, bw ) )
 +
 +
 +
def main():
 +
    # open the window
 +
    win = GraphWin( "CSC111 Image Viewer", WIDTH, HEIGHT )
 +
 +
    # get the file name from the user
 +
    fileName = "pres.gif"
 +
    print()
 +
   
 +
    # open the cat image
 +
    pres = Image( Point(WIDTH//2, HEIGHT//2), fileName )
 +
    pres.draw( win )
 +
 +
    # change the pixels to max red
 +
    copyAndFlip( pres, win, 0, 0, 240, 0, 240, 240)
 +
   
 +
    # show image after change
 +
    win.update()
 +
 +
    # show image after change
 +
    makeBW( pres, win, 240, 0, 240, 240 )
 +
    win.update()
 +
 +
    # add label
 +
    label = Text( Point( WIDTH//2, HEIGHT-30 ),
 +
                  "Mickey Mouse" )
 +
    label.setFill( "white" )
 +
    label.draw( win )
 +
   
 +
    # wait for user to click the mouse...
 +
    win.getMouse()
 +
 +
    # and close the window
 +
    win.close()
 +
 +
main()
 +
 +
</source>
 
</showafterdate>
 
</showafterdate>
 
<br />
 
<br />

Latest revision as of 12:45, 1 June 2018

D. Thiebaut (talk) 17:12, 8 April 2018 (EDT)




...


<showafterdate after="20180420 12:00" before="20180630 00:00">

Solution Programs


Part 1


# wheel1.py
# D. Thiebaut
# a skeleton program to get started with the graphics
# 
from graphics import *
import random
import tkcolors

# define geometry (constants)
WIDTH  = 700
HEIGHT = 600


class Wheel:
    """a Wheel is 2 concentric circles, the larger one black,
    the smaller one grey."""
    
    def __init__(self, centr, rad1, rad2 ):
        # make circ1 the smaller of the 2 circles
        self.circ1   = Circle( centr, min( rad1, rad2 ) )
        self.circ2   = Circle( centr, max( rad1, rad2 ) )
        self.circ1.setFill( "grey" )
        self.circ2.setFill( "black" )
        
    def draw( self, win ):
        self.circ2.draw( win )
        self.circ1.draw( win )

    def move( self, dx, dy ):
        self.circ1.move( dx, dy )
        self.circ2.move( dx, dy )
        
class Car:
    """a class containing a rectangle and 2 wheels, forming
    a simple car"""

    def __init__( self, refPoint ):
        # create rectangle
        self.width  = 60
        self.height = 20
        self.radius = 10
        
        P1  = refPoint
        xP1 = P1.getX()
        yP1 = P1.getY()
        xP2 = xP1 + self.width
        yP2 = yP1 + self.height
        P2  = Point( xP2, yP2 )
        self.body = Rectangle( P1, P2 )
        self.body.setFill( random.choice( tkcolors.colors ) )

        P3  = Point( xP1+self.width//4, yP1 )
        P4  = Point( xP1+self.width*3//4, yP1-self.height//2 )
        self.top = Rectangle( P3, P4 )
        self.top.setFill( random.choice( tkcolors.colors ) )
        
        # create the Wheels
        self.w1 = Wheel( Point( xP1+self.width//4, yP1+self.height ), self.radius//2, self.radius )
        self.w2 = Wheel( Point( xP1++self.width*3//4, yP1+self.height ), self.radius//2, self.radius )

        # set the initial speed
        self.dx = 1
        self.dy = 0

    def setSpeed( self, deltaX, deltaY ):
        self.dx = deltaX
        self.dy = deltaY
        
    def draw( self, win ):
        self.w1.draw( win )
        self.body.draw( win )
        self.w2.draw( win )
        self.top.draw( win )

    def autoMove( self ):
        self.w1.move( self.dx, self.dy )
        self.w2.move( self.dx, self.dy )
        self.body.move( self.dx, self.dy )

    def reverseDirection( self ):
        self.dx = -self.dx

    def getX( self ):
        return self.body.getP1().getX()
        
#----------------------------------------------------------------
def waitForClick( win, message ):
    """ waitForClick: stops the GUI and displays a message.  
    Returns when the user clicks the window. The message is erased."""

    # wait for user to click mouse to start
    startMsg = Text( Point( win.getWidth()/2, win.getHeight()/2 ), message )
    startMsg.draw( win )    # display message
    win.getMouse()          # wait
    startMsg.undraw()       # erase

def main():
    global W, H
    win = GraphWin( "Wrapping Paper", WIDTH, HEIGHT )
    win.setBackground( "lightblue" )
    
    #waitForClick( win, "click to start" )

    cars = []
    for x in range( 10, WIDTH, 78 ):
        for y in range( 20, HEIGHT, 50 ):
            car1 = Car( Point( x, y ) )
            car1.draw( win )
            cars.append( car1 )        

    Banner = Rectangle( Point( WIDTH//2-200, HEIGHT//2-40 ),
                        Point( WIDTH//2+200, HEIGHT//2+40 ) )
    Banner.setFill( "white" )
    Banner.draw( win )

    BannerText = Text( Point( WIDTH//2, HEIGHT//2 ),
                        "CSC111 HOMEWORK 10" )
    BannerText.setSize( 30 )
    BannerText.draw( win )
    
    waitForClick( win, "" )
    win.close()
    
main()


Part 2


# homework 10
# D. Thiebaut
from graphics import GraphicsWindow
from random import seed
from random import randrange

MAXWIDTH = 800
MAXHEIGHT = 600


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 ):
        xAnchor = self._x-self._diameter//2
        yAnchor = self._y-self._diameter//2
        canvas.setFill( self._color[0], self._color[1], self._color[2] )            
        canvas.drawOval( xAnchor,
                         yAnchor,
                         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 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 setRandomColor( self ):
        r = randrange( 256 )
        g = randrange( 256 )
        b = randrange( 256 )
        self._color = (r, g, b )

    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 )
         
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._top  = Rectangle( x+width//4, y-height//2,
                                width//2, height//2,
                                color )
        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 setRandomColor( self ):
        self._top.setRandomColor()
        self._body.setRandomColor()
        
    def getTotalHeight( self ):
        return self._height*2 +  self._width//10

    def getTotalWidth( self ):
        return self._width
        
    def draw( self, canvas ):
        self._body.draw( canvas )
        self._top.draw( canvas )
        self._w1.draw( canvas )
        self._w2.draw( canvas )

class Truck:
    def __init__( self,  x, y, width, color ):
        self._x       = x 
        self._y       = y 
        self._width   = width
        height        = 5 * width//14
        self._height  = height
        self._color   = color   # list of 3 ints between 0 and 255

        
        # build the body
        self._top  = Rectangle( x+ 9* width//14, y-height,
                                width*5//14, height,
                                color )
        self._window = Rectangle( x+ 10* width//14, y-4*height//5,
                                width*4//14, 4*height//5,
                                (0, 0, 0 ) )
        self._body = Rectangle( x, y, width, height, color )
        
        # build the wheels
        self._w1 = Wheel( x + 2*width//14, y + 13*height//10, 3*height//5 )
        self._w2 = Wheel( x + 11*width//14, y + height, width//2 )

    def getTotalHeight( self ):
        return self._height*2 +  self._width//2

    def getTotalWidth( self ):
        return self._width

    def setRandomColor( self ):
        self._top.setRandomColor()
        self._body.setRandomColor()
    
    def draw( self, canvas ):
        self._body.draw( canvas )
        self._top.draw( canvas )
        self._window.draw( canvas )
        self._w1.draw( canvas )
        self._w2.draw( canvas )
    
   
            
def main():
    seed()
    win = GraphicsWindow(MAXWIDTH, MAXHEIGHT)
    canvas = win.canvas()
    
    canvas.setBackground( 0, 250, 250 )
    
    carWidth = 90
    carHeight = 25
    oddEven = True
    for y in range( 0, MAXHEIGHT, carHeight*2 ):#+20 ):
        for x in range( 0, MAXWIDTH, carWidth+10 ):
            if oddEven:
                c = Car( x, y, carWidth, carHeight, ( 0, 0, 0 ) )
            else:
                c = Truck( x, y, carWidth, (0, 0, 0 ) )
            c.setRandomColor()
            c.draw( canvas )
        #oddEven = not oddEven

    banner = Rectangle( MAXWIDTH//3, 2*MAXHEIGHT//5,
                        MAXWIDTH//3, MAXHEIGHT//5, (255, 255, 255 ) )
    banner.draw( canvas )
    canvas.setTextFont( 'arial', 40, 'bold' )
    canvas.drawText( MAXWIDTH//3+10, 2*MAXHEIGHT//5+30,
                     "Happy Easter" )
    win.wait()
    win.close()
    
main()

Problem 3


# hw9_3.py
# D. Thiebaut
# Takes an image that is 480x240, with the left half
# containing the photo of a person, and replicates it
# on the right half (where a smiley appears), flips it
# horizontally, and make it black and white.

from graphics import *
from random import *

WIDTH  = 480   # width and height for pres.gif
HEIGHT = 240

def copyAndFlip( img, win, srcX, srcY, destX, destY, w, h ):
    '''copy the left half over the right half, and flips it at the
    same time'''

    x2 = destX+w
    for x in range( srcX, srcX+w ):
        y2 = destY 
        win.update()
        for y in range( srcY, srcY+h ):
            r,g,b = img.getPixel( x, y )
            img.setPixel( x2, y2, color_rgb( r, g, b ) )
            y2 += 1
        x2 -= 1

def makeBW( img, win, srcX, srcY, w, h ):
    '''change the right side of the image to b&w '''
    for x in range( srcX, srcX+w ):
        win.update()
        for y in range( srcY, srcY+h ):
            r,g,b = img.getPixel( x, y )
            bw = int(0.3 * r + 0.6 * g + 0.11 * b)
            img.setPixel( x, y, color_rgb( bw, bw, bw ) )


def main():
    # open the window
    win = GraphWin( "CSC111 Image Viewer", WIDTH, HEIGHT )

    # get the file name from the user
    fileName = "pres.gif"
    print()
    
    # open the cat image
    pres = Image( Point(WIDTH//2, HEIGHT//2), fileName )
    pres.draw( win )

    # change the pixels to max red
    copyAndFlip( pres, win, 0, 0, 240, 0, 240, 240)
    
    # show image after change
    win.update()

    # show image after change
    makeBW( pres, win, 240, 0, 240, 240 )
    win.update()

    # add label
    label = Text( Point( WIDTH//2, HEIGHT-30 ),
                  "Mickey Mouse" )
    label.setFill( "white" )
    label.draw( win )
    
    # wait for user to click the mouse...
    win.getMouse()

    # and close the window
    win.close()

main()

</showafterdate>