Program to Calculate Area of any Triangle using its Coordinates

Python Program to Calculate Area of any Triangle using its Coordinates

Enhancing programming skills is very important no matter what language you have chosen. So, practice frequently with these simple java programs examples and excel in coding the complex logic.

GIven three coordinates of a triangle the task is to find the area of the given Triangle in Python.

Examples:

Example1:

Input:

x coordinate of the first point = 6
y coordinate of the first point = 2
x coordinate of the second point = 9
y coordinate of the first point = 11
x coordinate of the third point = 5
y coordinate of the first point = 17

Output:

The Area of the triangle with the given coordinates  (6,2) (9,11) (5,17) = 27.0

Example2:

Input:

x coordinate of the first point = 3
y coordinate of the first point = 7
x coordinate of the second point = 2
y coordinate of the first point = 11
x coordinate of the third point = 9
y coordinate of the first point = 7

Output:

The Area of the triangle with the given cordinates (3,7) (2,11) (9,7) = 12.0

Program to Calculate Area of any Triangle using its Coordinates in

Python

Below are the ways to calculate to find the area of the given Triangle in Python.

Let’s look for the formula before we start coding in Python.

Let the coordinates of a triangle be A(x1, y1), B(x2, y2), and C(x3, y3). Using the Mathematical formula, we can compute the area of triangle ABC.

Area of Triangle = |(1/2)*(x1*(y2-y3)+x2*(y3-y1)+x3*(y1-y2))|

Calculating the Area without considering the modulus may result in a negative value. We only take the magnitude by applying modulus to the expression because Area cannot be negative.

In the program, we utilize the abs() method to get the absolute value or magnitude.

Method #1: Using Mathematical Formula (Static Input)

Approach:

  • Give the 3 coordinates as static input and store them in 6 separate variables.
  • Apply the given mathematical formula for the above coordinates.
  • Calculate the absolute value of the area of the triangle using the abs() function.
  • If the value of Area is zero, we can state that the input coordinates cannot be used to build a triangle.
  • Check whether the given area of the triangle is 0 using the If statement.
  • If it is true then the print Triangle cannot be formed using the given coordinates.
  • The Exit of the Program.

Below is the implementation:

# Give the 3 coordinates as static input and store them in 6 separate variables.
xcor1 = 3
ycor1 = 7
xcor2 = 2
ycor2 = 11
xcor3 = 9
ycor3 = 7
# Apply the given mathematical formula for the above coordinates.
# Calculate the absolute value of the area of the triangle using the abs() function.
TriangleArea = abs((0.5)*(xcor1*(ycor2-ycor3)+xcor2 *
                          (ycor3-ycor1)+xcor3*(ycor1-ycor2)))
# Check whether the given area of the triangle is 0 using the If statement.
# If it is true then the print Triangle cannot be formed using the given coordinates.
if (TriangleArea == 0):
    print("Triangle cannot be formed using the given coordinates ")
print('The Area of the triangle with the given coordinates ('+str(xcor1)+','+str(ycor1) +
      ')' + ' ('+str(xcor2)+','+str(ycor2)+')' + ' ('+str(xcor3)+','+str(ycor3)+')', '=', TriangleArea)

Output:

The Area of the triangle with the given cordinates (3,7) (2,11) (9,7) = 12.0

Method #2: Using Mathematical Formula (User Input)

Approach:

  • Give the 3 coordinates as user input using map(),int(),split() functions.
  • Store them in 6 separate variables.
  • Apply the given mathematical formula for the above coordinates.
  • Calculate the absolute value of the area of the triangle using the abs() function.
  • If the value of Area is zero, we can state that the input coordinates cannot be used to build a triangle.
  • Check whether the given area of the triangle is 0 using the If statement.
  • If it is true then the print Triangle cannot be formed using the given coordinates.
  • The Exit of the Program.

Below is the implementation:

# Give the 3 coordinates as user input using map(),int(),split() functions.
# Store them in 6 separate variables.
xcor1 = int(input('Enter some random x coordinate of the first point = '))
ycor1 = int(input('Enter some random y coordinate of the first point = '))
xcor2 = int(input('Enter some random x coordinate of the second point = '))
ycor2 = int(input('Enter some random y coordinate of the first point = '))
xcor3 = int(input('Enter some random x coordinate of the third point = '))
ycor3 = int(input('Enter some random y coordinate of the first point = '))
# Apply the given mathematical formula for the above coordinates.
# Calculate the absolute value of the area of the triangle using the abs() function.
TriangleArea = abs((0.5)*(xcor1*(ycor2-ycor3)+xcor2 *
                          (ycor3-ycor1)+xcor3*(ycor1-ycor2)))
# Check whether the given area of the triangle is 0 using the If statement.
# If it is true then the print Triangle cannot be formed using the given coordinates.
if (TriangleArea == 0):
    print("Triangle cannot be formed using the given coordinates")
print('The Area of the triangle with the given coordinates  ('+str(xcor1)+','+str(ycor1) +
      ')' + ' ('+str(xcor2)+','+str(ycor2)+')' + ' ('+str(xcor3)+','+str(ycor3)+')', '=', TriangleArea)

Output:

Enter some random x coordinate of the first point = 6
Enter some random y coordinate of the first point = 2
Enter some random x coordinate of the second point = 9
Enter some random y coordinate of the first point = 11
Enter some random x coordinate of the third point = 5
Enter some random y coordinate of the first point = 17
The Area of the triangle with the given coordinates  (6,2) (9,11) (5,17) = 27.0

Related Programs: