parallelogram.py

Created by matt-numworks

Created on January 09, 2026

949 Bytes

Calculates the area of a parallelogram given the coordinates (x,y,z) of three points.


import numpy as np

def get_vector(name):
    while True:
        raw = input("Enter vector " + name + " as three \n comma-separated numbers \n(e.g., 1, 2, 3): ")
        try:
            parts = [float(x.strip()) for x in raw.split(",")]
            if len(parts) != 3:
                print("Please enter exactly three values.")
                continue
            return parts
        except ValueError:
            print("Invalid input. Make sure all entries are numbers.")

print("Before we begin, remember:\n q must lie between p and r \n along the same line.\n")

p = get_vector("p")
q = get_vector("q")
r = get_vector("r")

def a(p, q, r):
    PQ = np.array(q) - np.array(p)
    PR = np.array(r) - np.array(p)
    cross_product = np.cross(PQ, PR)
    area = np.sqrt(np.sum(cross_product**2))
    return area

area = a(p, q, r)
print("\nThe area of the parallelogram \n formed by PQ and PR is: \n" + str(round(area,3)))

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.