Python Program for Area of a Polygon with Given n Ordered Vertices

In the previous article, we have discussed Python Program for How To End Program
Given two lists which are the ordered coordinates of a polygon with n vertices, the task is to find the area of a given polygon.

Ordered means that the coordinates are given either clockwise or anticlockwise from the first vertex to the last.

Examples:

Example1:

Input:

Given x-coordinates list = [1, 5, 1]
Given y-coordinates list = [6, 4, 3]

Output:

The Area of the given polygon for the given n ordered vertices :
6

Example2:

Input:

Given x-coordinates list = [2, 3, 3, 1]
Given y-coordinates list = [9, 2, 1, 2]

Output:

The Area of the given polygon for the given n ordered vertices :
8

Program for Area of a Polygon with Given n Ordered Vertices in Python

Below are the ways to find the area of a given polygon for the given n ordered vertices in python:

Method #1: Using Mathematical Formula (Static Input)

Approach:

  • Give the x-coordinates list as static input and store it in a variable.
  • Give the y-coordinates list as static input and store it in another variable.
  • Calculate the length of the given x-coordinates list and store it in another variable.
  • Take a variable say polygn_area and initialize its value to 0.
  • Subtract 1 from the above x-coordinates list length and store it in another variable say k .
  • Loop till the length of the given x-coordinates list using the for loop.
  • Calculate the area of the polygon using the above given mathematical formula and add it to the above-initialized variable polygn_area¬†
  • Assign the value of the iterator to the k which is the previous vertex to the iterator.
  • Divide the above polygn_area by 2, apply the abs() function to it which gives the absolute value, and convert it to an integer using the int() function.
  • Store it in another variable.
  • Print the above result which is the area of the given polygon.
  • The Exit of the Program.

Below is the implementation:

# Give the x-coordinates list as static input and store it in a variable.
a = [1, 5, 1]
# Give the y-coordinates list as static input and store it in another variable.
b = [6, 4, 3]
# Calculate the length of the given x-coordinates list and store it in another variable.
lst_lengt = len(a)
# Take a variable say polygn_area and initialize its value to 0.
polygn_area = 0
# Subtract 1 from the above x-coordinates list length and store it in another variable
# say k .
k = lst_lengt - 1
print("The Area of the given polygon for the given n ordered vertices :")
# Loop till the length of the given x-coordinates list using the for loop.
for itr in range(0, lst_lengt):
    # Calculate the area of the polygon using the above given mathematical formula and
    # add it to the above-initialized variable polygn_area.
    polygn_area += (a[k] + a[itr]) * (b[k] - b[itr])
    # Assign the value of the iterator to the k which is the previous vertex
    # to the iterator.
    k = itr
# Divide the above polygn_area by 2, apply the abs() function to it which gives
# the absolute value, and convert it to an integer using the int() function.
# Store it in another variable.
rslt = int(abs(polygn_area / 2.0))
# Print the above result which is the area of the given polygon.
print(rslt)

Output:

The Area of the given polygon for the given n ordered vertices :
6

Method #2: Using Mathematical Formula (User Input)

Approach:

  • Give the x-coordinates list as user input using list(),map(),input(),and split() functions.
  • Store it in a variable.
  • Give the y-coordinates list as user input using list(),map(),input(),and split() functions.
  • Store it in another variable.
  • Calculate the length of the given x-coordinates list and store it in another variable.
  • Take a variable say polygn_area and initialize its value to 0.
  • Subtract 1 from the above x-coordinates list length and store it in another variable say k .
  • Loop till the length of the given x-coordinates list using the for loop.
  • Calculate the area of the polygon using the above given mathematical formula and add it to the above-initialized variable polygn_area¬†
  • Assign the value of the iterator to the k which is the previous vertex to the iterator.
  • Divide the above polygn_area by 2, apply the abs() function to it which gives the absolute value, and convert it to an integer using the int() function.
  • Store it in another variable.
  • Print the above result which is the area of the given polygon.
  • The Exit of the Program.

Below is the implementation:

# Give the x-coordinates list as user input using list(),map(),input(),and split() functions.
# Store it in a variable.
a = list(map(int, input( 'Enter some random List Elements separated by spaces = ').split()))
# Give the y-coordinates list as user input using list(),map(),input(),and split() functions.
# Store it in another variable.
b = list(map(int, input( 'Enter some random List Elements separated by spaces = ').split()))
# Calculate the length of the given x-coordinates list and store it in another variable.
lst_lengt = len(a)
# Take a variable say polygn_area and initialize its value to 0.
polygn_area = 0
# Subtract 1 from the above x-coordinates list length and store it in another variable
# say k .
k = lst_lengt - 1
print("The Area of the given polygon for the given n ordered vertices :")
# Loop till the length of the given x-coordinates list using the for loop.
for itr in range(0, lst_lengt):
    # Calculate the area of the polygon using the above given mathematical formula and
    # add it to the above-initialized variable polygn_area.
    polygn_area += (a[k] + a[itr]) * (b[k] - b[itr])
    # Assign the value of the iterator to the k which is the previous vertex
    # to the iterator.
    k = itr
# Divide the above polygn_area by 2, apply the abs() function to it which gives
# the absolute value, and convert it to an integer using the int() function.
# Store it in another variable.
rslt = int(abs(polygn_area / 2.0))
# Print the above result which is the area of the given polygon.
print(rslt)

Output:

Enter some random List Elements separated by spaces = 2 3 3 1
Enter some random List Elements separated by spaces = 9 2 1 2
The Area of the given polygon for the given n ordered vertices :
8