area_np.py

Created by tlake

Created on June 20, 2023

822 Bytes

Computes the area of a polygon of any shape


# area_np
from math import *

def load_pts():
    pts=[]
    pts.append([4.5,-1.5])
    pts.append([8,2])
    pts.append([.5,4])
    pts.append([-1.5,.5])
    pts.append([.5,-2])
    pts.append([1,1])
    return pts

def area_pts(pts):
    area=0
    pts.append(pts[0])
    for i in range(len(pts)-1):
        a=pts[i][0]+pts[i+1][0]
        b=pts[i][1]-pts[i+1][1]
        area+=a*b
    return abs(area/2)

def ask_pts():
    pts=[]
    while True:
        try:
            print("Coordinate ",len(pts)+1)
            x=float(input("x: "))
            y=float(input("y: "))
            pts.append([x,y])
        except:
            break
    return pts

print("1. ask_pts")
print("2. load_pts")
n=int(input("? "))
if n==1:
    pts=ask_pts()
else:
    pts=load_pts()
area=area_pts(pts)
print("Area: ",area)

During your visit to our site, NumWorks needs to install "cookies" or use other technologies to collect data about you in order to:

With the exception of Cookies essential to the operation of the site, NumWorks leaves you the choice: you can accept Cookies for audience measurement by clicking on the "Accept and continue" button, or refuse these Cookies by clicking on the "Continue without accepting" button or by continuing your browsing. You can update your choice at any time by clicking on the link "Manage my cookies" at the bottom of the page. For more information, please consult our cookies policy.