Difference between revisions of "Vector Class"

From Derivative
Jump to navigation Jump to search
(Vector Class Auto Create)
(Vector Class Auto Create)
Line 5: Line 5:
 
values = [0, 1, 0]
 
values = [0, 1, 0]
 
v3 = tdu.Vector(values)}}
 
v3 = tdu.Vector(values)}}
<section begin=HELPBODY />
+
{{HelpbodyBegin}}
{{ClassMemberSection|empty=}}
+
{{ClassMemberSection|empty=
 +
|Sectionsummary=
 +
|items=
 +
 
{{ClassMember
 
{{ClassMember
 
     |class=tdu.Vector
 
     |class=tdu.Vector
Line 27: Line 30:
 
     |set=0
 
     |set=0
 
     |text=Gets or sets the Z component of the vector.
 
     |text=Gets or sets the Z component of the vector.
}}
+
}}}}
{{ClassMethodSection|empty=}}
+
{{ClassMethodSection|empty=
 +
|Sectionsummary=
 +
|items=
 +
 
{{ClassMethod
 
{{ClassMethod
 
     |class=tdu.Vector
 
     |class=tdu.Vector
     |name=lerp
+
     |name=slerp
 
     |call=lerp(vec, vec, t)
 
     |call=lerp(vec, vec, t)
 
     |returns=
 
     |returns=
Line 40: Line 46:
 
{{ClassMethod
 
{{ClassMethod
 
     |class=tdu.Vector
 
     |class=tdu.Vector
     |name=scale
+
     |name=reflect
     |call=scale(x, y, z)
+
     |call=reflect(vec)
 
     |returns=
 
     |returns=
 
     |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)
v.scale(1, 2, 1)
 
 
}}
 
}}
 
{{ClassMethod
 
{{ClassMethod
 
     |class=tdu.Vector
 
     |class=tdu.Vector
     |name=distance
+
     |name=copy
     |call=distance(vec)
+
     |call=copy()
 
     |returns=
 
     |returns=
     |text=: -> float
+
     |text=: -> Vector
Returns the distance of the current vector (vec1) to another vector (vec2).
+
Returns a new vector that is a copy of the vector.
l = v.distance(v2)
+
newV = v.copy()
 
}}
 
}}
 
{{ClassMethod
 
{{ClassMethod
 
     |class=tdu.Vector
 
     |class=tdu.Vector
     |name=normalize
+
     |name=lerp
     |call=normalize()
+
     |call=lerp(vec, vec, t)
 
     |returns=
 
     |returns=
     |text=:
+
     |text=: -> vec
Makes the length of this vector 1.
+
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.
m.normalize()
+
l = v.lerp(v, v2, t)
}}
 
{{ClassMethod
 
    |class=tdu.Vector
 
    |name=reflect
 
    |call=reflect(vec)
 
    |returns=
 
    |text=:
 
Reflects the current vector (vec) according to another vector (vec2).
 
v.reflect(v2)
 
 
}}
 
}}
 
{{ClassMethod
 
{{ClassMethod
Line 86: Line 82:
 
{{ClassMethod
 
{{ClassMethod
 
     |class=tdu.Vector
 
     |class=tdu.Vector
     |name=copy
+
     |name=lengthSquared
     |call=copy()
+
     |call=lengthSquared()
 
     |returns=
 
     |returns=
     |text=: -> Vector
+
     |text=: -> float
Returns a new vector that is a copy of the vector.
+
Returns the squared length of this vector.
newV = v.copy()
+
l = v.lengthSquared()
 
}}
 
}}
 
{{ClassMethod
 
{{ClassMethod
 
     |class=tdu.Vector
 
     |class=tdu.Vector
     |name=dot
+
     |name=cross
     |call=dot(vec)
+
     |call=cross(vec)
 
     |returns=
 
     |returns=
     |text=: -> float
+
     |text=: -> Vector
Returns the dot product of this vector and the passed 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 dot product
+
  vec - The other vector to use to calculate the cross product
d = v.dot(otherV)
+
c = v.cross(otherV)
 
}}
 
}}
 
{{ClassMethod
 
{{ClassMethod
Line 114: Line 110:
 
{{ClassMethod
 
{{ClassMethod
 
     |class=tdu.Vector
 
     |class=tdu.Vector
     |name=cross
+
     |name=scale
     |call=cross(vec)
+
     |call=scale(x, y, z)
 +
    |returns=
 +
    |text=:
 +
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)
 +
}}
 +
{{ClassMethod
 +
    |class=tdu.Vector
 +
    |name=dot
 +
    |call=dot(vec)
 
     |returns=
 
     |returns=
     |text=: -> Vector
+
     |text=: -> float
Returns the cross product of this vector and the passed vector. The operation is self cross vec
+
Returns the dot product of this vector and the passed vector.
  vec - The other vector to use to calculate the cross product
+
  vec - The other vector to use to calculate the dot product
c = v.cross(otherV)
+
d = v.dot(otherV)
 
}}
 
}}
 
{{ClassMethod
 
{{ClassMethod
 
     |class=tdu.Vector
 
     |class=tdu.Vector
     |name=lengthSquared
+
     |name=distance
     |call=lengthSuared()
+
     |call=distance(vec)
 
     |returns=
 
     |returns=
 
     |text=: -> float
 
     |text=: -> float
Returns the squared length of this vector.
+
Returns the distance of the current vector (vec1) to another vector (vec2).
l = v.lengthSquared()
+
l = v.distance(v2)
 
}}
 
}}
 
{{ClassMethod
 
{{ClassMethod
 
     |class=tdu.Vector
 
     |class=tdu.Vector
     |name=slerp
+
     |name=normalize
     |call=lerp(vec, vec, t)
+
     |call=normalize()
 
     |returns=
 
     |returns=
     |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.
+
Makes the length of this vector 1.
l = v.lerp(v, v2, t)
+
m.normalize()
 
}}
 
}}
 
{{ClassMethod
 
{{ClassMethod
 
     |class=tdu.Vector
 
     |class=tdu.Vector
 
     |name=project
 
     |name=project
     |call=project(vec, vec)
+
     |call=project(vec1, vec2)
 
     |returns=
 
     |returns=
 
     |text=:
 
     |text=:
Projects this vector onto the plan defined by vec1 and vec2. Both vec1 and vec2 must 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.
 
v.project(v1, v2)
 
v.project(v1, v2)
}}
+
}}}}
<section end=HELPBODY />
+
{{{{HelpbodyEnd}}}}
<taggedhistory/>
+
{{{{History}}}}
 
 
 
{{{{#invoke:Category|list|Python Reference}}}}
 
{{{{#invoke:Category|list|Python Reference}}}}

Revision as of 14:51, 14 September 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

x(Read Only):

Gets or sets the X component of the vector.

y(Read Only):

Gets or sets the Y component of the vector.

z(Read Only):

Gets or sets the Z component of the vector.

Methods

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):

:

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

v.reflect(v2)

copy():

: -> Vector

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

newV = v.copy()

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)

angle(vec):

: -> float

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

l = v.angle(v2)

lengthSquared():

: -> float

Returns the squared length of this vector.

l = v.lengthSquared()

cross(vec):

: -> 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)

length():

: -> float

Returns the length of this vector.

l = m.length()

scale(x, y, z):

:

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)

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)

normalize():

:

Makes the length of this vector 1.

m.normalize()

project(vec1, vec2):

:

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.

v.project(v1, v2)

{{{{HelpbodyEnd}}}} {{{{History}}}} {list}