Complex Shapes

In the graphics library we are limited to the four core shape types: rectangle, ellipse, line, and triangle. If more intricate or unique shapes are required, we must use these shapes as the building-blocks for others. We can write our own custom shape functions which use these four to create these custom features. Let’s see a few examples.

Creating a Plus-Sign Function

If we want a function for creating plus-signs, we can build one that does so. The parameters of the function will be:

def plus(graphics, x, y, w, h, color, thickness):
    # TODO: Implement the function

The graphics canvas to add the plus to will need to be passed in as a parameter. The x, y, w, and h are used to specify the position and size of the plus, in a similar way to how it works with an ellipse. The x and y will designate the center of the plus-sign, where the two lines intersect. w and h will be used to specify how large to make it. Finally the color an thickness will be used to specify the look of the symbol. To draw the first horizontal line, we’d need to write:

graphics.line(x-(w/2), y, x+(w/2), y, color, thickness)

And for the vertical line:

graphics.line(x, y-(h/2), x, y+(h/2), color, thickness)

Putting it all together gives us:

def plus(canvas, x, y, w, h, color, thickness):
    '''     
    The plus function draw a plus sign centered at x, y.
    Parameter information:
       * canvas: the graphics object to draw the plus on. 
       * x and y: The coordinates for the center of the plus.
       * w and h: Width and height.
       * color: The fill color.
       * thickness: How thick the plus should be. 
    '''     
    canvas.line(x-(w/2), y, x+(w/2), y, color, thickness)
    canvas.line(x, y-(h/2), x, y+(h/2), color, thickness)

Once we have this implemented, we can use it wherever we need a plus sign! Here is an example program that uses this function:

from graphics import graphics

def main():  
    gui = graphics(500, 500, 'plusses') 
    gui.rectangle(0, 0, 500, 500, 'white')
    plus(gui, 250, 250, 400, 400, 'green', 50)
    plus(gui, 100, 100, 50, 50, 'blue', 10)
    gui.draw()

main()
Which canvas will this code display? Canvas with two plusses Correct. The big green plus is correctly centered on the screen, and has the same width and height. The smaller blue plus is also in the currect location (upper-left) and has the same width and height. Canvas with two plusses Incorrect. The plussed should both have the same lengths for ther horizontal and vertical lines, which this example does not have. The positions are also off forboth of them.

Creating a Smiley Function

Next up we will create a function for drawing a smiley-face of an arbitrary color, size, and location. The face itself and the eyes should be a circle, and the mouth should be a half-circle. The parameters of the function should be:

def smiley(canvas, x, y, size, color):
    # TODO: Implement the function

As with the plus, the canvas will represent the canvas to draw the shape on to, and x and y will represent the location. The smiley face will always be round, so there is not a separate w and h input, only a size which will be used for both width and height. The user can also customize the color of the face. The easy part is drawing the main face background:

canvas.ellipse(x, y, size, size, color)

Next, the eyes. The eyes should always be black. They also need to be positioned correctly (where eyes would typically go on a face) and also relative to the size that was specified as a parameter. Some math is required to get this right:

canvas.ellipse(x+(size/5), y-(size/5), (size/7), (size/7), 'black')
canvas.ellipse(x-(size/5), y-(size/5), (size/7), (size/7), 'black')

The mouth is also a bit tricky. We will need to draw a black circle, but then cover up the top half with a rectangle with a color that matches the face of the smiley, so that it blends in and just looks like part of the circle. Again, some “tricky” math will be needed here:

canvas.ellipse(x, y+(size/10), (size/2), (size/2), 'black')
canvas.rectangle(x-(size/3), y-(size/4), (size/1.5), (size/3), color)

Putting it all together:

def smiley(canvas, x, y, size, color):
    '''
    Draws a smiley face wherever we want!
    Parameter information:
       * canvas: the graphics object to draw the plus on.
       * x and y: The coordinates for the center of the plus.
       * size: determines the width and height.
       * color: The fill color.
    '''
    canvas.ellipse(x, y, size, size, color)
    canvas.ellipse(x, y+(size/10), (size/2), (size/2), 'black')
    canvas.rectangle(x-(size/3), y-(size/4), (size/1.5), (size/3), color)
    canvas.ellipse(x+(size/5), y-(size/5), (size/7), (size/7), 'black')
    canvas.ellipse(x-(size/5), y-(size/5), (size/7), (size/7), 'black')

Again, let’s test out our program to see if it works:

def main():
    gui = graphics(500, 500, 'plusses')
    gui.rectangle(0, 0, 500, 500, 'white')
    smiley(gui, 125, 125, 200, 'orange')
    smiley(gui, 125, 375, 200, 'light green')
    smiley(gui, 375, 125, 200, 'light blue')
    smiley(gui, 375, 375, 200, 'purple')
    gui.draw()

main()
Which canvas will this code display? Canvas with four smileys Correct. Each face is the same size as the others, and is in it's own "corner" of the canvas. Canvas with four smileys Incorrect. According to the code, each face should be the same size, which is not the case here.

PyFlo Home Complete Bookmark Next