Point Classes¶
import introcs
Points have position, but they do not have magnitude or direction. Use the class Vector if you want direction. Points support basic point arithmetic via the operators. However, pay close attention to how we handle typing. For example, the difference between two points is a vector (as it should be). But points may freely convert to vectors and vice versa.
Class Point2¶
This class provides 2-dimensional points. It is the primary geometry class used by
the game2d
package.
Constructor¶
- class introcs.Point2(x=0, y=0)¶
An instance is a point in 2D space.
Attributes¶
- Point2.x¶
The x coordinate
Invariant: Value must be an
int
orfloat
.
- Point2.y¶
The y coordinate
Invariant: Value must be an
int
orfloat
.
Immutable Methods¶
Immutable methods return a new object and do not modify the original.
- Point2.toVector()¶
- Returns
The
Vector2
object equivalent to this point- Return type
Vector2
- Point2.midpoint(other)¶
Computes the midpoint between self and
other
.This method treats
self
andother
as a line segment, so they must both be points.- Parameters
other (
Point2
) – the other end of the line segment- Returns
the midpoint between this point and
other
- Return type
Point2
- Point2.distance(other)¶
Computes the Euclidean between two points
- Parameters
other (
Point2
) – value to compare against- Returns
the Euclidean distance from this point to
other
- Return type
float
- Point2.distance2(other)¶
Computes the squared Euclidean between two points
This method is slightly faster than
distance()
.- Parameters
other (
Point2
) – value to compare against- Returns
the squared Euclidean distance from this point to
other
- Return type
float
- Point2.under(other)¶
Compares
self
toother
under the domination partial orderWe say that one point dominates another is all components of the first are greater than or equal to the components of the second. This is a partial order, not a total one.
- Parameters
other (
Vector2
) – The object to check- Returns
True if
other
dominatesself
; False otherwise- Return type
bool
- Point2.over(other)¶
Compares
self
toother
under the domination partial orderWe say that one point dominates another is all components of the first are greater than or equal to the components of the second. This is a partial order, not a total one.
- Parameters
other (
Vector2
) – The object to check- Returns
True if
self
dominatesother
; False otherwise- Return type
bool
- Point2.isZero()¶
Determines whether or not this object is ‘close enough’ to the origin.
This method uses
allclose()
to test whether the coordinates are “close enough”. It does not require exact equality for floats.- Returns
True if this object is ‘close enough’ to the origin; False otherwise
- Return type
bool
- Point2.interpolant(other, alpha)¶
Interpolates this object with another, producing a new object
The resulting value is:
alpha*self+(1-alpha)*other
according to the rules of addition and scalar multiplication.
- Parameters
other (
Vector2
) – object to interpolate withalpha (
int
orfloat
) – scalar to interpolate by
- Returns
the interpolation of this object and
other
viaalpha
.- Return type
Vector2
- Point2.copy()¶
- Returns
A copy of this point
- Return type
Vector2
- Point2.list()¶
- Returns
A python list with the contents of this point.
- Return type
list
Mutable Methods¶
Mutable methods modify the underlying object.
- Point2.interpolate(other, alpha)¶
Interpolates this object with another in place
This method will modify the attributes of this oject. The new attributes will be equivalent to:
alpha*self+(1-alpha)*other
according to the rules of addition and scalar multiplication.
This method returns this object for chaining.
- Parameters
other (
Vector2
) – object to interpolate withalpha (
int
orfloat
) – scalar to interpolate by
- Returns
This object, newly modified
- Point2.clamp(low, high)¶
Clamps this point to the range [
low
,high
].Any value in this tuple less than
low
is set tolow
. Any value greater thanhigh
is set tohigh
.This method returns this object for chaining.
- Parameters
low (
int
orfloat
) – The low range of the clamphigh (
int
orfloat
) – The high range of the clamp
- Returns
This object, newly modified
- Return type
Vector2
Operators¶
Operators redefine the meaning of the basic operations. For example:: p + q
is
the same as p.__add__(q)
. This allows us to treat points like regular numbers.
For the sake of brevity, we have not listed all operators – only the most important
ones. The equivalences are as follows:
p == q --> p.__eq__(q)
p < q --> p.__lt__(q)
p + q --> p.__add__(q)
p - q --> p.__sub__(q)
p * q --> p.__mul__(q)
q * p --> p.__rmul__(q)
p / q --> p.__truediv__(q)
q / p --> p.__rtruediv__(q)
- Point2.__eq__(other)¶
Compares this point with
other
This method uses
allclose()
to test whether the coordinates are “close enough”. It does not require exact equality for floats. Equivalence also requires type equivalence.- Parameters
other (
any
) – The object to check- Returns
True if
self
andother
are equivalent- Return type
bool
- Point2.__lt__(other)¶
Compares the lexicographic ordering of
self
andother
.Lexicographic ordering checks the x-coordinate first, and then y.
- Parameters
other (
Vector2
) – The object to check- Returns
True if
self
is lexicographic kess thanother
- Return type
float
- Point2.__add__(other)¶
Performs a context dependent addition of this point and
other
.If
other
is a point, the result is the vector from this position toother
(soother
is the head). If it is a vector, it is the point at the head of the vector when it is anchored at this point.- Parameters
other (
Point2
orVector2
) – object to add- Returns
the sum of this object and
other
.- Return type
Point2
orVector2
- Point2.__sub__(other)¶
Performs a context dependent subtraction of this point and
other
.If
other
is a point, the result is the vector fromother
to this position (soother
is the tail). If it is a vector, it is the point at the tail of the vector whose head is at this point.- Parameters
other (
Point2
orVector2
) – object to subtract- Returns
the difference of this object and
other
.- Return type
Point2
orVector2
- Point2.__mul__(value)¶
Multiples this object by a scalar,
Vector2
, or aMatrix
, producing a new object.The exact effect is determined by the type of value. If
value
is a scalar, the result is standard scalar multiplication. If it is a point, then the result is pointwise multiplication. Finally, if is a matrix, then we use the matrix to transform the object. We treat matrix transformation as multiplication on the right to make in-place multiplication easier. SeeMatrix
doe more- Parameters
value (
int
,float
,Vector2
orMatrix
) – value to multiply by- Returns
the altered object
- Return type
Vector2
- Point2.__rmul__(value)¶
Multiplies this object by a scalar or
Vector2
on the left.The exact effect is determined by the type of value. If
value
is a scalar, the result is standard scalar multiplication. If it is a 2d tuple, then the result is pointwise multiplication. We do not allow matrix multiplication on the left.- Parameters
value (
int
,float
, orVector2
) – The value to multiply by- Returns
the scalar multiple of
self
andscalar
- Return type
Vector2
- Point2.__truediv__(value)¶
Divides this object by a scalar or a
Vector2
on the right, producting a new object.The exact effect is determined by the type of value. If
value
is a scalar, the result is standard scalar division. If it is aVector2
, then the result is pointwise division.The value returned has the same type as
self
(so ifself
is an instance of a subclass, it uses that object instead of the original class. The contents of this object are not altered.- Parameters
value (
int
,float
, orVector2
) – The value to multiply by- Returns
the division of
self
byvalue
- Return type
Vector2
- Point2.__rtruediv__(value)¶
Divides a scalar or
Vector2
by this object.Dividing by a point means pointwise reciprocation, followed by multiplication.
- Parameters
value (
int
,float
, orVector2
) – The value to divide- Returns
the division of
value
byself
- Return type
Class Point3¶
This class provides 3-dimensional points. It will not be used by this assignment. However,
the name Point
is an alias for Point3
.
Constructor¶
- class introcs.Point3(x=0, y=0, z=0)¶
An instance is a point in 3D space.
Attributes¶
- Point3.x¶
The x coordinate
Invariant: Value must be an
int
orfloat
.
- Point3.y¶
The y coordinate
Invariant: Value must be an
int
orfloat
.
Immutable Methods¶
Immutable methods return a new object and do not modify the original.
- Point3.toVector()¶
- Returns
The
Vector3
object equivalent to this point- Return type
Vector3
- Point3.midpoint(other)¶
Computes the midpoint between self and
other
.This method treats
self
andother
as a line segment, so they must both be points.- Parameters
other (
Point3
) – the other end of the line segment- Returns
the midpoint between this point and
other
- Return type
Point3
- Point3.distance(other)¶
Computes the Euclidean between two points
- Parameters
other (
Point3
) – value to compare against- Returns
the Euclidean distance from this point to
other
- Return type
float
- Point3.distance2(other)¶
Computes the squared Euclidean between two points
This method is slightly faster than
distance()
.- Parameters
other (
Point3
) – value to compare against- Returns
the squared Euclidean distance from this point to
other
- Return type
float
- Point3.under(other)¶
Compares
self
toother
under the domination partial orderWe say that one point dominates another is all components of the first are greater than or equal to the components of the second. This is a partial order, not a total one.
- Parameters
other (
Vector3
) – The object to check- Returns
True if
other
dominatesself
; False otherwise- Return type
bool
- Point3.over(other)¶
Compares
self
toother
under the domination partial orderWe say that one point dominates another is all components of the first are greater than or equal to the components of the second. This is a partial order, not a total one.
- Parameters
other (
Vector3
) – The object to check- Returns
True if
self
dominatesother
; False otherwise- Return type
bool
- Point3.isZero()¶
Determines whether or not this object is ‘close enough’ to the origin.
This method uses
allclose()
to test whether the coordinates are “close enough”. It does not require exact equality for floats.- Returns
True if this object is ‘close enough’ to the origin; False otherwise
- Return type
bool
- Point3.interpolant(other, alpha)¶
Interpolates this object with another, producing a new object
The resulting value is:
alpha*self+(1-alpha)*other
according to the rules of addition and scalar multiplication.
- Parameters
other (
Vector3
) – object to interpolate withalpha (
int
orfloat
) – scalar to interpolate by
- Returns
the interpolation of this object and
other
viaalpha
.- Return type
Vector3
- Point3.copy()¶
- Returns
A copy of this point
- Return type
Vector3
- Point3.list()¶
- Returns
A python list with the contents of this point.
- Return type
list
Mutable Methods¶
Mutable methods modify the underlying object.
- Point3.interpolate(other, alpha)¶
Interpolates this object with another in place
This method will modify the attributes of this oject. The new attributes will be equivalent to:
alpha*self+(1-alpha)*other
according to the rules of addition and scalar multiplication.
This method returns this object for chaining.
- Parameters
other (
Vector3
) – object to interpolate withalpha (
int
orfloat
) – scalar to interpolate by
- Returns
This object, newly modified
- Point3.clamp(low, high)¶
Clamps this point to the range [
low
,high
].Any value in this tuple less than
low
is set tolow
. Any value greater thanhigh
is set tohigh
.This method returns this object for chaining.
- Parameters
low (
int
orfloat
) – The low range of the clamphigh (
int
orfloat
) – The high range of the clamp
- Returns
This object, newly modified
- Return type
Vector3
Operators¶
Operators redefine the meaning of the basic operations. For example:: p + q
is
the same as p.__add__(q)
. This allows us to treat points like regular numbers.
For the sake of brevity, we have not listed all operators – only the most important
ones. The equivalences are as follows:
p == q --> p.__eq__(q)
p < q --> p.__lt__(q)
p + q --> p.__add__(q)
p - q --> p.__sub__(q)
p * q --> p.__mul__(q)
q * p --> p.__rmul__(q)
p / q --> p.__truediv__(q)
q / p --> p.__rtruediv__(q)
- Point3.__eq__(other)¶
Compares this point with
other
This method uses
allclose()
to test whether the coordinates are “close enough”. It does not require exact equality for floats. Equivalence also requires type equivalence.- Parameters
other (
any
) – The object to check- Returns
True if
self
andother
are equivalent- Return type
bool
- Point3.__lt__(other)¶
Compares the lexicographic ordering of
self
andother
.Lexicographic ordering checks the x-coordinate first, and then y.
- Parameters
other (
Vector3
) – The object to check- Returns
True if
self
is lexicographic kess thanother
- Return type
float
- Point3.__add__(other)¶
Performs a context dependent addition of this point and other.
If
other
is a point, the result is the vector from this position toother
(soother
is the head). If it is a vector, it is the point at the head of the vector when it is anchored at this point.- Parameters
other (
Point2
orVector2
) – object to add- Returns
the sum of this object and
other
.- Return type
Point2
orVector2
- Point3.__sub__(other)¶
Performs a context dependent subtraction of this point and other.
If
other
is a point, the result is the vector fromother
to this position (soother
is the tail). If it is a vector, it is the point at the tail of the vector whose head is at this point.- Parameters
other (
Point3
orVector3
) – object to subtract- Returns
the difference of this object and
other
.- Return type
Point3
orVector3
- Point3.__mul__(value)¶
Multiples this object by a scalar,
Vector3
, or aMatrix
, producing a new object.The exact effect is determined by the type of value. If
value
is a scalar, the result is standard scalar multiplication. If it is a point, then the result is pointwise multiplication. Finally, if is a matrix, then we use the matrix to transform the object. We treat matrix transformation as multiplication on the right to make in-place multiplication easier. SeeMatrix
doe more- Parameters
value (
int
,float
,Vector3
orMatrix
) – value to multiply by- Returns
the altered object
- Return type
Vector3
- Point3.__rmul__(value)¶
Multiplies this object by a scalar or
Vector3
on the left.The exact effect is determined by the type of value. If
value
is a scalar, the result is standard scalar multiplication. If it is a 2d tuple, then the result is pointwise multiplication. We do not allow matrix multiplication on the left.- Parameters
value (
int
,float
, orVector3
) – The value to multiply by- Returns
the scalar multiple of
self
andscalar
- Return type
Vector3
- Point3.__truediv__(value)¶
Divides this object by a scalar or a
Vector3
on the right, producting a new object.The exact effect is determined by the type of value. If
value
is a scalar, the result is standard scalar division. If it is aVector3
, then the result is pointwise division.The value returned has the same type as
self
(so ifself
is an instance of a subclass, it uses that object instead of the original class. The contents of this object are not altered.- Parameters
value (
int
,float
, orVector3
) – The value to multiply by- Returns
the division of
self
byvalue
- Return type
Vector3
- Point3.__rtruediv__(value)¶
Divides a scalar or
Vector3
by this object.Dividing by a point means pointwise reciprocation, followed by multiplication.
- Parameters
value (
int
,float
, orVector3
) – The value to divide- Returns
the division of
value
byself
- Return type