Add docstrings to math.py
This commit is contained in:
		
							
								
								
									
										37
									
								
								geom/math.py
									
									
									
									
									
								
							
							
						
						
									
										37
									
								
								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)
 | 
			
		||||
 | 
			
		||||
		Reference in New Issue
	
	Block a user