SHARE
TWEET

Untitled

a guest Jun 19th, 2017 49 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #User-Defined Triangle
  2. #Created By: Aaron Schendel
  3. #October 2nd, 2010
  4.  
  5. from graphics import *
  6. from math import *
  7. from time import sleep
  8.  
  9.  
  10. def main():
  11.     # Create Window
  12.     win=GraphWin("User's Choice", 800,500) # What numbers are 800 and 500? Why did you use them.
  13.     win.setBackground("lightblue")
  14.     win.setCoords(0,0,13,10)
  15.  
  16.     # Create and Label Coordinate Plane
  17.     Line(Point(1,1),Point(1,8)).draw(win)
  18.     Line(Point(1,1),Point(8,1)).draw(win)
  19.     Text(Point(.75,.75),'(1,1)').draw(win)
  20.     Text(Point(.5,8),'(1,8)').draw(win)
  21.     Text(Point(8,.5),'(8,1)').draw(win)
  22.  
  23.     # User Instructions
  24.     instructions=Text(Point(4,.25),'Click on three points to make a triangle.')
  25.     instructions.draw(win)
  26.  
  27.     # Get User to enter Points
  28.     input1=win.getMouse()
  29.     input1.draw(win)
  30.     input2=win.getMouse()
  31.     input2.draw(win)
  32.     input3=win.getMouse()
  33.     input3.draw(win)
  34.  
  35.     #Create Triangle
  36.     triangle = Polygon((input1),(input2),(input3))
  37.     triangle.draw(win)
  38.  
  39.     # Use midpoints to label triangle sides/edit label size
  40.     a = Line(input1, input2).getCenter()
  41.     b = Line(input2, input3).getCenter()
  42.     c = Line(input3,input1).getCenter()
  43.  
  44.     atext = Text((a),'a')
  45.     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...
  46.     atext.setTextColor("darkgreen")
  47.     atext.draw(win)
  48.     btext = Text((b),'b')
  49.     btext.setSize(18)
  50.     btext.setTextColor("darkgreen")
  51.     btext.draw(win)
  52.     ctext = Text((c),'c')
  53.     ctext.setSize(18)
  54.     ctext.setTextColor("darkgreen")
  55.     ctext.draw(win)
  56.  
  57.     # Use input points to label angles
  58.     Atext = Text((input3),'A')
  59.     Atext.setSize(18)
  60.     Atext.setTextColor("red")
  61.     Atext.draw(win)
  62.     Btext = Text((input1),'B')
  63.     Btext.setSize(18)
  64.     Btext.setTextColor("red")
  65.     Btext.draw(win)
  66.     Ctext = Text((input2),'C')
  67.     Ctext.setSize(18)
  68.     Ctext.setTextColor("red")
  69.     Ctext.draw(win)
  70.    
  71.     # Quick Explanation
  72.     explanation = Text(Point(1.7,9.5),'Dark Green Text = Side')
  73.     explanation.setTextColor('darkgreen')
  74.     explanation.draw(win)
  75.  
  76.     explanation2 = Text(Point(1.32,9),'Red Text = Angle')
  77.     explanation2.setTextColor('red')
  78.     explanation2.draw(win)
  79.  
  80.     # Get X and Y of the three inputs
  81.     x1 = input1.getX()
  82.     y1 = input1.getY()
  83.     x2 = input2.getX()
  84.     y2 = input2.getY()
  85.     x3 = input3.getX()
  86.     y3 = input3.getY()
  87.  
  88.     # Figure out length of each side
  89.     alength = sqrt((x2-x1)**2 + (y2-y1)**2)
  90.     blength = sqrt((x3-x2)**2 + (y3-y2)**2)
  91.     clength = sqrt((x1-x3)**2 + (y1-y3)**2)
  92.  
  93.     # Figure out perimeter
  94.     perimeter = alength + blength + clength
  95.  
  96.     # Figure out area
  97.     semiperimeter = perimeter / 2
  98.     area = sqrt(semiperimeter*(semiperimeter-alength)*(semiperimeter-blength)*(semiperimeter-clength))
  99.  
  100.     # Figure out angles
  101.     aangle = (acos(((alength**2)-(blength**2)-(clength**2))/((-2)*blength*clength))) * (180/pi)
  102.     bangle = (acos(((blength**2)-(alength**2)-(clength**2))/((-2)*alength*clength))) * (180/pi)
  103.     cangle = (acos(((clength**2)-(blength**2)-(alength**2))/((-2)*blength*alength))) * (180/pi)
  104.  
  105.     # Output
  106.  
  107.     prop = Text(Point(10.5,9),'Triangle Properties')
  108.     prop.setSize(22)
  109.     prop.draw(win)
  110.  
  111.     # Output side lengths
  112.     sleep(1)
  113.     sidelengths = Text(Point(9.7,8),'Side Lengths: ')
  114.     sidelengths.setSize(18)
  115.     sidelengths.draw(win)
  116.  
  117.     sleep(.25)
  118.     sidea = Text(Point(9.9,7.5),'Side a = ' + str(alength))
  119.     sidea.setTextColor('darkgreen')
  120.     sidea.draw(win)
  121.     sleep(.25)
  122.     sideb = Text(Point(9.9,7),'Side b = ' + str(blength))
  123.     sideb.setTextColor('darkgreen')
  124.     sideb.draw(win)
  125.     sleep(.25)
  126.     sidec = Text(Point(9.9,6.5),'Side c = ' + str(clength))
  127.     sidec.setTextColor('darkgreen')
  128.     sidec.draw(win)
  129.  
  130.     # Ouput angle sizes
  131.     sleep(1)
  132.     anglesizes = Text(Point(9.6,5.8), 'Angle Sizes: ')
  133.     anglesizes.setSize(18)
  134.     anglesizes.draw(win)
  135.  
  136.     sleep(.25)
  137.     anglea = Text(Point(9.9,5.3), 'Angle A = ' + str(aangle))
  138.     anglea.setTextColor('red')
  139.     anglea.draw(win)
  140.     sleep(.25)
  141.     angleb = Text(Point(9.9,4.8), 'Angle B = ' + str(bangle))
  142.     angleb.setTextColor('red')
  143.     angleb.draw(win)
  144.     sleep(.25)
  145.     anglec = Text(Point(9.9,4.3), 'Angle C = ' + str(cangle))
  146.     anglec.setTextColor('red')
  147.     anglec.draw(win)
  148.  
  149.     # Output perimeter
  150.     sleep(1)
  151.     perimeteroutput = Text(Point(9.3,3.1), ' Perimeter = ')
  152.     perimeteroutput.setSize(18)
  153.     perimeteroutput.draw(win)
  154.  
  155.     perimeteroutput2 = Text(Point(11.6,3.1),perimeter)
  156.     perimeteroutput2.draw(win)
  157.  
  158.     # Output area
  159.     sleep(1)
  160.     areaoutput = Text(Point(9, 2),'Area = ')
  161.     areaoutput.setSize(18)
  162.     areaoutput.draw(win)
  163.  
  164.     areaoutput2 = Text(Point(11,2),(area))
  165.     areaoutput2.draw(win)
  166.  
  167.     # Change user instructions
  168.     instructions.setText('Click anywhere to exit.')
  169.  
  170.    
  171.     # Get mouse click to close and end the program
  172.     win.getMouse()
  173.     win.close()
  174.  
  175.  
  176.  
  177. main()
RAW Paste Data
Want to get better at Python?
Learn to code Python in 2017
Pastebin PRO Summer Special!
Get 40% OFF on Pastebin PRO accounts!
Top