geom Documentation¶
About¶
geom (pronounced “jom”) is a a module for working with geometry in a pythonic way. The motivation behind it’s development was to use as a base for collision detection, but the functionality offered by the module offers much more depth.
Reference¶
Functions¶
-
geom.
set_tolerance
(epsilon)¶ Set the error tolerance for which to compare floating point numbers.
TypeError is raised if epsilon isn’t numeric. ValueError is raised if epsilon isn’t positive.
-
geom.
is_numeric
(N)¶ Determine if N is numeric.
N may be a single value or a collection.
Classes¶
Vector¶
-
class
geom.
Vector
(components)¶ A Vector represents a mathematical vector for any dimension.
Overloaded Operations
len(v) gives the dimension of the vector vabs(v) gives the magnitude of the vector v~v gives the normalized version of the vector v-v gives the a vector in the opposite direction but same magnitude as vv[i] gives the vector component in the ith dimension.a == b compare two vectors for equalitya + b adds two vectors togethera - b subtracts the vector b from the vector aa * m multiplies all components of the vector a by a scalar ma / m divides all components of the vector a by a non-zero scalar ma * b computes the cross product of the a with b, where a and bare numeric collections in R3.a @ b computes the dot product of the the vector a and the vector bFor binary operations, as long as one of the arguments is a geom.Vector, the other argument may be any form of numeric collection of the same dimension.
Vector Methods¶
-
Vector.
__init__
(components)¶ Create a vector from components
components should be a collection of numeric values. Initializing a Vector with a collection of non-numeric values will raise a TypeError. ValueError is raised if Vector is initialized with no components.
-
Vector.
mag
()¶ Compute the magnitude of this vector. Equivalent to abs(v).
-
Vector.
magSq
()¶ Compute the square of the magnitude of this vector.
-
Vector.
norm
()¶ Return a normalized version of this vector. Equivalent to ~v.
-
Vector.
normalize
()¶ Normalize this vector.
Similar to v.norm() or ~v, but v.normalize() mutates v instead of returning a new vector. ValueError is raised if this vector is the zero vector.
-
Vector.
add
(other)¶ Return the sum of this vector and the vector other.
Equivalent to v + other. TypeError is raised if other is not a numeric collection the same length as this vector.
-
Vector.
addOn
(other)¶ Add other to this vector.
Similar to v.add(other) or v + other, but v.addOn(other) mutates this v. TypeError is raised if other is not a numeric collection. ValueError is raised if the vectors are not the same length.
-
Vector.
sub
(other)¶ Return the difference of this vector and the vector other.
Equivalent to v - other. TypeError is raised if other is not a numeric collection the same length as this vector.
-
Vector.
takeAway
(other)¶ Subtract the vector other from this vector.
Similar to v.sub(other) or v - other, but v.takeAway(other) mutates this vector. TypeError is raised if other is not a numeric collection the same length as this vector.
-
Vector.
mul
(m)¶ Return the product of this vector and the scalar m.
Equivalent to v * m. TypeError is raised if m is not a number.
-
Vector.
mulBy
(m)¶ Multiply this vector by the scalar m.
Similar to v * m or v.mul(m), but v.mulBy(m) mutates the vector v. TypeError is raised if m isn’t a number.
-
Vector.
div
(m)¶ Return the quotient of this vector the scalar m.
Equivalent to v / m. TypeError is raised if m isn’t a number.
-
Vector.
divBy
(m)¶ Divide this vector by the scalar m.
Similar to v / m or v.div(m), but v.divBy(m) mutates this the vector v. TypeError is raised if m isn’t a number.
-
Vector.
dot
(other)¶ Return the dot product of this vector and other.
Equivalent to v @ other. TypeError is raised if other is not a numeric collection. ValueError is raised if this vector and other are not of the same dimension.
-
Vector.
cross
(other)¶ Return the cross product of this vector and other.
Equivalent to v * other. TypeError is raised if other is not a numeric collection. ValueError is raised if this vector and other are not in R3.
Circle¶
-
class
geom.
Circle
(center, radius)¶ A Circle stores basic circle info and provides relevant useful methods.
-
Circle.
center
¶ The center of the circle.
center is stored as a Vector. Setting center will raise TypeError if it’s not numeric, AttributeError if has no length, and ValueError if it’s not in R2.
-
Circle.
radius
¶ The radius of the circle.
Setting radius will raise TypeError if it’s not numeric and ValueError if it’s negative.
-
Circle.
area
¶ The area of the circle.
Defined as pi*r*r. Setting the area will change the radius of the circle. Setting the area will raise TypeError if it’s not numeric and ValueError if it’s less than zero.
-
Circle.
circumference
¶ The circumference of the circle.
Defined as 2*pi*r. Setting the circumference will change the radius of the circle; raises TypeError if it’s not numeric and ValueError if it’s less than zero.
Circle Methods¶
-
Circle.
__init__
(center, radius)¶ Create a circle with a given
center
andradius
.TypeError is raised if center is not a numeric collection or if radius is not a number. ValueError is raised if center is not in R2 or if radius is not a real non-negative number.
-
Circle.
scaled_to
(m, attr='radius')¶ Return a new circle scaled to m.
By default, m is used to scale the radius. Calling
scaled_to
withattr="area"
cause m to scale the area instead of the radius, and calling it withattr="circumference"
will cauese m to scale the circumference.scaled_to
will raise TypeError if m isn’t numeric or if attr isn’t a string, and ValueError if m is less than zero or if attr isn’t'radius'
,'circumference'
, or'area'
.
-
Circle.
scaled_by
(m, attr='radius')¶ Return a copy of this circle scaled by a factor of m.
By default, m is used to scale the radius. Calling
scaled_by
withattr="area"
cause m to scale the area instead of the radius.scaled_by
will raise TypeError if m isn’t numeric or if attr isn’t a string, and ValueError if m is less than zero or if attr is neither'radius'
nor'area'
.
-
Circle.
moved_to
(position)¶ Return a copy of this circle moved to the given position.
TypeError is raised if position is not a numeric collection. ValueError is raised if position is not in R2.
-
Circle.
moved_by
(vector)¶ Return a copy of this circle moved by the given vector.
TypeError is raised if vector is not a numeric collection. ValueError is raised if position is not in R2.
-
Circle.
intersects
(other)¶ Return true if this circle intersects the geometric object other.
other may be a numeric collection in R2, or some form of a circle. TypeError is raised if other is neither of these things.