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

Operators usage: They have to be the same size vectors in order for the operators to work.

from gem import vector

vectorA = vector.Vector(3, data=[5.0, 3.0, 8.0])
vectorB = vector.Vector(3, data=[1.0, 4.0, 5.0])

vectorAaddB = vectorA + vectorB

vectorAsubB = vectorA - vectorB

vectorAMulScalar = vectorA * 2.0

vectorBDivScalar = vectorB / 3.0

vectorANeg = -vectorA

vectorAdotB = vectorA.dot(vectorB)

# Since we are using 3D vectors here we can use the cross product
vectorAcrossB = vector.cross(vectorA, vectorB)

# Output:
# VectorAaddB = 6.0, 7.0, 13.0
# VectorAsubB = 4.0, -1.0, 3.0
# VectorAMulScalar = 10.0, 6.0, 16.0
# VectorBDivScalar = 0.33333, 1.33333, 1.66666
# VectorANeg = -5.0, -3.0, -8.0

# vectorAdotB = 57 (This returns a scalar)
# vectorAcrossB = -17, -17, 17

Function usage: Inside the vector classes most functions have two declartion, regular function name and with a "i_" in front of it. For example, the normalize function is declared as "normalize" and "i_normalize", the former will return a new normalized vector, the latter will normalize the vector in its place. So any function with "i_" in front of the function name will do the changes in place without returning a new vector.

Clone this wiki locally