From dc11efd60a9d1323b6f3ad34f267b714511871a7 Mon Sep 17 00:00:00 2001 From: Matt Low Date: Sat, 14 Nov 2020 22:10:04 +0400 Subject: [PATCH] Add docstrings to math.py --- geom/math.py | 37 +++++++++++++++++++++++++++++++++++++ 1 file changed, 37 insertions(+) diff --git a/geom/math.py b/geom/math.py index 98a2bee..417ce9f 100644 --- a/geom/math.py +++ b/geom/math.py @@ -2,27 +2,64 @@ import math ZERO = (0, 0) + def dot(x1, y1, x2, y2): + """Returns the dot product of two vectors.""" return (x1 * x2) + (y1 * y2) + def length(x, y): + """Returns the magnitude of a vector.""" return math.sqrt((x * x) + (y * y)) def normalize(x, y): + """Returns the unit vector pointed in the same direction as [x, y].""" _len = length(x, y) return x / _len, y / _len + +def lnormal(x, y): + """Returns the left-hand normal of a vector.""" + return -y, x + + +def rnormal(x, y): + """Returns the right-hand normal of a vector.""" + return y, -x + + +def reverse(x, y): + """Returns a vector pointing in the opposite direction.""" + return -x, -y + + def scale(x, y, factor): + """Scale a vector by a given factor.""" return x * factor, y * factor + def add(x1, y1, x2, y2): + """Add two vectors.""" return x1 + x2, y1 + y2 + def sub(x1, y1, x2, y2): + """Subtract a vector from another.""" return x1 - x2, y1 - y2 + def vmin(x1, y1, x2, y2): + """Return the minimum [x, y] components of the given vectors.""" return min(x1, x2), min(y1, y2) + def vmax(x1, y1, x2, y2): + """Return the maximum [x, y] components of the given vectors.""" return max(x1, x2), max(y1, y2) + + +def is_equal(x1, y1, x2, y2): + """Returns true if two unit vectors are pointed in either the same or + opposite directions. + """ + return math.isclose(abs(dot(x1, y1, x2, y2)), 1, rel_tol=1e-03)