Difference between revisions of "Vector Class"

From Derivative
Jump to navigation Jump to search
(Vector Class Auto Create)
Line 1: Line 1:
 
{{TDClassSummary|summary=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.
 
{{TDClassSummary|summary=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:
 
Examples of creating a vector:
 +
<syntaxhighlight lang=python>
 
v = tdu.Vector() # starts as (0, 0, 0)
 
v = tdu.Vector() # starts as (0, 0, 0)
 
v2 = tdu.Vector(0, 0, -1)
 
v2 = tdu.Vector(0, 0, -1)
 
values = [0, 1, 0]
 
values = [0, 1, 0]
v3 = tdu.Vector(values)}}
+
v3 = tdu.Vector(values)
 +
</syntaxhighlight>
 +
}}
 
<section begin=HELPBODY />
 
<section begin=HELPBODY />
 
{{ClassMemberSection|empty=
 
{{ClassMemberSection|empty=
Line 13: Line 16:
 
     |class=tdu.Vector
 
     |class=tdu.Vector
 
     |name=x
 
     |name=x
     |type=
+
     |type=float
     |set=0
+
     |set=1
 
     |text=Gets or sets the X component of the vector.
 
     |text=Gets or sets the X component of the vector.
 
}}
 
}}
Line 20: Line 23:
 
     |class=tdu.Vector
 
     |class=tdu.Vector
 
     |name=y
 
     |name=y
     |type=
+
     |type=float
     |set=0
+
     |set=1
 
     |text=Gets or sets the Y component of the vector.
 
     |text=Gets or sets the Y component of the vector.
 
}}
 
}}
Line 27: Line 30:
 
     |class=tdu.Vector
 
     |class=tdu.Vector
 
     |name=z
 
     |name=z
     |type=
+
     |type=float
     |set=0
+
     |set=1
 
     |text=Gets or sets the Z component of the vector.
 
     |text=Gets or sets the Z component of the vector.
 
}}}}
 
}}}}
Line 39: Line 42:
 
     |name=normalize
 
     |name=normalize
 
     |call=normalize()
 
     |call=normalize()
     |returns=
+
     |returns=None
     |text=:
+
     |text=Makes the length of this vector 1.
Makes the length of this vector 1.
+
<syntaxhighlight lang=python>
 
m.normalize()
 
m.normalize()
 +
</syntaxhighlight>
 
}}
 
}}
 
{{ClassMethod
 
{{ClassMethod
Line 48: Line 52:
 
     |name=angle
 
     |name=angle
 
     |call=angle(vec)
 
     |call=angle(vec)
     |returns=
+
     |returns=float
     |text=: -> float
+
     |text=Returns the angel (in degrees) between the current vector (vec1) and another vector (vec2).
Returns the angel (in degrees) between the current vector (vec1) and another vector (vec2).
+
<syntaxhighlight lang=python>
 
l = v.angle(v2)
 
l = v.angle(v2)
 +
</syntaxhighlight>
 
}}
 
}}
 
{{ClassMethod
 
{{ClassMethod
Line 57: Line 62:
 
     |name=lerp
 
     |name=lerp
 
     |call=lerp(vec, vec, t)
 
     |call=lerp(vec, vec, t)
     |returns=
+
     |returns=vec
     |text=: -> vec
+
     |text=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.
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.
+
<syntaxhighlight lang=python>
 
l = v.lerp(v, v2, t)
 
l = v.lerp(v, v2, t)
 +
</syntaxhighlight>
 
}}
 
}}
 
{{ClassMethod
 
{{ClassMethod
Line 66: Line 72:
 
     |name=project
 
     |name=project
 
     |call=project(vec1, vec2)
 
     |call=project(vec1, vec2)
     |returns=
+
     |returns=None
     |text=:
+
     |text=Projects this vector onto the plan defined by vec1 and vec2. Both vec1 and vec2 must be normalized. The result may not be normalized.
Projects this vector onto the plan defined by vec1 and vec2. Both vec1 and vec2 must be normalized. The result will may not be normalized.
+
*vec1, vec2 - The  vectors that specify the plane to project onto. Must be normalized.
vec1, vec2 - The  vectors that specify the plane to project onto. Must be normalized.
+
<syntaxhighlight lang=python>
 
v.project(v1, v2)
 
v.project(v1, v2)
 +
</syntaxhighlight>
 
}}
 
}}
 
{{ClassMethod
 
{{ClassMethod
Line 76: Line 83:
 
     |name=lengthSquared
 
     |name=lengthSquared
 
     |call=lengthSquared()
 
     |call=lengthSquared()
     |returns=
+
     |returns=float
     |text=: -> float
+
     |text=Returns the squared length of this vector.
Returns the squared length of this vector.
+
<syntaxhighlight lang=python>
 
l = v.lengthSquared()
 
l = v.lengthSquared()
 +
</syntaxhighlight>
 
}}
 
}}
 
