Vector (hwx.common.math)#

class Vector(x=0, y=0, z=0)#

Bases: list

A mathematical represenation of a Vector in 3D space.

Attribute Table#

Name

Type

x

property

y

property

z

property

Method Table#

Name

Description

angle (self, x, y=None, z=None, degrees=False)

Computes the angle with the Vector defined by x, y, z.

close (self, x, y=None, z=None, tol=1e-07)

Determines if the distance to the Vector defined by x, y, z is less or

copy (self, x=None, y=None, z=None)

Creates a copy of self.

cross (self, x, y=None, z=None)

Computes the cross product with the Vector defined by x, y, z.

dot (self, x, y=None, z=None)

Computes the dot product with the Vector defined by x, y, z.

findAngle (pt1, pt2, pt3, degrees=False)

Computes the angle subtended between (pt2-pt1) and (pt3-pt1).

isAlignedWith (self, x, y=None, z=None, tolerance=1e-05, normalize=True)

Determines if self is parallel to the Vector defined by x, y, z.

iszero (self)

Returns True if x, y and z are set to zero, False otherwise.

magnitude (self)

Returns the magnitude.

normalize (self)

Returns the normalized Vector.

perpendicularize (self)

Computes a Vector perpendicular to self.

scale (self, x, y=None, z=None)

Scales by a single value ‘x’ or a tripple (x, y, z), elementwise.

Example

Define and use vectors.#
from hwx.common.math import Vector

# Vectors have a direction and magnitude
v = Vector([1, 1, 1])
print(v)

v += Vector(x=0, y=0, z=5)
print("V(x = {}, y = {}, z = {})".format(v.x, v.y, v.z))

v *= 5
print("V(x = {}, y = {}, z = {})".format(v.x, v.y, v.z))

v /= 2
print("V(x = {}, y = {}, z = {})".format(v.x, v.y, v.z))

v = Vector()  # by default creates a zero vector
print("V(x = {}, y = {}, z = {})".format(v.x, v.y, v.z))
print(v.iszero())
copy(x=None, y=None, z=None)#

Creates a copy of self.

If x, y, z are given then the copy has these as coordinates.

Parameters:
  • x (float) – The x coordinate.

  • y (float) – The y coordinate.

  • z (float) – The z coordinate.

Returns:

The newly created object.

Return type:

Vector

property x#

The x coordinate.

property y#

The y coordinate.

property z#

The z coordinate.

iszero()#

Returns True if x, y and z are set to zero, False otherwise.

dot(x, y=None, z=None)#

Computes the dot product with the Vector defined by x, y, z.

Parameters:
  • x (float) – The x coordinate.

  • y (float) – The y coordinate.

  • z (float) – The z coordinate.

Returns:

The dot product.

Return type:

float

cross(x, y=None, z=None)#

Computes the cross product with the Vector defined by x, y, z.

Parameters:
  • x (float) – The x coordinate.

  • y (float) – The y coordinate.

  • z (float) – The z coordinate.

Returns:

The cross product.

Return type:

Vector

scale(x, y=None, z=None)#

Scales by a single value ‘x’ or a tripple (x, y, z), elementwise.

Parameters:
  • x (float) – The x scale factor.

  • y (float) – The y scale factor.

  • z (float) – The z scale factor.

Returns:

The scaled Vector.

Return type:

Vector

magnitude()#

Returns the magnitude.

normalize()#

Returns the normalized Vector.

angle(x, y=None, z=None, degrees=False)#

Computes the angle with the Vector defined by x, y, z.

Parameters:
  • x (float) – The x coordinate.

  • y (float) – The y coordinate.

  • z (float) – The z coordinate.

  • degrees (bool) – Determines if the return value will be in degrees or not.

Returns:

The angle.

Return type:

float

perpendicularize()#

Computes a Vector perpendicular to self.

If any of the coordinates are zero, returns a Vector along that coordinate, otherwise returns the cross product with (1, 0, 0).

isAlignedWith(x, y=None, z=None, tolerance=1e-05, normalize=True)#

Determines if self is parallel to the Vector defined by x, y, z.

Parameters:
  • x (float) – The x coordinate.

  • y (float) – The y coordinate.

  • z (float) – The z coordinate.

  • tolerance (float) – The tolerance to consider when checking condition.

  • normalize (bool) – Determines whether to normalize self before checking the condition.

Returns:

True if Vectors are aligned, False otherwise.

Return type:

bool

close(x, y=None, z=None, tol=1e-07)#

Determines if the distance to the Vector defined by x, y, z is less or equal than tolerance.

Parameters:
  • x (float) – The x coordinate.

  • y (float) – The y coordinate.

  • z (float) – The z coordinate.

  • tolerance (float) – The tolerance to consider when checking condition.

Returns:

True if Vectors are close, False otherwise.

Return type:

bool