Difference between revisions of "Vector Class"

From Derivative
Jump to navigation Jump to search
(Vector Class Auto Create)
m
Tags: 2018.28070 2021.10000
 
(17 intermediate revisions by 5 users not shown)
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|label=Vector|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 Class|Position]] as appropriate for the data that is being calculated. When being multiplied by a [[Matrix Class|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)
{{HelpbodyBegin}}
+
</syntaxhighlight>}}
 +
<section begin=HELPBODY />
 
{{ClassMemberSection|empty=
 
{{ClassMemberSection|empty=
 
|Sectionsummary=
 
|Sectionsummary=
Line 13: Line 15:
 
     |class=tdu.Vector
 
     |class=tdu.Vector
 
     |name=x
 
     |name=x
     |returns=
+
     |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 22:
 
     |class=tdu.Vector
 
     |class=tdu.Vector
 
     |name=y
 
     |name=y
     |returns=
+
     |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 29:
 
     |class=tdu.Vector
 
     |class=tdu.Vector
 
     |name=z
 
     |name=z
     |returns=
+
     |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 34: Line 36:
 
|Sectionsummary=
 
|Sectionsummary=
 
|items=
 
|items=
 
 
{{ClassMethod
 
{{ClassMethod
 
     |class=tdu.Vector
 
     |class=tdu.Vector
     |name=slerp
+
     |name=angle
     |call=lerp(vec, vec, t)
+
     |call=angle(vec)
     |returns=
+
     |returns=float
     |text=: -> vec
+
     |text=Returns the angel (in degrees) between the current vector and specified vector (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.
+
<syntaxhighlight lang=python>
l = v.lerp(v, v2, t)
+
d = v.angle(v2)
 +
</syntaxhighlight>
 
}}
 
}}
 
{{ClassMethod
 
{{ClassMethod
 
     |class=tdu.Vector
 
     |class=tdu.Vector
     |name=reflect
+
     |name=scale
     |call=reflect(vec)
+
     |call=scale(x, y, z)
     |returns=
+
     |returns=None
     |text=:
+
     |text=Scales each component of the vector by the specified values.
Reflects the current vector (vec) according to another vector (vec2).
+
*x, y, z - The values to scale each component of the vector by.
v.reflect(v2)
+
<syntaxhighlight lang=python>
 +
v.scale(1, 2, 1)
 +
</syntaxhighlight>
 
}}
 
}}
 
{{ClassMethod
 
{{ClassMethod
 
     |class=tdu.Vector
 
     |class=tdu.Vector
     |name=copy
+
     |name=normalize
     |call=copy()
+
     |call=normalize()
     |returns=
+
     |returns=None
     |text=: -> Vector
+
     |text=Makes the length of this vector 1.
Returns a new vector that is a copy of the vector.
+
<syntaxhighlight lang=python>
newV = v.copy()
+
m.normalize()
 +
</syntaxhighlight>
 
}}
 
}}
 
{{ClassMethod
 
{{ClassMethod
 
     |class=tdu.Vector
 
     |class=tdu.Vector
     |name=lerp
+
     |name=length
     |call=lerp(vec, vec, t)
+
     |call=length()
     |returns=
+
     |returns=float
     |text=: -> vec
+
     |text=Returns the length of this 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 = m.length()
 +
</syntaxhighlight>
 
}}
 
}}
 
{{ClassMethod
 
{{ClassMethod
 
     |class=tdu.Vector
 
     |class=tdu.Vector
     |name=angle
+
     |name=lengthSquared
     |call=angle(vec)
+
     |call=lengthSquared()
     |returns=
+
     |returns=float
     |text=: -> float
+
     |text=Returns the squared length of this vector.
Returns the angel (in degrees) between the current vector (vec1) and another vector (vec2).
+
<syntaxhighlight lang=python>
l = v.angle(v2)
+
l = v.lengthSquared()
 +
</syntaxhighlight>
 
}}
 
}}
 
{{ClassMethod
 
{{ClassMethod
 
     |class=tdu.Vector
 
     |class=tdu.Vector
     |name=lengthSquared
+
     |name=copy
     |call=lengthSquared()
+
     |call=copy()
     |returns=
+
     |returns=tdu.Vector
     |text=: -> float
+
     |text=Returns a new vector that is a copy of the vector.
Returns the squared length of this vector.
+
<syntaxhighlight lang=python>
l = v.lengthSquared()
+
newV = v.copy()
 +
</syntaxhighlight>
 
}}
 
}}
 
{{ClassMethod
 
{{ClassMethod
 
     |class=tdu.Vector
 
     |class=tdu.Vector
     |name=cross
+
     |name=distance
     |call=cross(vec)
+
     |call=distance(vec)
     |returns=
+
     |returns=float
     |text=: -> Vector
+
     |text=Returns the distance of the current vector to specified vector (vec).
Returns the cross product of this vector and the passed vector. The operation is self cross vec
+
<syntaxhighlight lang=python>
vec - The other vector to use to calculate the cross product
+
l = v.distance(v2)
c = v.cross(otherV)
+
</syntaxhighlight>
 
}}
 
}}
 
{{ClassMethod
 
{{ClassMethod
 
     |class=tdu.Vector
 
     |class=tdu.Vector
     |name=length
+
     |name=lerp
     |call=length()
+
     |call=lerp(vec2, t)
     |returns=
+
     |returns=tdu.Vector
     |text=: -> float
+
     |text=Returns the linear interpolation of this vector and vec2. That is vec1 * (1.0 - t) + vec2 * t, where vec1 is the current vector.  The value for t is not restricted to the range [0, 1].
Returns the length of this vector.
+
<syntaxhighlight lang=python>
l = m.length()
+
l = v.lerp(v2, t)
 +
</syntaxhighlight>
 
}}
 
}}
 
{{ClassMethod
 
{{ClassMethod
 
     |class=tdu.Vector
 
     |class=tdu.Vector
     |name=scale
+
     |name=slerp
     |call=scale(x, y, z)
+
     |call=slerp(vec2, t)
     |returns=
+
     |returns=tdu.Vector
     |text=:
+
     |text=Returns the spherical interpolation of this vector and vec2. The value for t is not restricted to the range [0, 1].
Scales each component of the vector by the specified values.
+
<syntaxhighlight lang=python>
x, y, z - The values to scale each component of the vector by.
+
l = v.slerp(v2, t)
v.scale(1, 2, 1)
+
</syntaxhighlight>
 
}}
 
}}
 
{{ClassMethod
 
{{ClassMethod
Line 122: Line 131:
 
     |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
 
     |class=tdu.Vector
 
     |class=tdu.Vector
     |name=distance
+
     |name=cross
     |call=distance(vec)
+
     |call=cross(vec)
     |returns=
+
     |returns=tdu.Vector
     |text=: -> float
+
     |text=Returns the cross product of this vector and the passed vector. The operation is self cross vec.
Returns the distance of the current vector (vec1) to another vector (vec2).
+
*vec - The other vector to use to calculate the cross product.
l = v.distance(v2)
+
<syntaxhighlight lang=python>
 +
c = v.cross(otherV)
 +
</syntaxhighlight>
 
}}
 
}}
 
{{ClassMethod
 
{{ClassMethod
 
     |class=tdu.Vector
 
     |class=tdu.Vector
     |name=normalize
+
     |name=project
     |call=normalize()
+
     |call=project(vec, vec)
     |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.
Makes the length of this vector 1.
+
*vec1, vec2 - The  vectors that specify the plane to project onto. Must be normalized.
m.normalize()
+
<syntaxhighlight lang=python>
 +
v.project(v1, v2)
 +
</syntaxhighlight>
 
}}
 
}}
 
{{ClassMethod
 
{{ClassMethod
 
     |class=tdu.Vector
 
     |class=tdu.Vector
     |name=project
+
     |name=reflect
     |call=project(vec1, vec2)
+
     |call=reflect(vec)
     |returns=
+
     |returns=None
     |text=:
+
     |text=Reflects the current vector about the specified vector (vec).
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.
+
<syntaxhighlight lang=python>
vec1, vec2 - The  vectors that specify the plane to project onto. Must be normalized.
+
v.reflect(v2)
v.project(v1, v2)
+
</syntaxhighlight>
 
}}}}
 
}}}}
{{{{HelpbodyEnd}}}}
+
{{SubSection|title=Special Functions
{{{{History}}}}
+
|text=
{{{{#invoke:Category|list|Python Reference}}}}
+
}}
 +
{{SpecialFunc
 +
    |class=tdu.Vector
 +
    |name=[]
 +
    |call=tdu.Vector[i]
 +
    |returns=float
 +
    |text=Gets or sets the component of the vector specified by i, where i can be 0, 1, or 2.
 +
<syntaxhighlight lang=python>
 +
y = v[1]
 +
v[1] = y * 2.0
 +
</syntaxhighlight>
 +
}}
 +
{{SpecialFunc
 +
    |class=tdu.Vector
 +
    |name=V*f
 +
    |call=tdu.Vector * float
 +
    |returns=tdu.Vector
 +
    |text=Scales the vector by the give float scalar and returns a new vector as the result.
 +
<syntaxhighlight lang=python>
 +
v = v * 2.0
 +
v = 2.0 * v
 +
</syntaxhighlight>
 +
}}
 +
{{SpecialFunc
 +
    |class=tdu.Vector
 +
    |name=V+f
 +
    |call=tdu.Vector + float
 +
    |returns=tdu.Vector
 +
    |text=Adds the given scalar to all 3 components of the vector and returns a new vector as the result.
 +
<syntaxhighlight lang=python>
 +
v = v + 5.0
 +
v = 5.0 + v
 +
</syntaxhighlight>
 +
}}
 +
{{SpecialFunc
 +
    |class=tdu.Vector
 +
    |name=V-f
 +
    |call=tdu.Vector - float
 +
    |returns=tdu.Vector
 +
    |text=Subtracts the given scalar from all 3 components of the vector and returns a new vector as the result.
 +
<syntaxhighlight lang=python>
 +
v = v - 1.5
 +
v = 1.5 - v
 +
</syntaxhighlight>
 +
}}
 +
{{SpecialFunc
 +
    |class=tdu.Vector
 +
    |name=V+V
 +
    |call=tdu.Vector + tdu.Vector
 +
    |returns=tdu.Vector
 +
    |text=Adds the two vectors to create a new vector.
 +
<syntaxhighlight lang=python>
 +
v3 = v1 + v2
 +
</syntaxhighlight>
 +
}}
 +
{{SpecialFunc
 +
    |class=tdu.Vector
 +
    |name=V-V
 +
    |call=tdu.Vector - tdu.Vector
 +
    |returns=tdu.Vector
 +
    |text=Subtracts the two vectors to create a new vector.
 +
<syntaxhighlight lang=python>
 +
v3 = v1 - v2
 +
</syntaxhighlight>
 +
}}
 +
{{SpecialFunc
 +
    |class=tdu.Vector
 +
    |name=V+=V
 +
    |call=tdu.Vector += tdu.Vector
 +
    |returns=tdu.Vector
 +
    |text=Adds the 2nd vector to the 1st vector, the 1st vector will contain the result of the operation.
 +
<syntaxhighlight lang=python>
 +
v1 += v2
 +
</syntaxhighlight>
 +
}}
 +
{{SpecialFunc
 +
    |class=tdu.Vector
 +
    |name=V+=f
 +
    |call=tdu.Vector += float
 +
    |returns=tdu.Vector
 +
    |text=Adds the given scalar to all 3 components of the vector, the vector will contain the result of the operation.
 +
<syntaxhighlight lang=python>
 +
v1 += 0.4
 +
</syntaxhighlight>
 +
}}
 +
{{SpecialFunc
 +
    |class=tdu.Vector
 +
    |name=V-=V
 +
    |call=tdu.Vector -= tdu.Vector
 +
    |returns=tdu.Vector
 +
    |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=tdu.Vector
 +
    |text=Multiplies the vector by the matrix and returns the a new vector as the result. Since a Vector is direction only and has no notion of a position, the translate part of the matrix does not get applied to the vector.
 +
<syntaxhighlight lang=python>
 +
v = M * v
 +
</syntaxhighlight>
 +
}}
 +
{{SpecialFunc
 +
    |class=tdu.Vector
 +
    |name=V/f
 +
    |call=tdu.Vector / float
 +
    |returns=tdu.Vector
 +
    |text=Divides each component of the vector by the scalar and returns the a new vector as the result.
 +
<syntaxhighlight lang=python>
 +
v = v / 0.2
 +
</syntaxhighlight>
 +
}}
 +
{{SpecialFunc
 +
    |class=tdu.Vector
 +
    |name=V*=M
 +
    |call=tdu.Vector *= tdu.Matrix
 +
    |returns=tdu.Vector
 +
    |text=Multiplies the vector by the matrix, the vector will contain the result. The vector is multiplied on the right of the matrix. This is the same as doing v = M * v, although more efficient since it doesn't require assigning a new vector to v. Since a Vector is direction only and has no notion of a position, the translate part of the matrix does not get applied to the vector.
 +
<syntaxhighlight lang=python>
 +
v *= M
 +
</syntaxhighlight>
 +
}}
 +
{{SpecialFunc
 +
    |class=tdu.Vector
 +
    |name=V*=f
 +
    |call=tdu.Vector *= float
 +
    |returns=tdu.Vector
 +
    |text=Scales all 3 components of the vector by the given scalar. The vector will contain the result.
 +
<syntaxhighlight lang=python>
 +
v *= 1.1
 +
</syntaxhighlight>
 +
}}
 +
{{SpecialFunc
 +
    |class=tdu.Vector
 +
    |name=V*=V
 +
    |call=tdu.Vector *= tdu.Vector
 +
    |returns=tdu.Vector
 +
    |text=Does a component-wise scale of all 3 components of the vector by the components of the 2nd vector. The vector will contain the result.
 +
<syntaxhighlight lang=python>
 +
v1 *= v2
 +
</syntaxhighlight>
 +
}}
 +
{{SpecialFunc
 +
    |class=tdu.Vector
 +
    |name=absV
 +
    |call=abs(tdu.Vector)
 +
    |returns=tdu.Vector
 +
    |text=Returns a new vector with all 3 components being the absolute value of the given vector's components.
 +
<syntaxhighlight lang=python>
 +
v2 = abs(v1)
 +
</syntaxhighlight>
 +
}}
 +
{{SpecialFunc
 +
    |class=tdu.Vector
 +
    |name=-V
 +
    |call=-tdu.Vector
 +
    |returns=tdu.Vector
 +
    |text=Returns a new vector with all 3 components being negated.
 +
<syntaxhighlight lang=python>
 +
v2 = -v1
 +
</syntaxhighlight>
 +
}}
 +
<section end=HELPBODY />
 +
{{History}}
 +
{{#invoke:Category|list|Python Reference}}

Latest revision as of 14:36, 11 January 2019

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

angle(vec)float:

Returns the angel (in degrees) between the current vector and specified vector (vec).

d = v.angle(v2)

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)

normalize()None:

Makes the length of this vector 1.

m.normalize()

length()float:

Returns the length of this vector.

l = m.length()

lengthSquared()float:

Returns the squared length of this vector.

l = v.lengthSquared()

copy()tdu.Vector:

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

newV = v.copy()

distance(vec)float:

Returns the distance of the current vector to specified vector (vec).

l = v.distance(v2)

lerp(vec2, t)tdu.Vector:

Returns the linear interpolation of this vector and vec2. That is vec1 * (1.0 - t) + vec2 * t, where vec1 is the current vector. The value for t is not restricted to the range [0, 1].

l = v.lerp(v2, t)

slerp(vec2, t)tdu.Vector:

Returns the spherical interpolation of this vector and vec2. The value for t is not restricted to the range [0, 1].

l = v.slerp(v2, t)

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)

cross(vec)tdu.Vector:

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)

project(vec, vec)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)

reflect(vec)None:

Reflects the current vector about the specified vector (vec).

v.reflect(v2)

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 * floattdu.Vector:

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 + floattdu.Vector:

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 - floattdu.Vector:

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.Vectortdu.Vector:

Adds the two vectors to create a new vector.

v3 = v1 + v2

tdu.Vector - tdu.Vectortdu.Vector:

Subtracts the two vectors to create a new vector.

v3 = v1 - v2

tdu.Vector += tdu.Vectortdu.Vector:

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

v1 += v2

tdu.Vector += floattdu.Vector:

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.Vectortdu.Vector:

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

v1 -= v2

tdu.Matrix * tdu.Vectortdu.Vector:

Multiplies the vector by the matrix and returns the a new vector as the result. Since a Vector is direction only and has no notion of a position, the translate part of the matrix does not get applied to the vector.

v = M * v

tdu.Vector / floattdu.Vector:

Divides each component of the vector by the scalar and returns the a new vector as the result.

v = v / 0.2

tdu.Vector *= tdu.Matrixtdu.Vector:

Multiplies the vector by the matrix, the vector will contain the result. The vector is multiplied on the right of the matrix. This is the same as doing v = M * v, although more efficient since it doesn't require assigning a new vector to v. Since a Vector is direction only and has no notion of a position, the translate part of the matrix does not get applied to the vector.

v *= M

tdu.Vector *= floattdu.Vector:

Scales all 3 components of the vector by the given scalar. The vector will contain the result.

v *= 1.1

tdu.Vector *= tdu.Vectortdu.Vector:

Does a component-wise scale of all 3 components of the vector by the components of the 2nd vector. The vector will contain the result.

v1 *= v2

abs(tdu.Vector)tdu.Vector:

Returns a new vector with all 3 components being the absolute value of the given vector's components.

v2 = abs(v1)

-tdu.Vectortdu.Vector:

Returns a new vector with all 3 components being negated.

v2 = -v1

TouchDesigner Build: