Draw a face
Program plan:
- Import the header file.
- Define the “drawFace” method
- Get the “x” and “y” positions
- Get the “p1” and “p2” positions
- Call the “Oval” method for drawing the face
- Set the color to the face
- Draw the face
- Get the “lc” and “rc” position
- Call the “Circle” method
- Set the color to the face
- Call the “draw” method
- Call the “Circle” method
- Set the color to the face
- Call the “draw” method
- Get the “m1”, “m2”, and “m3” values
- Call the “Rectangle” method
- Set the color to the face
- Call the “draw” method
- Call the “Line” method
- Get the center position
- Get the “x” and “y” position
- Call the “Point” method
- Call the “Line” method
- Call the “draw” method
- Get the “x” and “y” position for different variables
- Calculate “t” and “t1” value
- Call the “Rectangle” method
- Call the “draw” method
- Iterate “i” until it reaches 7
- Clone the teeth by calling “clone” method
- Move the face by calling “move” method
- Call the “draw” method
- Define the main method.
- Read the filename from the user
- Get the image width and height from the file
- Set the graph win size
- Set the coords for graph
- Draw the face
- Get the number of faces from the user
- Iterate “i” until it reaches “n” value
- Get the mouse action
- Call the “drawFace” method
- Call the function “main()”.
This Python program is used to get the GIF (Graphic Interchange format) file and draw the smileys when the user clicking the GIF image.
Explanation of Solution
Program:
#import the header file
from graphics import *
#definition of "drawFace" method
def drawFace(center, size, window):
#get the "x" value
x1 = center.getX()
#get the "y" value
y1 = center.getY()
#get the "p1" value
p1 = Point(x1-(.7 * size), y1 - size)
#get the "p2" value
p2 = Point(x1+(.7 * size), y1 + size)
#call the "Oval" method
head = Oval(p1, p2)
#fill the color
head.setFill("white")
#call the "draw" method
head.draw(window)
#get the "lc" value
lc = Point(x1 - .2 * size, y1 + .6 * size)
#get the "rc" value
rc = Point(x1 + .2 * size, y1 + .6 * size)
#call the "Circle" method
leftEye = Circle(lc, .13 * size)
#fill the color
leftEye.setFill("white")
#call the "draw" method
leftEye.draw(window)
#call the "Circle" method
rightEye = Circle(rc, .13 * size)
#fill the color
rightEye.setFill("white")
#call the "draw" method
rightEye.draw(window)
#get the "m1" value
m1 = Point(x1 - .3 * size, y1 - .5 * size)
#get the "m2" value
m2 = Point(x1 + .3 * size, y1 - .25 * size)
#get the "m3" value
m3 = Point(x1 + .3 * size, y1 - .5 * size)
#call the "Rectangle" method
mouth = Rectangle(m1, m2)
#fill the color
mouth.setFill("white")
#call the "draw" method
mouth.draw(window)
#call the "Line" method
leftLip = Line(m1, Point(x1- .3 * size, y1 - .25 * size))
#get the center position
mLeftCent = leftLip.getCenter()
#get the "x" position
mLCx = mLeftCent.getX()
#get the "y" position
mLCy = mLeftCent.getY()
#call the "Point" method
mRightCent = Point(x1 + .3 * size, mLCy)
#call the "Line" method
lip = Line(mLeftCent, mRightCent)
#call the "draw" method
lip.draw(window)
#get the "x" positions
m1x = m1.getX()
m3x = m3.getX()
#get the "y" positions
m1y = m1.getY()
m2y = m2.getY()
#calculate the "t" value
t = m1x - m3x
#calculate the "t1" value
t1 = Point(m1x - (1/8 * t), m2y)
#call the "Rectangle" method
teeth = Rectangle(m1, t1)
#call the "draw" method
teeth.draw(window)
#iterate "i" until it reaches 7
for i in range (7):
#clone the face
t2 = teeth.clone()
#call the "move" method
t2.move(-i * (1/8 * t), 0)
#call the "draw" method
t2.draw(window)
#definition of main method
def main():
#get the input filename from the user
fname = input("Enter filename: ")
infile = Image(Point(10, 10), fname)
#get the width of the image
wWidth = infile.getWidth()
#get the height of the image
wHeight = infile.getHeight()
#set graph win name
window = GraphWin('Smile!', wWidth,wHeight)
#set the coords
window.setCoords(0, 0, 20, 20)
#draw a smiley
infile.draw(window)
#get how many faces the user wants to draw
n = eval(input("How many faces should we block? "))
#iterate "i" until it reaches "n"
for i in range(n):
#get the mouse action
point = window.getMouse()
#draw a face by calling the method "drawFace"
drawFace(point, 3, window)
#call the "main" method
main()
Output:
Enter filename: test_img.gif
How many faces should we block? 2
>>>
Screenshot of “Smile!” window
Want to see more full solutions like this?
Chapter 6 Solutions
Python Programming: An Introduction to Computer Science, 3rd Ed.
- Discuss with appropriate examples the types of relationships in a database. Give one reference with your answer.arrow_forwardDetermine the velocity error constant (k,) for the system shown. + R(s)- K G(s) where: K=1.6 A(s+B) G(s) = as²+bs C(s) where: A 14, B =3, a =6. and b =10arrow_forward• Solve the problem (pls refer to the inserted image)arrow_forward
- Write .php file that saves car booking and displays feedback. There are 2 buttons, which are <Book it> <Select a date>. <Select a date> button gets an input from the user, start date and an end date. Book it button can be pressed only if the start date and ending date are chosen by the user. If successful, it books cars for specific dates, with bookings saved. Booking should be in the .json file which contains all the bookings, and have the following information: Start Date. End Date. User Email. Car ID. If the car is already booked for the selected period, a failure message should be displayed, along with a button to return to the homepage. In the booking.json file, if the Car ID and start date and end date matches, it fails Use AJAX: Save bookings and display feedback without page refresh, using a custom modal (not alert).arrow_forwardWrite .php file with the html that saves car booking and displays feedback. Booking should be in the .json file which contains all the bookings, and have the following information: Start Date. End Date. User Email. Car ID. There are 2 buttons, which are <Book it> <Select a date> Book it button can be pressed only if the start date and ending date are chosen by the user. If successful, book cars for specific dates, with bookings saved. If the car is already booked for the selected period, a failure message should be displayed, along with a button to return to the homepage. Use AJAX: Save bookings and display feedback without page refresh, using a custom modal (not alert). And then add an additional feature that only free dates are selectable (e.g., calendar view).arrow_forward• Solve the problem (pls refer to the inserted image) and create line graph.arrow_forward
- who started the world wide webarrow_forwardQuestion No 1: (Topic: Systems for collaboration and social business The information systems function in business) How does Porter's competitive forces model help companies develop competitive strategies using information systems? • List and describe four competitive strategies enabled by information systems that firms can pursue. • Describe how information systems can support each of these competitive strategies and give examples.arrow_forwardData communıcatıon digital data is transmitted via analog ASK and PSK are used together to increase the number of bits transmitted a)For m=8,suggest a solution and define signal elements , and then draw signals for the following sent data data = 0 1 0 1 1 0 0 0 1 0 1 1arrow_forward
- DatacommunicationData = 1 1 0 0 1 0 0 1 0 1 1 1 1 0 0a) how many bıts can be detected and corrected by this coding why prove?b)what wıll be the decision of the reciever if it recieve the following codewords why?arrow_forwardpattern recognitionPCA algor'thmarrow_forwardConsider the following program: LOAD AC, IMMEDIATE(30) ADD AC, REGISTER(R1) STORE AC, MEMORY(20) Given that the value of R1 is 50, determine the value stored at memory address 20 after the program is executed. Provide an explanation to support your answer.arrow_forward
- C++ Programming: From Problem Analysis to Program...Computer ScienceISBN:9781337102087Author:D. S. MalikPublisher:Cengage LearningEBK JAVA PROGRAMMINGComputer ScienceISBN:9781337671385Author:FARRELLPublisher:CENGAGE LEARNING - CONSIGNMENT