{{ClassMethod
 
{{ClassMethod
Line 85: Line 93:
 
     |name=scale
 
     |name=scale
 
     |call=scale(x, y, z)
 
     |call=scale(x, y, z)
     |returns=
+
     |returns=None
     |text=:
+
     |text=Scales each component of the vector by the specified values.
Scales each component of the vector by the specified values.
+
*x, y, z - The values to scale each component of the vector by.
x, y, z - The values to scale each component of the vector by.
+
<syntaxhighlight lang=python>
 
v.scale(1, 2, 1)
 
v.scale(1, 2, 1)
 +
</syntaxhighlight>
 
}}
 
}}
 
{{ClassMethod
 
{{ClassMethod
Line 95: Line 104:
 
     |name=slerp
 
     |name=slerp
 
     |call=lerp(vec, vec, t)
 
     |call=lerp(vec, vec, t)
     |returns=
+
     |returns=vec
     |text=: -> vec
+
     |text=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.
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.
+
<syntaxhighlight lang=python>
 
l = v.lerp(v, v2, t)
 
l = v.lerp(v, v2, t)
 +
</syntaxhighlight>
 
}}
 
}}
 
{{ClassMethod
 
{{ClassMethod
Line 104: Line 114:
 
     |name=reflect
 
     |name=reflect
 
     |call=reflect(vec)
 
     |call=reflect(vec)
     |returns=
+
     |returns=None
     |text=:
+
     |text=Reflects the current vector (vec) according to another vector (vec2).
Reflects the current vector (vec) according to another vector (vec2).
+
<syntaxhighlight lang=python>
 
v.reflect(v2)
 
v.reflect(v2)
 +
</syntaxhighlight>
 
}}
 
}}
 
{{ClassMethod
 
{{ClassMethod
Line 113: Line 124:
 
     |name=cross
 
     |name=cross
 
     |call=cross(vec)
 
     |call=cross(vec)
     |returns=
+
     |returns=vec
     |text=: -> Vector
+
     |text=Returns the cross product of this vector and the passed vector. The operation is self cross 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.
vec - The other vector to use to calculate the cross product
+
<syntaxhighlight lang=python>
 
c = v.cross(otherV)
 
c = v.cross(otherV)
 +
</syntaxhighlight>
 
}}
 
}}
 
{{ClassMethod
 
{{ClassMethod
Line 123: Line 135:
 
     |name=length
 
     |name=length
 
     |call=length()
 
     |call=length()
     |returns=
+
     |returns=float
     |text=: -> float
+
     |text=Returns the length of this vector.
Returns the length of this vector.
+
<syntaxhighlight lang=python>
 
l = m.length()
 
l = m.length()
 +
</syntaxhighlight>
 
}}
 
}}
 
{{ClassMethod
 
{{ClassMethod
Line 132: Line 145:
 
     |name=dot
 
     |name=dot
 
     |call=dot(vec)
 
     |call=dot(vec)
     |returns=
+
     |returns=float
     |text=: -> float
+
     |text=Returns the dot product of this vector and the passed vector.
Returns the dot product of this vector and the passed vector.
+
*vec - The other vector to use to calculate the dot product
vec - The other vector to use to calculate the dot product
+
<syntaxhighlight lang=python>
 
d = v.dot(otherV)
 
d = v.dot(otherV)
 +
</syntaxhighlight>
 
}}
 
}}
 
{{ClassMethod
 
{{ClassMethod
Line 142: Line 156:
 
     |name=distance
 
     |name=distance
 
     |call=distance(vec)
 
     |call=distance(vec)
     |returns=
+
     |returns=float
     |text=: -> float
+
     |text=Returns the distance of the current vector (vec1) to another vector (vec2).
Returns the distance of the current vector (vec1) to another vector (vec2).
+
<syntaxhighlight lang=python>
 
l = v.distance(v2)
 
l = v.distance(v2)
 +
</syntaxhighlight>
 
}}
 
}}
 
{{ClassMethod
 
{{ClassMethod
Line 151: Line 166:
 
     |name=copy
 
     |name=copy
 
     |call=copy()
 
     |call=copy()
     |returns=
+
     |returns=vec
     |text=: -> Vector
+
     |text=Returns a new vector that is a copy of the vector.
Returns a new vector that is a copy of the vector.
+
<syntaxhighlight lang=python>
 
newV = v.copy()
 
newV = v.copy()
 +
</syntaxhighlight>
 
}}}}
 
}}}}
 
<section end=HELPBODY />
 
<section end=HELPBODY />
 
{{History}}
 
{{History}}
 
{{#invoke:Category|list|Python Reference}}
 
{{#invoke:Category|list|Python Reference}}

Revision as of 11:27, 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()

TouchDesigner Build: