Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # 4.9 This program displays information about a rectangle drawn by the user
- from graphics import *
- import math
- def main():
- win = GraphWin('Rectangle Information', 400., 400)
- win.setCoords(0, 0, 10, 10)
- # Get the x and y coordinate for Rectangle
- t1 = Text(Point(5, 9), 'First click to get the upper left corner').draw(win)
- ul = win.getMouse()
- t1.undraw()
- t2 = Text(Point(5, 9), 'Second click to get the bottom right corner').draw(win)
- lr = win.getMouse()
- t2.undraw()
- xul = int(ul.getX())
- yul = int(ul.getY())
- print(xul, yul)
- xlr = int(lr.getX())
- ylr = int(lr.getY())
- print(xlr, ylr)
- rec = Rectangle(Point(xul, yul), Point(xlr, ylr))
- rec.draw(win)
- # calculation of the area and perimeter
- area = abs((xul - xlr) * (yul - ylr))
- t3 = 'Area:', area
- Text(Point(5, 9), t3).draw(win)
- print(area)
- l = abs(xul - xlr)
- w = abs(yul - ylr)
- perimeter = 2 * (l + w)
- t4 = 'Perimeter:', perimeter
- Text(Point(5, 8.5), t4).draw(win)
- print(perimeter)
- win.getKey()
- main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement