Saturday, August 19, 2023

Area of a polygon (Coordinate Geometry)


First, number the vertices in order, going either clockwise or counterclockwise, starting at any vertex. A method for finding the area of any polygon when the coordinates of its vertices are known.

 



Result base on simulation 


Source Code 

def calculate_area(vertices):

    area = 0.0

    n = len(vertices)

    for i in range(n-1):

        area += vertices[i][0] * vertices[i+1][1] - vertices[i+1][0] * vertices[i][1]

    area += vertices[n-1][0] * vertices[0][1] - vertices[0][0] * vertices[n-1][1]

    area = abs(area) * 0.5

    return area

def main():

    while True:

        point = int(input("Input total points in area: "))

        if point >= 3:

            vertices = []

            for i in range(point):

                print(f"\nPoint {i+1}")

                print("=================")

                x = float(input("\t X : "))

                y = float(input("\t Y : "))

                vertices.append((x, y))

                print("=================")

            area = calculate_area(vertices)

            print("\n===========================")

            print(f"Area: {area} m^2")

        else:

            print("\n--------------------------------")

            print("| Need at least three vertices |")

            print("--------------------------------")

        print("\n=======================================")

        option = input("Enter '0' to exit or any other key to calculate again: ")

        print("=======================================")

        if option == "0":

            break

if __name__ == "__main__":

    main()


Share:

0 comments:

Post a Comment