geo2d.geometry.Vector

class geo2d.geometry.Vector(*args, **kwargs)[source]

An abstract Vector object.

It’s defined by x, y components or rho (length) and phi (angle relative to X axis in radians).

Parameters :

*args : {two scalars, vector, point, (list, tuple of length 2)}

Given coordinates, args compose the vector components. If the Cartesian coordinates are given, the Polar are calculated and vice-versa. If args is of Vector type then all of the other arguments are ignored and we create a Vector copy of the given parameter. It can also be Point-like element; if there are two Point-like elements given then the vector will have rho equal to the distance between the two points and the direction of point1 -> point2 (i.e. args[0] -> args[1]). If only one Point-like is given then this object’s x and y values are used, having obviously the direction Point(0, 0) -> Point(x, y).

**kwargs : coordinates={“cartesian”, “polar”}, optional

If cartesian then arg1 is x and arg2 is y components, else if polar then arg1 is rho and arg2 is phi (in radians).

Raises :

TypeError :

In case args is not the correct type(Vector, two scalars or point-like).

Attributes

normalized [Vector] Get a normalized self.
phi [scalar] Get the angle (radians).
rho [scalar] Get the length of the Vector (polar coordinates).
x [scalar] Get the x component of the Vector.
y [scalar] Get the y component of the Vector.

Methods

__add__(vector) Add two vectors.
__contains__(x) Searches for x in “itself”.
__getitem__(idx) Return values as a list for easier acces some times.
__len__() The length of a Vector is 2.
__mul__(arg) Calculates the dot product with another Vector, or
__neg__() Turns self to 180 degrees and returns the new Vector.
__sub__(vector) Subtraction of two vectors.
_calculate_cartesian_coords() Helper function for internally calculating self.x and self.y.
_calculate_polar_coords() Helper function for internally calculating self.rho and self.phi.
cross(arg) Calculates the cross product with another Vector, as defined
dot(arg) Calculates the dot product with another Vector, or
intersection(obj) Return points of intersection if any.
parallel_to(obj) Is self parallel with obj?
perpendicular_to(obj) Is self perpendicular to obj?
random_direction() Create a randomly oriented Vector (with phi in the
rotate(theta[, angle]) Rotate self by theta degrees.
translate(*args) Dummy function since it doesn’t make sense to translate a

Detailed description

class geo2d.geometry.Vector(*args, **kwargs)[source]

An abstract Vector object.

It’s defined by x, y components or rho (length) and phi (angle relative to X axis in radians).

Parameters :

*args : {two scalars, vector, point, (list, tuple of length 2)}

Given coordinates, args compose the vector components. If the Cartesian coordinates are given, the Polar are calculated and vice-versa. If args is of Vector type then all of the other arguments are ignored and we create a Vector copy of the given parameter. It can also be Point-like element; if there are two Point-like elements given then the vector will have rho equal to the distance between the two points and the direction of point1 -> point2 (i.e. args[0] -> args[1]). If only one Point-like is given then this object’s x and y values are used, having obviously the direction Point(0, 0) -> Point(x, y).

**kwargs : coordinates={“cartesian”, “polar”}, optional

If cartesian then arg1 is x and arg2 is y components, else if polar then arg1 is rho and arg2 is phi (in radians).

Raises :

TypeError :

In case args is not the correct type(Vector, two scalars or point-like).

__add__(vector)[source]

Add two vectors.

Parameters :

vector : vector

The vector to be added to self.

Returns :

A new vector with components ``self.x + vector.x``, :

self.y + vector.y.

__contains__(x)

Searches for x in “itself”. If we’re talking about a Point or a Vector then this searches within their components (x, y). For everything else it searches within the list of points (vertices).

Parameters :

x : {point, scalar}

The object to search for.

Returns :

out : {True, False}

True if we find x in self, else False.

__getitem__(idx)[source]

Return values as a list for easier acces some times.

__len__()[source]

The length of a Vector is 2.

__mul__(arg)[source]

Calculates the dot product with another Vector, or multiplication by scalar.

For more details see dot.

__neg__()[source]

Turns self to 180 degrees and returns the new Vector.

Returns :

out : vector

Return a new Vector with same self.rho, but self.phi-math.pi.

__sub__(vector)[source]

Subtraction of two vectors.

It is __add__ passed with turnerd round vector.

_calculate_cartesian_coords()[source]

Helper function for internally calculating self.x and self.y.

Raises :

ValueError :

In case self.phi is outside of the interval [0, 2PI) an Exception is raised.

_calculate_polar_coords()[source]

Helper function for internally calculating self.rho and self.phi.

cross(arg)[source]

Calculates the cross product with another Vector, as defined in 2D space (not really a cross product since it gives a scalar, not another Vector).

Parameters :

arg : vector

Another Vector to calculate the cross product with.

Returns :

res : float

Take a look at the parameters section.

Raises :

TypeError :

In case arg is not a Vector.

dot(arg)[source]

Calculates the dot product with another Vector, or multiplication by scalar.

Parameters :

arg : {scalar, vector}

If it’s a number then calculates the product of that number with this Vector, if it’s another Vector then it will calculate the dot product.

Returns :

res : {float, vector}

Take a look at the parameters section.

Raises :

TypeError :

In case arg is not number or Vector.

intersection(obj)

Return points of intersection if any.

This method just calls the intersection method on the other objects that have it implemented.

Parameters :

obj : geometric object

obj is any object that has intersection implemented.

Returns :

ret : {point, None}

The point of intersection if any, if not, just None.

normalized[source]

[Vector] Get a normalized self.

parallel_to(obj)[source]

Is self parallel with obj?

Find out if this Vector is parallel with another object (Vector or Line-like). Since we are in a 2D plane, we can use the geometric interpretation of the cross product.

Parameters :

obj : {vector, line-like}

The object to be parallel with.

Returns :

res : {True, False}

If it’s parallel return True, else False.

perpendicular_to(obj)[source]

Is self perpendicular to obj?

Find out if this Vector is perpendicular to another object (Vector or Line-like). If the dot product between the two vectors is 0 then they are perpendicular.

Parameters :

obj : {vector, line-like}

The object to be parallel with.

Returns :

res : {True, False}

If they are perpendicular return True, else False.

phi[source]

[scalar] Get the angle (radians).

Get the angle (in radians) of the Vector with the X axis (polar coordinates). phi will always be mapped to [0, 2PI).

static random_direction()[source]

Create a randomly oriented Vector (with phi in the interval [0, PI)) and with unit length.

Returns :

out : vector

A Vector with random orientation in positive Y direction and with unit length.

rho[source]

[scalar] Get the length of the Vector (polar coordinates).

rotate(theta, angle='degrees')[source]

Rotate self by theta degrees.

translate(*args)[source]

Dummy function since it doesn’t make sense to translate a Vector.

x[source]

[scalar] Get the x component of the Vector.

y[source]

[scalar] Get the y component of the Vector.

[Arnon1983]Arnon et al., A Linear Time Algorithm for the Minimum Area Rectangle Enclosing a Convex Polygon” (1983), Computer Science Technical Reports. Paper 382
[WPolygon]http://en.wikipedia.org/wiki/Polygon

Previous topic

geo2d.geometry.Point

Next topic

geo2d.geometry.BoundingBox

This Page