Untitled

 avatar
unknown
plain_text
a year ago
1.3 kB
6
Indexable
import math

def get_inputs():
    n = int(input("Enter the number of sides (3-7): "))
    angles = []
    distances = []
    for i in range(n):
        angle = float(input(f"Enter the interior angle at vertex {i+1} (degrees): "))
        distance = float(input(f"Enter the distance for side {i+1}: "))
        angles.append(angle)
        distances.append(distance)
    return n, angles, distances

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, angles, distances = get_inputs()
    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