Advertisement
kenadams53

Triangle_display

Aug 23rd, 2019
225
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.34 KB | None | 0 0
  1. #Triangle_display code by Ken Adams
  2. #Triangle coordinates are entered in pixels. Then a turtle object is used to draw a triangle with fill color decided by
  3. #the parameter 'col'. Then the perimeter and area are calculated. Results can be displayed in pixels or centimeters;
  4. #to use centimeters the parameter 'cm' must be set to 1. Testing: four triangles were sketched by hand and area, perimeter
  5. #calculated then their shape and calculations were confirmed using this program.
  6.  
  7. from turtle import Turtle
  8.  
  9. tina = Turtle() # time is a turtle object
  10. pxcm = 0.026458333 # multiply pixels by this number to convert to centimeters
  11.  
  12. # calculates the distance on the plane between two coordinates
  13. # will be called by the draw_triangle function
  14. def line_length(x1,y1,x2,y2):
  15. length = ((x1-x2)**2 + (y1-y2)**2)**0.5
  16. return length
  17.  
  18. #draws a triagle with points (x1,y1), (x2,y2), (x3,y3)
  19. def draw_triangle(t_name,x1,y1,x2,y2,x3,y3,col,cm): # if cm is set to 1 result will be in centimeters
  20. t_name.penup()# we dont want to trace the path from the origin to the starting point
  21. t_name.goto(x1,y1) # the first vertex of the triangle
  22. t_name.pendown() # ready to trace
  23. t_name.begin_fill()
  24. t_name.color(col)
  25. t_name.goto(x2,y2) # we trace from the first vertix to the second
  26. t_name.goto(x3,y3) # from the second to the third
  27. t_name.goto(x1,y1) #then back to the first
  28. t_name.end_fill()
  29. a = line_length(x1,y1,x2,y2) # we call another function to calculate the length of the side between the first two vertices
  30. b = line_length(x1,y1,x3,y3) # length second side
  31. c = line_length(x2,y2,x3,y3) # length third side
  32. #print("a = " +str(a) + "b = " +str(b) + "c = " +str(c))# code for testing
  33. perimeter = a + b + c # with known lengths we calculate the perimeter
  34. s= perimeter*0.5 # used later to calculate area
  35. area = (s*(s-a)*(s-b)*(s-c))**0.5 # well known formula for area
  36.  
  37.  
  38. if cm == 1: # dimensions were input and calculated in pixels. Here we have the option to convert to centimeters
  39. print("results in centimeters")
  40. perimeter = perimeter*pxcm
  41. area = area*pxcm*pxcm
  42. units = "centimeters" # the string 'units' will be printed as part of the output
  43. else:
  44. print("results in pixels")
  45. units = "pixels"
  46. xmean = (x1+x2+x3)/3
  47. ymean = (y1+y2+y3)/3
  48. t_name.penup()
  49. t_name.goto(xmean,ymean) # (xmean,ymean) should be somewhere in the middle of the triangle
  50. t_name.pendown()
  51. t_name.color("black")# new color for printing information on the triangle
  52. print_area =round(area,2) # we print results rounded to 2 dp
  53. print_perimeter =round(perimeter,2)
  54. t_name.write("Area: " + str(print_area) + " " + units + "\n\n")
  55. t_name.write("perimeter: " + str(print_perimeter) + " " + units)
  56.  
  57. return perimeter, area
  58. #draws the triangle and returns the perimeter and area
  59. prim, area = draw_triangle(tina,-250,-275,-100,200,315,-275,"yellow",1) #example and first test
  60. #prim, area = draw_triangle(tina,-275,-50,0,250,250,200,"yellow",1) # three more tests
  61. #prim, area = draw_triangle(tina,50,200,-280,270,275,125,"yellow",1)
  62. #prim, area = draw_triangle(tina,50,50,200,300,100,200,"yellow",1)
  63.  
  64. #prints the results in the python screen (not the Turtle screen)
  65. print("perimeter " + str(prim) + "; area " + str(area))
  66. print("bye")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement