Skip to content

Vector Class

Alex Marinescu edited this page Nov 1, 2015 · 8 revisions

Vector Class features breakdown:


Operators:

  • Adding (Addition of two vectors)
  • Subtracting (Subtraction of two vectors)
  • Multiplication (scalar only)
  • Division (scalar only)
  • Negation (Negates every value inside the vector)
  • Equal boolean test (==)

Special cases:

  • cross (Cross product for 3D vectors only)
  • reflect (reflect a 3D vector)
  • refract (refract a 3D vector)

Functions:

  • Clone (Returns exact copy of a vector)
  • One (Returns a vector with all its components 1)
  • Zero (Returns a vector with all its components 0)
  • negate (Negates a vector)
  • maxV (Returns the max vector between two vectors)
  • maxS (Returns the max component between two vectors)
  • minV (Returns the min vector between two vectors)
  • minS (Returns the min component between two vectors)
  • magnitude (Returns magnitude of a vector)
  • clamp (Clamps a vector)
  • normalize (Normalized the vector)
  • dot (dot product of two vectors)
  • isInSameDirection
  • isInOppositeDirection
  • barycentric (Get a coordinates with respect to a triangle)
  • transform (matrix multiplication for transformation)

Swizzling:

This is similar to GLSL and will work with 3D or 4D vectors. Returns a 2D or 3D vector depends on the case.

  • xy
  • yz
  • xz
  • xw
  • yw
  • zw
  • xyw
  • yzw
  • xzw
  • xyw

Identities:

These are only for 3D vectors.

  • right
  • left
  • front
  • back
  • up
  • down

Examples:

It is a 1D, 2D, ... , ND Vector class. However, some functions only apply for certain dimensions only so for example a 8D vector will be limited in functionality.

Declaration of a vector:

from gem import vector

vectorA = vector.Vector(3, data=[1.0, 2.0, 3.0])
vectorB = vector.Vector(3)
vectorC = vector.Vector(3).one()
vectorD = vector.Vector(3).zero()

vectorData = [5.0, 6.0, 9.0]
vectorE = vector.Vector(3, vectorData)


# Output:
# VectorA: 1.0, 2.0, 3.0
# VectorB: 0.0, 0.0, 0.0
# VectorC: 1.0, 1.0, 1.0
# VectorD: 0.0, 0.0, 0.0
# VectorE: 5.0, 6.0, 9.0

Clone this wiki locally