#
# file: triangle.py
#
# The program draws a triangle based on three user's mouse clicks.
# Then it finds the perimeter of the rectangle and displays it
# in the same graphics window, where rectangle is drawn.
# Program uses function for finding the distance between two points.
#
from graphics import *
def main():
width = 800
height = 600
win=GraphWin("Drawing a triangle",width,height)
win.setBackground('white')
message=Text(Point(width/2,30),"This program draws a triangle from three mouse clicks.")
message.setSize(20)
msg=Text(Point(width/2,60),"Please click a mouse in three different places inside this window.")
msg.setSize(20)
message.draw(win)
msg.draw(win)
p1=win.getMouse() # getting first point
p2=win.getMouse() # getting second point
p3=win.getMouse() # getting third point
tr=Polygon(p1,p2,p3)
tr.setOutline('blue')
tr.setFill('yellow')
tr.draw(win)
P = distance(p1,p2) + distance(p2,p3) + distance(p1,p3) # finds the perimeter
message.setText("The perimeter of the triangle is %0.2f pixels" % P)
msg.setText("Click a mouse anywhere in this window to finish the program")
x = win.getMouse()
win.close()
# This function finds the distance between two points
def distance(p1,p2):
import math # importing math library for the square root operation
dx = p2.getX()-p1.getX() # dx = x2-x1
dy = p2.getY()-p1.getY() # dy = y2-y1
return math.sqrt(dx*dx+dy*dy)
main()