Untitled

 avatar
unknown
plain_text
a year ago
996 B
12
Indexable
import math

def calculate_area(n, angles, distances):
    vertices = [(0, 0), (distances[0], 0)]  # Start with the first two vertices
    angle_from_horizontal = 0
    for i in range(1, n-1):
        angle_from_horizontal += 180 - angles[i-1]
        radians = math.radians(angle_from_horizontal)
        x = vertices[-1][0] + distances[i] * math.cos(radians)
        y = vertices[-1][1] + distances[i] * math.sin(radians)
        vertices.append((x, y))
    # Use shoelace formula to calculate area
    area = 0
    for i in range(-1, n-1):
        area += vertices[i][0] * vertices[i+1][1]
        area -= vertices[i+1][0] * vertices[i][1]
    area = abs(area) / 2
    return area

def main():
    n = 4  # Number of sides
    angles = [90, 110, 90, 70]  # Interior angles in degrees
    distances = [5, 8, 5, 8]  # Distances of each side

    area = calculate_area(n, angles, distances)
    print(f"The area of the polygon is: {area:.2f} square units")

if __name__ == "__main__":
    main()
Editor is loading...
Leave a Comment