Home > Learning, Lesson, Problem, Projects/Experiments, Python > Python : Triangle Parameters

## Python : Triangle Parameters

Learning : Python to Draw a Triangle
Subject: To get the Parameters of a Triangle

A week ago I start helping a friend in a mathematics geometric to draw a Triangle from Two points that represent the The Hypotenuse of a Triangle, then I deside to write a python project to Draw a triangle based on two given points (x1,y1) and (x2,y2) and to print it’s parameter (other sides and angles). We will use the Trinket.io as a Python interrapter and will draw with turtle library.

Project Details: The system will ask the user to Enter the coordinates of Two points To draw a Right Angle Triangle. In our Triangle, we will call the (x1,y1) as Point A, and (x2,y2) as Point C. From our starting poins we can calculate the distance between point A and point (B), also the length of Hypotenuse and the angels in the Triangle.

So, What we have:

tri_opposite = abs( y2 – y1 )

tri_adjacent = abs( x2 – x1 )

From a mathimatics triangle formula we know that:

where: x^2 = square(x)

So:

tri_hypo = math.sqrt(tri_hypo)

tri_hypo is the distance between point A and Point C (the opposite
side of the right angle)

Now the Angels:
As we know that in a triangle the summation on all inside angles is 180deg, and in a Right Triangle we will have a one fixed 90 degree angle (ABC), so the first thing we will work on calculating the Opposite angle (BAC).

From a Triangle math:

tri_opposite = abs( y2 – y1)

the_deg (BAC) = inverse tan for (tri_opposite / tri_adjacent)

# get the inverse of Tan

the_ang = math.degrees(math.atan(the_deg))

Now, lets do some coding ..

First thing we will import two libraries turtle and math, also we will do some setting for our turtle. … Here is the code ..

```# Turtle setting

import turtle, math
# turtle_setting
t = turtle.Turtle()
t.penup()
t.shape("non")
#t.speed(9)
t.speed('fastes')
#t.visible = False

t.hideturtle()
```

Then we will write a code to draw a ‘Gray’ coordinates cross lines for our project. Here is the code for it ..

```

def the_cross() :
# To draw the cross represents the coordinate.
t.penup()
t.pencolor('gray')
t.goto(-200,0)
t.pendown()
t.forward(400)
t.penup()
t.goto(0,200)
t.pendown()
t.forward(-400)
t.penup()
t.goto(2,0)
t.pendown()
t.circle(2)
t.penup()
t.goto(0,0)

# calling the function
the_cross() ```

Now we will write the main function code, First we will ask the user to enter the x,y for two points, then we will do our calculations to get other Triangle parameters and angles, finally we will draw the Triangle. .. Here is the code..

```

def draw_triangle(x1, y1, x2, y2) :

# draw the Triangle.
style = ('Courier', 20)   # font style.
t.penup()
t.goto(x1,y1)
t.pendown()
t.circle(1)  # To draw small circle on point A.
t.write('A',font = style)  # To write A next to the point.
t.forward((x2-x1))
x3 = t.xcor() # here is the x for point B
y3 = t.ycor() # here is the y for point B

t.right(-90)
t.forward((y2-y1))
t.write('C',font = style)  # To write C next to the point.
t.goto(x2,y2)
t.pendown()
t.circle(1)  # To draw small circle on point C.
t.penup()
t.goto(x3,y3)
t.pendown()
t.circle(1)  # To draw small circle on point B.
t.penup()
t.setpos(x3-15,y3-15)
t.write('B',font = style)  # To write B next to the point.

# To calculate the angle of BAC using Triangle Math Equations.
tri_opposite = abs( y2 - y1)

# get the inverse ot Tan using Triangle Math Equations.
the_ang = math.degrees(math.atan(the_deg)) #atan(x) is the inverse of math Tan function.

t.goto(x1,y1)

# To correct the rotation angle based on it's coordinates.
if (x1 > x3) and (y2 > y1) :
rotation = the_ang - 180

elif (x1  y1):
rotation =  360 - the_ang

elif (x1 > x3) and  (y2 < y1) :
rotation =  180 - the_ang

elif (x1 < x3) and  (y2 < y1) :
rotation =   the_ang - 360

#t.right(rotation)
#t.pendown()
# From using Triangle Math Equations, we know that Hypotenuse^2 = Opposite^2 + adjacent^2.

tri_hypo = math.sqrt(tri_hypo)
t.right(rotation)
t.pendown()
t.forward(tri_hypo)
t.penup()

print('\n  The Triangle Information:')
print('  Point A= ({},{})'.format(x1,y1))
print('  Point B= ({},{})'.format(x3,y3))
print('  Point C= ({},{})'.format(x2,y2))
print('  Opposite',tri_opposite)
print('  Hypotenuse',tri_hypo)
print('  Angle: ABC = 90')
print('  Angle: BAC = ',the_ang)
print('  Angle: ACB = ',180 - the_ang - 90)

```

Finally, we need to call the function and pass the two point to it, to do this first we will ask the user to Enter the Two Points and we will make sure that the points are not the same.. Here is the code ..

```# calling the draw_triangle function ..

print('\n   To draw the Triangle you need to Enter Two Points as X,Y for each one.')
x1 = int(input('   Enter x for First point: '))
y1 = int(input('   Enter y for First point: '))
x2 = int(input('   Enter x for Second point: '))
y2 = int(input('   Enter y for Second point: '))

if (x2==x1) or (y2==y1) :
print('  Your Points are not valid, (x1,x2) or (y1,y2) can't be equal ')
else :
draw_triangle(x1, y1, x2, y2)
``` Follow me on Twitter..