Python program to find area of triangle using class

In this post, we are going to understand the python program to find area of triangle using class using Heron’s Formula and also find area of trinagle along with coordinate using class.

1. Python Program to find area of triangle Using classe


We have defined a class Triangle with a method def Triangle_area() which is used to calculate the area of the triangle Using Heron’s Formula.

#Heron's Formula to calculate Area of a Triangle
 √(s(s-a)(s-b)*(s-c))

  • We will create object of class triangle obj to call Triangle_area() method and passed input argument entered by user to Triangle_area().At last printing the result.

Python Program

class Triangle:
    def __init__(self,a, b, c):
        self.a = a
        self.b = b
        self.c = c

    def Triangle_area(self):
        s = (self.a + self.b + self.c)/2
        return (s*(s-self.a)*(s-self.b)*(s-self.c)) ** 0.5


#input three side from user
a = float(input('Please Enter length of first side: '))
b = float(input('Please Enter length of second side: '))
c = float(input('Please Enter length of third side: '))


#creating object of classTriangle
obj = Triangle(a,b,c)

#calling the class method Triangle_area()
print("Area of triangle : {}".format(obj.Triangle_area()))


Output

Please Enter length of first side: 12
Please Enter length of second side: 13
Please Enter length of third side: 14
Area of triangle : 72.30793524918272

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


We have defined a class name Triangle with the method triangeArea(self) to find area of triangle using coordinates in class

  • In this program,Firstly We will ask the user to enter all coordinates of the triangle.
  • We will create the object(obj) of Triangle class to call method Triangle_area().
  • We are calling the Triangle_area(). and passing the agrumentes enetered by users

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 2 Python programs to find area of triangle using class.