Skip to content

Vector2D

Canonical path: NemAll_Python_Geometry.Vector2D

Representation class for 2D Vector.

To3D property

To3D: Vector3D

convert to 3D3

X property writable

X: float

Get the X coordinate reference.

Y property writable

Y: float

Get the Y coordinate reference.

CrossProduct

CrossProduct(vec: Vector2D) -> Vector3D

Cross(vector) product operator, according to the formula:

\[ \vec{c} = \vec{a} \times \vec{b} = \begin{pmatrix} 0 \\ 0 \\ a_x b_y - a_y b_x \end{pmatrix} \]

Where \(\vec{a}\) is this vector.

Parameters:

Returns:

Examples:

>>> vec = Vector2D(2, 0)
>>> vec.CrossProduct(Vector2D(0, 3))
Vector3D(0, 0, 6)

DotProduct

DotProduct(vec: Vector2D) -> float

Dot product operator.

\[ s = \vec{a} \cdot \vec{b} = a_x b_x + a_y b_y \]

Where \(\vec{a}\) is this vector.

Parameters:

Returns:

  • float

    double.

Examples:

Calculate dot product of a unit vector along X axis and a vector perpendiular to it

>>> vec = Vector2D(1, 0)
>>> vec.DotProduct(Vector2D(0, 1))
0.0

Calculate dot product of the same vector and a vector parallel to it

>>> vec.DotProduct(Vector2D(2, 0))
2.0

GetAngle

GetAngle() -> Angle

Get the angle to X axis

Returns:

  • Angle

    Angle in a range <0, 2*PI> (normalized 2pi).

Examples:

>>> Vector2D(0, 1).GetAngle().Deg
90.0
>>> Vector2D(0, -1).GetAngle().Deg
270.0

GetAngleSigned

GetAngleSigned() -> Angle

Get the signed angle to X axis

Returns:

  • Angle

    Angle in a range <-PI..0..PI>

Examples:

>>> Vector2D(0, 1).GetAngleSigned().Deg
90.0
>>> Vector2D(0, -1).GetAngleSigned().Deg
-90.0

GetCoords

GetCoords() -> tuple[float, float]

Get copy of X, Y coordinates.

Returns:

  • float

    X coodinate

  • float

    Y coodinate

Examples:

>>> Vector2D(1.0, 2.0).GetCoords()
(1.0, 2.0)

GetLength

GetLength() -> float

Get the length of the vector, according to the formula:

\[ l = \|\vec{a}\| = \sqrt{a_x^2 + a_y^2} \]

Where \(\vec{a}\) is this vector.

Returns:

  • float

    Length of the vector

Examples:

>>> Vector2D(1, 1).GetLength()
1.4142135623730951

IsZero

IsZero() -> bool

Check the coords [0.0, 0.0] (binary comparison)

Returns:

  • bool

    Is zero? true/false

Normalize overloaded

Normalize()

Normalize the vector to a unit vector, according to the formula:

\[ \vec{a} = \frac{\vec{a}}{\|\vec{a}\|} \]

Where \(\vec{a}\) is this vector.

This is a mutator method - it changes the vector, on which it is called

Examples:

>>> vec = Vector2D(1, 1)
>>> vec.Normalize()
>>> vec
Vector2D(0.707..., 0.707...)
Normalize(length: float)

Normalize the vector to a defined length, according to the formula:

\[ \vec{a} = \frac{\vec{a}}{\|\vec{a}\|} \cdot l \]

Where \(\vec{a}\) is this vector and \(l\) is the desired length.

This is a mutator method - it changes the vector, on which it is called.

Parameters:

  • length (float) –

    new length of vector.

Examples:

>>> vec = Vector2D(1, 1)
>>> vec.Normalize(2.0)
>>> vec
Vector2D(1.414..., 1.414...)

Orthogonal

Orthogonal(counterClockwise: bool = True) -> Vector2D

Get orthogonal vector

Parameters:

  • counterClockwise (bool, default: True ) –

    Orientation of orthogonal vector

Returns:

Examples:

>>> Vector2D(1, 1).Orthogonal()
Vector2D(-1, 1)
>>> Vector2D(1, 1).Orthogonal(False)
Vector2D(1, -1)

Reverse

Reverse() -> Vector2D

Compute reversed vector

Method calculate vector with reversed orientation.

Returns:

Set overloaded

Set(vec: Vector2D)

Initialize from vector 2D.

Parameters:

Set(x: float, y: float)

Initialize from x,y coordinates.

Parameters:

  • x (float) –

    coordinate.

  • y (float) –

    coordinate.

Set(startPoint: Point2D, endPoint: Point2D)

Initialize vector from two points.

Parameters:

  • startPoint (Point2D) –

    start point of vector.

  • endPoint (Point2D) –

    end point of vector.

Values

Values() -> list[float]

Get copy of X,Y coordinates as python list

Returns:

  • list[float]

    Vector coordinates in a list

__add__

__add__(vec: Vector2D) -> Vector2D

Addition operator.

\[ \vec{c} = \vec{a} + \vec{b} \]

Where \(\vec{a}\) is this vector.

Parameters:

Returns:

Examples:

>>> Vector2D(1, 2) + Vector2D(3, 4)
Vector2D(4, 6)

__eq__

__eq__(vec: Vector2D) -> bool

Comparison of vectors without tolerance.

Be careful, this method work without tolerance!

Parameters:

Returns:

  • bool

    True when points are equal, otherwise false.

__iadd__

__iadd__(vec: Vector2D) -> Vector2D

In-place addition operator.

\[ \vec{a} = \vec{a} + \vec{b} \]

Where \(\vec{a}\) is this vector.

