Difference between revisions of "Vector Class"

From Derivative
Jump to navigation Jump to search
Line 178: Line 178:
 
     |class=tdu.Vector
 
     |class=tdu.Vector
 
     |name=[]
 
     |name=[]
     |call=[i]
+
     |call=tdu.Vector[i]
 
     |returns=float
 
     |returns=float
 
     |text=Gets or sets the component of the vector specified by i, where i can be 0, 1, or 2.
 
     |text=Gets or sets the component of the vector specified by i, where i can be 0, 1, or 2.
Line 189: Line 189:
 
     |class=tdu.Vector
 
     |class=tdu.Vector
 
     |name=V*f
 
     |name=V*f
     |call= * float
+
     |call=tdu.Vector * float
 
     |returns=vec
 
     |returns=vec
 
     |text=Scales the vector by the give float scalar and returns a new vector as the result.
 
     |text=Scales the vector by the give float scalar and returns a new vector as the result.
Line 200: Line 200:
 
     |class=tdu.Vector
 
     |class=tdu.Vector
 
     |name=V+f
 
     |name=V+f
     |call= + float
+
     |call=tdu.Vector + float
 
     |returns=vec
 
     |returns=vec
 
     |text=Adds the given scalar to all 3 components of the vector and returns a new vector as the result.
 
     |text=Adds the given scalar to all 3 components of the vector and returns a new vector as the result.
Line 211: Line 211:
 
     |class=tdu.Vector
 
     |class=tdu.Vector
 
     |name=V-f
 
     |name=V-f
     |call= - float
+
     |call=tdu.Vector - float
 
     |returns=vec
 
     |returns=vec
 
     |text=Subtracts the given scalar from all 3 components of the vector and returns a new vector as the result.
 
     |text=Subtracts the given scalar from all 3 components of the vector and returns a new vector as the result.
Line 222: Line 222:
 
     |class=tdu.Vector
 
     |class=tdu.Vector
 
     |name=V+V
 
     |name=V+V
     |call= + tdu.Vector
+
     |call=tdu.Vector + tdu.Vector
 
     |returns=vec
 
     |returns=vec
 
     |text=Adds the two vectors to create a new vector.
 
     |text=Adds the two vectors to create a new vector.
Line 232: Line 232:
 
     |class=tdu.Vector
 
     |class=tdu.Vector
 
     |name=V-V
 
     |name=V-V
     |call= - tdu.Vector
+
     |call=tdu.Vector - tdu.Vector
 
     |returns=vec
 
     |returns=vec
 
     |text=Subtracts the two vectors to create a new vector.
 
     |text=Subtracts the two vectors to create a new vector.
Line 242: Line 242:
 
     |class=tdu.Vector
 
     |class=tdu.Vector
 
     |name=V+=V
 
     |name=V+=V
     |call= += tdu.Vector
+
     |call=tdu.Vector += tdu.Vector
 
     |returns=vec
 
     |returns=vec
 
     |text=Adds the 2nd vector to the 1st vector, the 1st vector will contain the result of the operation.
 
     |text=Adds the 2nd vector to the 1st vector, the 1st vector will contain the result of the operation.
Line 252: Line 252:
 
     |class=tdu.Vector
 
     |class=tdu.Vector
 
     |name=V+=f
 
     |name=V+=f
     |call= += float
+
     |call=tdu.Vector += float
 
     |returns=vec
 
     |returns=vec
 
     |text=Adds the given scalar to all 3 components of the vector, the vector will contain the result of the operation.
 
     |text=Adds the given scalar to all 3 components of the vector, the vector will contain the result of the operation.
 
<syntaxhighlight lang=python>
 
<syntaxhighlight lang=python>
 
v1 += 0.4
 
v1 += 0.4
 +
</syntaxhighlight>
 +
}}
 +
{{SpecialFunc
 +
    |class=tdu.Vector
 +
    |name=V-=V
 +
    |call=tdu.Vector -= tdu.Vector
 +
    |returns=vec
 +
    |text=Subtracts the 2nd vector from the 1st vector, the 1st vector will contain the result of the operation.
 +
<syntaxhighlight lang=python>
 +
v1 -= v2
 +
</syntaxhighlight>
 +
}}
 +
{{SpecialFunc
 +
    |class=tdu.Vector
 +
    |name=M*V
 +
    |call=tdu.Matrix * tdu.Vector
 +
    |returns=vec
 +
    |text=Multiplies the vector by the matrix and returns the a new vector as the result.
 +
<syntaxhighlight lang=python>
 +
v = M * v
 
</syntaxhighlight>
 
</syntaxhighlight>
 
}}
 
}}

