# # 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(): win=GraphWin("Drawing a triangle",640,480) win.setBackground('white') message=Text(Point(320,20),"This program draws a triangle from three mouse clicks.") msg=Text(Point(320,40),"Please click a mouse in three different places inside this window.") message.draw(win) msg.draw(win) p1=win.getMouse() # getting first point p2=win.getMouse() # getting second point p3=win.getMouse() # getting fourth 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.getY() # dx = x2-x1 dy=p2.getY()-p1.getY() # dy = y2-y1 dist=math.sqrt(dx*dx+dy*dy) return dist main()