w3resource

Python: Compute the area of the polygon

Python Basic - 1: Exercise-59 with Solution

A convex polygon is a simple polygon in which no line segment between two points on the boundary ever goes outside the polygon. Equivalently, it is a simple polygon whose interior is a convex set. In a convex polygon, all interior angles are less than or equal to 180 degrees, while in a strictly convex polygon all interior angles are strictly less than 180 degrees.

Write a Python program that compute the area of the polygon . The vertices have the names vertex 1, vertex 2, vertex 3, ... vertex n according to the order of edge connections.

Input:
Input number of sides: 5
Side: 1
Input the Coordinate:
Input Coordinate x: 1
Input Coordinate y: 0
Side: 2
Input the Coordinate:
Input Coordinate x: 0
Input Coordinate y: 0
Side: 3
Input the Coordinate:
Input Coordinate x: 1
Input Coordinate y: 1
Side: 4
Input the Coordinate:
Input Coordinate x: 2
Input Coordinate y: 0
Side: 5
Input the Coordinate:
Input Coordinate x: -1
Input Coordinate y: 1
Area of the Polygon: 0.5

Python: Compute the area of the polygon rules

Visual Presentation:

Python: Compute the area of the polygon

Sample Solution:

Python Code:

# Define a function 'poly_area' to calculate the area of a polygon
def poly_area(c):
    # Initialize an empty list 'add' to store intermediate values
    add = []
    
    # Use a loop to iterate through the coordinates
    for i in range(0, (len(c) - 2), 2):
        # Calculate and append the cross product of consecutive coordinate pairs
        add.append(c[i] * c[i + 3] - c[i + 1] * c[i + 2])
    
    # Calculate and append the cross product of the last and first coordinate pairs
    add.append(c[len(c) - 2] * c[1] - c[len(c) - 1] * c[0])
    
    # Return the absolute value of half of the sum of all cross products
    return abs(sum(add) / 2)

# Prompt the user to input the number of sides of the polygon
no_sides = int(input('Input number of sides: '))
# Initialize an empty list 'cord_data' to store coordinates
cord_data = []

# Use a loop to input coordinates for each side of the polygon
for z in range(no_sides):
    print("Side:", z+1)
    print("Input the Coordinate:")
    # Input x-coordinate
    x = int(input('Input Coordinate x:'))
    # Input y-coordinate
    y = int(input('Input Coordinate y:'))
    # Append the coordinates to 'cord_data'
    cord_data.append(x)
    cord_data.append(y)

# Print the area of the polygon using the 'poly_area' function
print("\nArea of the Polygon:", poly_area(cord_data))

Sample Output:

Input number of sides:  5
Side: 1
Input the Coordinate:
Input Coordinate x: 1
Input Coordinate y: 0
Side: 2
Input the Coordinate:
Input Coordinate x: 0
Input Coordinate y: 0
Side: 3
Input the Coordinate:
Input Coordinate x: 1
Input Coordinate y: 1
Side: 4
Input the Coordinate:
Input Coordinate x: 2
Input Coordinate y: 0
Side: 5
Input the Coordinate:
Input Coordinate x: -1
Input Coordinate y: 1

Area of the Polygon: 0.5

Explanation:

Here is a breakdown of the above Python code:

  • Define a function "poly_area()" to calculate the area of a polygon using the shoelace formula.
  • Initialize an empty list 'add' to store intermediate values.
  • Use a loop to calculate and append the cross product of consecutive coordinate pairs.
  • Calculate and append the cross product of the last and first coordinate pairs.
  • Return the absolute value of half of the sum of all cross products.
  • Prompt the user to input the number of sides of the polygon.
  • Initialize an empty list 'cord_data' to store coordinates.
  • Use a loop to input coordinates for each side of the polygon.
  • Print the area of the polygon using the "poly_area()" function.

Flowchart:

Flowchart: Python - Compute the area of the polygon

Python Code Editor:

Have another way to solve this solution? Contribute your code (and comments) through Disqus.

Previous: Write a Python program to restore the original string by entering the compressed string with this rule. However, the # character does not appear in the restored character string.
Next: Write a Python program to cut out words of 3 to 6 characters length from a given sentence not more than 1024 characters.

What is the difficulty level of this exercise?

Test your Programming skills with w3resource's quiz.



Become a Patron!

Follow us on Facebook and Twitter for latest update.

It will be nice if you may share this link in any developer community or anywhere else, from where other developers may find this content. Thanks.

https://198.211.115.131/python-exercises/basic/python-basic-1-exercise-59.php