Revision as of 12:10, 2 October 2017

The vector class holds a single 3 component vector. A vector describes a direction in space, and it's important to use a vector or Position as appropriate for the data that is being calculated. When being multiplied by a Matrix, this class will implicitly have a 4th component (W component) of 0. A new vector can be created without any arguments, with 3 arguments for the x,y,z values, or with a single argument which is a variable that has 3 entries such as a list of length 3, or a position or vector. Examples of creating a vector:

v = tdu.Vector() # starts as (0, 0, 0)
v2 = tdu.Vector(0, 0, -1)
values = [0, 1, 0]
v3 = tdu.Vector(values)


Members

xfloat :

Gets or sets the X component of the vector.

yfloat :

Gets or sets the Y component of the vector.

zfloat :

Gets or sets the Z component of the vector.

Methods

normalize()None:

Makes the length of this vector 1.

m.normalize()

angle(vec)float:

Returns the angel (in degrees) between the current vector (vec1) and another vector (vec2).

l = v.angle(v2)

lerp(vec, vec, t)vec:

Returns vec1 * (1.0 - t) + vec2 * t, i.e., the linear interpolation of vec1 and vec2 using the floating-point value t. The value for t is not restricted to the range [0, 1]. The vec1 referes to the current vector.

l = v.lerp(v, v2, t)

project(vec1, vec2)None:

Projects this vector onto the plan defined by vec1 and vec2. Both vec1 and vec2 must be normalized. The result may not be normalized.

  • vec1, vec2 - The vectors that specify the plane to project onto. Must be normalized.
v.project(v1, v2)

lengthSquared()float:

Returns the squared length of this vector.

l = v.lengthSquared()

scale(x, y, z)None:

Scales each component of the vector by the specified values.

  • x, y, z - The values to scale each component of the vector by.
v.scale(1, 2, 1)

lerp(vec, vec, t)vec:

Returns vec1 * (1.0 - t) + vec2 * t, i.e., the linear interpolation of vec1 and vec2 using the floating-point value t. The value for t is not restricted to the range [0, 1]. The vec1 referes to the current vector.

l = v.lerp(v, v2, t)

reflect(vec)None:

Reflects the current vector (vec) according to another vector (vec2).

v.reflect(v2)

cross(vec)vec:

Returns the cross product of this vector and the passed vector. The operation is self cross vec.

  • vec - The other vector to use to calculate the cross product.
c = v.cross(otherV)

length()float:

Returns the length of this vector.

l = m.length()

dot(vec)float:

Returns the dot product of this vector and the passed vector.

  • vec - The other vector to use to calculate the dot product
d = v.dot(otherV)

distance(vec)float:

Returns the distance of the current vector (vec1) to another vector (vec2).

l = v.distance(v2)

copy()vec:

Returns a new vector that is a copy of the vector.

newV = v.copy()

Special Functions

tdu.Vector[i]float:

Gets or sets the component of the vector specified by i, where i can be 0, 1, or 2.

y = v[1]
v[1] = y * 2.0

tdu.Vector * floatvec:

Scales the vector by the give float scalar and returns a new vector as the result.

v = v * 2.0
v = 2.0 * v

tdu.Vector + floatvec:

Adds the given scalar to all 3 components of the vector and returns a new vector as the result.

v = v + 5.0
v = 5.0 + v

tdu.Vector - floatvec:

Subtracts the given scalar from all 3 components of the vector and returns a new vector as the result.

v = v - 1.5
v = 1.5 - v

tdu.Vector + tdu.Vectorvec:

Adds the two vectors to create a new vector.

v3 = v1 + v2

tdu.Vector - tdu.Vectorvec:

Subtracts the two vectors to create a new vector.

v3 = v1 - v2

tdu.Vector += tdu.Vectorvec:

Adds the 2nd vector to the 1st vector, the 1st vector will contain the result of the operation.

v1 += v2

tdu.Vector += floatvec:

Adds the given scalar to all 3 components of the vector, the vector will contain the result of the operation.

v1 += 0.4

tdu.Vector -= tdu.Vectorvec:

Subtracts the 2nd vector from the 1st vector, the 1st vector will contain the result of the operation.

v1 -= v2

tdu.Matrix * tdu.Vectorvec:

Multiplies the vector by the matrix and returns the a new vector as the result.

v = M * v



TouchDesigner Build: