Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #User-Defined Triangle
- #Created By: Aaron Schendel
- #October 2nd, 2010
- from graphics import *
- from math import *
- from time import sleep
- def main():
- # Create Window
- win=GraphWin("User's Choice", 800,500) # What numbers are 800 and 500? Why did you use them.
- win.setBackground("lightblue")
- win.setCoords(0,0,13,10)
- # Create and Label Coordinate Plane
- Line(Point(1,1),Point(1,8)).draw(win)
- Line(Point(1,1),Point(8,1)).draw(win)
- Text(Point(.75,.75),'(1,1)').draw(win)
- Text(Point(.5,8),'(1,8)').draw(win)
- Text(Point(8,.5),'(8,1)').draw(win)
- # User Instructions
- instructions=Text(Point(4,.25),'Click on three points to make a triangle.')
- instructions.draw(win)
- # Get User to enter Points
- input1=win.getMouse()
- input1.draw(win)
- input2=win.getMouse()
- input2.draw(win)
- input3=win.getMouse()
- input3.draw(win)
- #Create Triangle
- triangle = Polygon((input1),(input2),(input3))
- triangle.draw(win)
- # Use midpoints to label triangle sides/edit label size
- a = Line(input1, input2).getCenter()
- b = Line(input2, input3).getCenter()
- c = Line(input3,input1).getCenter()
- atext = Text((a),'a')
- atext.setSize(18) # Same as before. What is 18, and why? What if you needed to change it for the ENTIRE system. 5 minutes. Now think of this, but 10,000 lines of it...
- atext.setTextColor("darkgreen")
- atext.draw(win)
- btext = Text((b),'b')
- btext.setSize(18)
- btext.setTextColor("darkgreen")
- btext.draw(win)
- ctext = Text((c),'c')
- ctext.setSize(18)
- ctext.setTextColor("darkgreen")
- ctext.draw(win)
- # Use input points to label angles
- Atext = Text((input3),'A')
- Atext.setSize(18)
- Atext.setTextColor("red")
- Atext.draw(win)
- Btext = Text((input1),'B')
- Btext.setSize(18)
- Btext.setTextColor("red")
- Btext.draw(win)
- Ctext = Text((input2),'C')
- Ctext.setSize(18)
- Ctext.setTextColor("red")
- Ctext.draw(win)
- # Quick Explanation
- explanation = Text(Point(1.7,9.5),'Dark Green Text = Side')
- explanation.setTextColor('darkgreen')
- explanation.draw(win)
- explanation2 = Text(Point(1.32,9),'Red Text = Angle')
- explanation2.setTextColor('red')
- explanation2.draw(win)
- # Get X and Y of the three inputs
- x1 = input1.getX()
- y1 = input1.getY()
- x2 = input2.getX()
- y2 = input2.getY()
- x3 = input3.getX()
- y3 = input3.getY()
- # Figure out length of each side
- alength = sqrt((x2-x1)**2 + (y2-y1)**2)
- blength = sqrt((x3-x2)**2 + (y3-y2)**2)
- clength = sqrt((x1-x3)**2 + (y1-y3)**2)
- # Figure out perimeter
- perimeter = alength + blength + clength
- # Figure out area
- semiperimeter = perimeter / 2
- area = sqrt(semiperimeter*(semiperimeter-alength)*(semiperimeter-blength)*(semiperimeter-clength))
- # Figure out angles
- aangle = (acos(((alength**2)-(blength**2)-(clength**2))/((-2)*blength*clength))) * (180/pi)
- bangle = (acos(((blength**2)-(alength**2)-(clength**2))/((-2)*alength*clength))) * (180/pi)
- cangle = (acos(((clength**2)-(blength**2)-(alength**2))/((-2)*blength*alength))) * (180/pi)
- # Output
- prop = Text(Point(10.5,9),'Triangle Properties')
- prop.setSize(22)
- prop.draw(win)
- # Output side lengths
- sleep(1)
- sidelengths = Text(Point(9.7,8),'Side Lengths: ')
- sidelengths.setSize(18)
- sidelengths.draw(win)
- sleep(.25)
- sidea = Text(Point(9.9,7.5),'Side a = ' + str(alength))
- sidea.setTextColor('darkgreen')
- sidea.draw(win)
- sleep(.25)
- sideb = Text(Point(9.9,7),'Side b = ' + str(blength))
- sideb.setTextColor('darkgreen')
- sideb.draw(win)
- sleep(.25)
- sidec = Text(Point(9.9,6.5),'Side c = ' + str(clength))
- sidec.setTextColor('darkgreen')
- sidec.draw(win)
- # Ouput angle sizes
- sleep(1)
- anglesizes = Text(Point(9.6,5.8), 'Angle Sizes: ')
- anglesizes.setSize(18)
- anglesizes.draw(win)
- sleep(.25)
- anglea = Text(Point(9.9,5.3), 'Angle A = ' + str(aangle))
- anglea.setTextColor('red')
- anglea.draw(win)
- sleep(.25)
- angleb = Text(Point(9.9,4.8), 'Angle B = ' + str(bangle))
- angleb.setTextColor('red')
- angleb.draw(win)
- sleep(.25)
- anglec = Text(Point(9.9,4.3), 'Angle C = ' + str(cangle))
- anglec.setTextColor('red')
- anglec.draw(win)
- # Output perimeter
- sleep(1)
- perimeteroutput = Text(Point(9.3,3.1), ' Perimeter = ')
- perimeteroutput.setSize(18)
- perimeteroutput.draw(win)
- perimeteroutput2 = Text(Point(11.6,3.1),perimeter)
- perimeteroutput2.draw(win)
- # Output area
- sleep(1)
- areaoutput = Text(Point(9, 2),'Area = ')
- areaoutput.setSize(18)
- areaoutput.draw(win)
- areaoutput2 = Text(Point(11,2),(area))
- areaoutput2.draw(win)
- # Change user instructions
- instructions.setText('Click anywhere to exit.')
- # Get mouse click to close and end the program
- win.getMouse()
- win.close()
- main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement