Point2

import introcs

Points have position, but they do not have magnitude or direction. Use the class Vector2 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.

Constructor

class introcs.Point2(x=0, y=0)

An instance is a point in 2D space.

All attribute values are 0.0 by default.

Parameters
  • x (int or float) – initial x value

  • y (int or float) – initial y value

Attributes

Point2.x

The x coordinate

Invariant: Value must be an int or float.

Point2.y

The y coordinate

Invariant: Value must be an int or float.

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 and other 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 to other under the domination partial order

We 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 dominates self; False otherwise

Return type

bool

Point2.over(other)

Compares self to other under the domination partial order

We 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 dominates other; 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 with

  • alpha (int or float) – scalar to interpolate by

Returns

the interpolation of this object and other via alpha.

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 with

  • alpha (int or float) – 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 to low. Any value greater than high is set to high.

This method returns this object for chaining.

Parameters
  • low (int or float) – The low range of the clamp

  • high (int or float) – 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 and other are equivalent

Return type

bool

Point2.__lt__(other)

Compares the lexicographic ordering of self and other.

Lexicographic ordering checks the x-coordinate first, and then y.

Parameters

other (Vector2) – The object to check

Returns

True if self is lexicographic kess than other

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 to other (so other 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 or Vector2) – object to add

Returns

the sum of this object and other.

Return type

Point2 or Vector2

Point2.__sub__(other)

Performs a context dependent subtraction of this point and other.

If other is a point, the result is the vector from other to this position (so other 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 or Vector2) – object to subtract

Returns

the difference of this object and other.

Return type

Point2 or Vector2

Point2.__mul__(value)

Multiples this object by a scalar, Vector2, or a Matrix, 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. See Matrix doe more

Parameters

value (int, float, Vector2 or Matrix) – 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, or Vector2) – The value to multiply by

Returns

the scalar multiple of self and scalar

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 a Vector2, then the result is pointwise division.

The value returned has the same type as self (so if self 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, or Vector2) – The value to multiply by

Returns

the division of self by value

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, or Vector2) – The value to divide

Returns

the division of value by self

Return type