Python Program to find area of triangle using coordinates

Python PythonBasic

In this post, we are going to learn Python Program to find area of triangle using coordinates. We will define a class and function to make code reusable.

1. Python program to find area of triangle using coordinates


The coordinates for all three sides of the triangle are A(x1, y1), B(x2, y2), and C(x3, y3). So by using these coordinates we can calculate the area of the triangle with the help of a mathematical formula given below.

We can use different ways to find areas of the circle also in python.

The abs() function helps us to get the absolute result of the calculated area of a triangle.

Formulas to find the area of a triangle using coordinates

 (0.5)(x1(y2-y3)+x2(y3-y1)+x3(y1-y2))

#formula Using abs() function
abs((0.5)*(x1*(y2-y3)+x2*(y3-y1)+x3*(y1-y2)))

Python Program Example

In this example, we are asking the user to input all coordinates of the triangle and using the formula Triange_Area = abs((0.5)(x1(y2-y3)+x2(y3-y1)+x3(y1-y2))) to find the triangle area.

If the area of a triangle is zero then the printing message “A Triangle cannot be draw by given input coordinates!”

x1 = int(input("Enter the value of x1 :"))
y1 = int(input("Enter the value of y1 :"))
x2 = int(input("Enter the value of x2 :"))
y2 = int(input("Enter the value of y2 :"))
x3 = int(input("Enter the value of x3 :"))
y3 = int(input("Enter the value of y3 :"))

Triange_Area = abs((0.5)*(x1*(y2-y3)+x2*(y3-y1)+x3*(y1-y2)))
print("calculated  Area of the Triangle is :",Triange_Area)
if Triange_Area == 0:
    print("A Triangle cannot be draw by the input coordinates!")

Output

Enter the value of x1 :0
Enter the value of y1 :0
Enter the value of x2 :2
Enter the value of y2 :4
Enter the value of x3 :5
Enter the value of y3 :6
calculated  Area of the Triangle is : 4.0

2. Python program to find area of triangle using coordinates in function


In this Python program, we have defined a function triangle area() that takes coordinates of the triangle arguments.

  • Using the formula = abs((0.5)(x1(y2-y3)+x2(y3-y1)+x3(y1-y2))) to find triangle area.
  • Finally calling the function triangeArea() and print the result to the output.

Python Program

#defing  triangeArea() to find area of traingle using coordinates

def triangeArea(x1,y1,x2,y2,x3,y3):
  Triange_Area = abs((0.5)*(x1*(y2-y3)+x2*(y3-y1)+x3*(y1-y2)))
  print("calculated  Area of the Triangle is :",Triange_Area)
  if Triange_Area == 0:
    print("A Triangle cannot be formed using the input coordinates!")


x1 = int(input("Enter the value of x1 :"))
y1 = int(input("Enter the value of y1 :"))
x2 = int(input("Enter the value of x2 :"))
y2 = int(input("Enter the value of y2 :"))
x3 = int(input("Enter the value of x3 :"))
y3 = int(input("Enter the value of y3 :"))

#calling triangeArea() to find area of traingle


triangeArea(x1,y1,x2,y2,x3,y3)

Output

Enter the value of x1 :0
Enter the value of y1 :0
Enter the value of x2 :7
Enter the value of y2 :8
Enter the value of x3 :9
Enter the value of y3 :4
calculated  Area of the Triangle is : 22.0

3. Python program to find area of triangle using coordinates in class


We have defined a class name Triangle with the method triangeArea(self):

  • We will ask the user to enter all coordinates of the triangle.
  • We have created the object of class Triangle.
  • By using the object of class we are calling the function Triangle_area().

Python Program

#defing  triangeArea() to find area of traingle using coordinates
class Triangle:
    def __init__(self,x1,y1,x2,y2,x3,y3):
        self.x1 = x1
        self.y1 = y1
        self.x2 = x2
        self.y2 = y2
        self.x3 = x3
        self.y3 = y3
    def triangeArea(self):
       Triange_Area = abs((0.5)*(self.x1*(self.y2-self.y3)+self.x2*(self.y3-self.y1)+self.x3*(self.y1-self.y2)))
       print("calculated  Area of the Triangle is :",Triange_Area)
       if Triange_Area == 0:
         print("A Triangle cannot be formed using the input coordinates!")
         


x1 = int(input("Enter the value of x1 :"))
y1 = int(input("Enter the value of y1 :"))
x2 = int(input("Enter the value of x2 :"))
y2 = int(input("Enter the value of y2 :"))
x3 = int(input("Enter the value of x3 :"))
y3 = int(input("Enter the value of y3 :"))

#creating class Triangle object
obj = Triangle(x1,y1,x2,y2,x3,y3)



#calling the class method Triangle_area()
obj.triangeArea()

Output

Enter the value of x1 :0
Enter the value of y1 :0
Enter the value of x2 :7
Enter the value of y2 :8
Enter the value of x3 :9
Enter the value of y3 :4
calculated  Area of the Triangle is : 22.0

Summary

In this post, we have learned Python Program to find the area of triangles using coordinates also covered Python Program to find the area of triangles using coordinates with function Python Program to find the area of triangles using coordinates in class.