Parameters:

Returns:

Examples:

>>> vec = Vector2D(1, 2)
>>> vec += Vector2D(3, 4)
>>> vec
Vector2D(4, 6)

__imul__ overloaded

__imul__(matrix: Matrix2D) -> Vector2D

In-place matrix transformation.

\[ \vec{a} = \vec{a} \cdot M \]

Where \(\vec{a}\) is this vector and \(M\) is the transformation matrix.

Parameters:

  • matrix (Matrix2D) –

    Transformation matrix.

Returns:

Examples:

rotation_3 is a transformation matrix that rotates by 90° around the origin. Apply it to the vector like:

>>> vec = Vector2D(1, 2)
>>> vec *= rotation_3
>>> vec
Vector2D(-2, 1)
__imul__(factor: float) -> Vector2D

In-place multiplication operator.

\[ \vec{a} = \vec{a} \cdot s \]

Where \(\vec{a}\) is this vector and \(s\) is the scaling multiplier.

Parameters:

  • factor (float) –

    Scale factor

Returns:

Examples:

>>> vec = Vector2D(1, 2)
>>> vec *= 2
>>> vec
Vector2D(2, 4)

__init__ overloaded

__init__()

Initialize zero vector.

__init__(vec: Vector2D)

Copy constructor.

Parameters:

  • vec (Vector2D) –

    Vector which will be copied.

__init__(angle: Angle, length: float)

Initialize from angle and length, according to the formula:

\[ \vec{a} = (l\cos(\alpha), l\sin(\alpha)) \]

Where \(\vec{a}\) is this vector, \(l\) is the length and \(\alpha\) is the angle.

Parameters:

  • angle (Angle) –

    base angle.

  • length (float) –

    length of vector (must be greater then zero).

__init__(x: float, y: float)

Initialize from X and Y coordinates.

Parameters:

  • x (float) –

    X coordinate of vector.

  • y (float) –

    Y coordinate of vector.

__init__(startPoint: Point2D, endPoint: Point2D)

Initialize from two points.

Parameters:

  • startPoint (Point2D) –

    start point of vector.

  • endPoint (Point2D) –

    end point of vector.

__init__(endPoint: Point2D)

Initialize from only the end point.

Parameters:

  • endPoint (Point2D) –

    End point of the vector (startpoint is 0./0.)

__init__(vec: Vector3D)

Initialize from a 3D vector.

Parameters:

  • vec (Vector3D) –

    2D vector which will be copied to the 3D vector.

__isub__

__isub__(vec: Vector2D) -> Vector2D

In-place subtraction operator.

\[ \vec{a} = \vec{a} - \vec{b} \]

Where \(\vec{a}\) is this vector.

Parameters:

Returns:

Examples:

>>> vec = Vector2D(1, 2)
>>> vec -= Vector2D(3, 4)
>>> vec
Vector2D(-2, -2)

__itruediv__

__itruediv__(divider: float) -> Vector2D

In-place division operator.

\[ \vec{a} = \frac{\vec{a}}{s} \]

Where \(\vec{a}\) is this vector and \(s\) is the scaling divider.

Parameters:

  • divider (float) –

    scaling divider

Returns:

Examples:

>>> vec = Vector2D(2, 4)
>>> vec /= 2
>>> vec
Vector2D(1, 2)

__mul__ overloaded

__mul__(vec: Vector2D) -> Vector3D

Cross(vector) product operator, according to the formula:

\[ \vec{c} = \vec{a} \times \vec{b} = \begin{pmatrix} 0 \\ 0 \\ a_x b_y - a_y b_x \end{pmatrix} \]

Where \(\vec{a}\) is this vector.

Parameters:

Returns:

Examples:

>>> Vector2D(2, 0) * Vector2D(0, 3)
Vector3D(0, 0, 6)
__mul__(matrix: Matrix2D) -> Vector2D

Matrix transformation.

\[ \vec{c} = \vec{a} \cdot M \]

Where \(\vec{a}\) is this vector and \(M\) is the transformation matrix.

Parameters:

  • matrix (Matrix2D) –

    Transformation matrix.

Returns:

Examples:

rotation_3 is a transformation matrix that rotates by 90° around the origin. Apply it to the vector like:

>>> Vector2D(1, 2) * rotation_3
Vector2D(-2, 1)
__mul__(factor: float) -> Vector2D

Multiplication operator.

\[ \vec{c} = \vec{a} \cdot s \]

Where \(\vec{a}\) is this vector and \(s\) is the scaling multiplier.

Parameters:

  • factor (float) –

    Scale factor

Returns:

Examples:

>>> Vector2D(1, 2) * 2
Vector2D(2, 4)

__ne__

__ne__(vec: Vector2D) -> bool

Comparison of vectors without tolerance.

Be careful, this method work without tolerance!

Parameters:

Returns:

  • bool

    True when points are not equal, otherwise false.

__repr__

__repr__() -> str

Convert to string

__sub__

__sub__(vec: Vector2D) -> Vector2D

Subtraction operator.

\[ \vec{c} = \vec{a} - \vec{b} \]

Where \(\vec{a}\) is this vector.

Parameters:

Returns:

Examples:

>>> Vector2D(1, 2) - Vector2D(3, 4)
Vector2D(-2, -2)

__truediv__

__truediv__(divider: float) -> Vector2D

Division operator.

\[ \vec{c} = \frac{\vec{a}}{s} \]

Where \(\vec{a}\) is this vector and \(s\) is the scaling divider.

Parameters:

  • divider (float) –

    scaling divider

Returns:

Examples:

>>> Vector2D(2, 4) / 2
Vector2D(1, 2)
Placeholder