Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #Triangle_display code by Ken Adams
- #Triangle coordinates are entered in pixels. Then a turtle object is used to draw a triangle with fill color decided by
- #the parameter 'col'. Then the perimeter and area are calculated. Results can be displayed in pixels or centimeters;
- #to use centimeters the parameter 'cm' must be set to 1. Testing: four triangles were sketched by hand and area, perimeter
- #calculated then their shape and calculations were confirmed using this program.
- from turtle import Turtle
- tina = Turtle() # time is a turtle object
- pxcm = 0.026458333 # multiply pixels by this number to convert to centimeters
- # calculates the distance on the plane between two coordinates
- # will be called by the draw_triangle function
- def line_length(x1,y1,x2,y2):
- length = ((x1-x2)**2 + (y1-y2)**2)**0.5
- return length
- #draws a triagle with points (x1,y1), (x2,y2), (x3,y3)
- def draw_triangle(t_name,x1,y1,x2,y2,x3,y3,col,cm): # if cm is set to 1 result will be in centimeters
- t_name.penup()# we dont want to trace the path from the origin to the starting point
- t_name.goto(x1,y1) # the first vertex of the triangle
- t_name.pendown() # ready to trace
- t_name.begin_fill()
- t_name.color(col)
- t_name.goto(x2,y2) # we trace from the first vertix to the second
- t_name.goto(x3,y3) # from the second to the third
- t_name.goto(x1,y1) #then back to the first
- t_name.end_fill()
- a = line_length(x1,y1,x2,y2) # we call another function to calculate the length of the side between the first two vertices
- b = line_length(x1,y1,x3,y3) # length second side
- c = line_length(x2,y2,x3,y3) # length third side
- #print("a = " +str(a) + "b = " +str(b) + "c = " +str(c))# code for testing
- perimeter = a + b + c # with known lengths we calculate the perimeter
- s= perimeter*0.5 # used later to calculate area
- area = (s*(s-a)*(s-b)*(s-c))**0.5 # well known formula for area
- if cm == 1: # dimensions were input and calculated in pixels. Here we have the option to convert to centimeters
- print("results in centimeters")
- perimeter = perimeter*pxcm
- area = area*pxcm*pxcm
- units = "centimeters" # the string 'units' will be printed as part of the output
- else:
- print("results in pixels")
- units = "pixels"
- xmean = (x1+x2+x3)/3
- ymean = (y1+y2+y3)/3
- t_name.penup()
- t_name.goto(xmean,ymean) # (xmean,ymean) should be somewhere in the middle of the triangle
- t_name.pendown()
- t_name.color("black")# new color for printing information on the triangle
- print_area =round(area,2) # we print results rounded to 2 dp
- print_perimeter =round(perimeter,2)
- t_name.write("Area: " + str(print_area) + " " + units + "\n\n")
- t_name.write("perimeter: " + str(print_perimeter) + " " + units)
- return perimeter, area
- #draws the triangle and returns the perimeter and area
- prim, area = draw_triangle(tina,-250,-275,-100,200,315,-275,"yellow",1) #example and first test
- #prim, area = draw_triangle(tina,-275,-50,0,250,250,200,"yellow",1) # three more tests
- #prim, area = draw_triangle(tina,50,200,-280,270,275,125,"yellow",1)
- #prim, area = draw_triangle(tina,50,50,200,300,100,200,"yellow",1)
- #prints the results in the python screen (not the Turtle screen)
- print("perimeter " + str(prim) + "; area " + str(area))
- print("bye")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement