Difference between revisions of "IC Python API:RLPy RVector3"
Chuck (RL) (Talk | contribs) m |
Chuck (RL) (Talk | contribs) m |
||
Line 1: | Line 1: | ||
{{TOC}} | {{TOC}} | ||
{{Parent|IC_Python_API:RL_Python_Modules|Modules}} | {{Parent|IC_Python_API:RL_Python_Modules|Modules}} | ||
− | == | + | {{last_modified}} |
− | This class represent the 3D vector. | + | |
− | == | + | == Description == |
+ | |||
+ | This class represent the 3D vector (x, y, z). This class provides access to RLPy's internal vector3 math library allowing vector3s to be handled easily, in a manner compatible with internal RLPy data structures. It also supports operators and provides some constants for your convenience: | ||
+ | |||
+ | {|class = "wikitable" | ||
+ | !Constant | ||
+ | !Description | ||
+ | |- | ||
+ | |RVector3.ZERO | ||
+ | |3D zero vector: (0, 0, 0) | ||
+ | |- | ||
+ | |RVector3.UNIT_X | ||
+ | |3D x unit vector: (1, 0, 0) | ||
+ | |- | ||
+ | |RVector3.UNIT_Y | ||
+ | |3D y unit vector: (0, 1, 0) | ||
+ | |- | ||
+ | |RVector3.UNIT_Z | ||
+ | |3D z unit vector: (0, 0, 1) | ||
+ | |- | ||
+ | |RVector3.UNIT_XY | ||
+ | |3D vector: (1, 1, 0) | ||
+ | |- | ||
+ | |RVector3.UNIT_YZ | ||
+ | |3D vector: (0, 1, 1) | ||
+ | |- | ||
+ | |RVector3.UNIT_XZ | ||
+ | |3D vector: (1, 0, 1) | ||
+ | |- | ||
+ | |RVector3.UNIT_XYZ | ||
+ | |3D vector: (1, 1, 1) | ||
+ | |} | ||
+ | |||
+ | == Constructor & Destructor == | ||
+ | |||
+ | === __init__ ( self ) === | ||
− | |||
<syntaxhighlight lang="Python"> | <syntaxhighlight lang="Python"> | ||
− | RLPy.RVector3 | + | a = RLPy.RVector3() |
</syntaxhighlight> | </syntaxhighlight> | ||
+ | |||
+ | === __init__ ( self, x, y, z ) === | ||
+ | |||
+ | <syntaxhighlight lang="Python"> | ||
+ | a = RLPy.RVector3(1, 2, 3) | ||
+ | </syntaxhighlight> | ||
+ | |||
+ | ==== Parameters ==== | ||
+ | :'''x''' [IN] a numerical value for x coordinate - float or int | ||
+ | :'''y''' [IN] a numerical value for y coordinate - float or int | ||
+ | :'''z''' [IN] a numerical value for z coordinate - float or int | ||
+ | |||
+ | === __init__ ( self, args ) === | ||
+ | |||
+ | The constructor. Initialize a new RVector3 object with another RVector3 object: args. This new RVector3 object has the same value as args. | ||
+ | |||
+ | <syntaxhighlight lang="Python"> | ||
+ | a = RLPy.RVector3(1, 2, 3) | ||
+ | b = RLPy.RVector3(a) | ||
+ | </syntaxhighlight> | ||
+ | |||
+ | ==== Parameters ==== | ||
+ | :'''args''' [IN] a RVector3 object - RLPy.RVector3 | ||
+ | |||
+ | == Operators == | ||
+ | |||
+ | === == === | ||
+ | |||
+ | The "equal to" operator. | ||
+ | |||
+ | <syntaxhighlight lang="Python"> | ||
+ | a = RLPy.RVector3(1, 2, 3) | ||
+ | b = a | ||
+ | |||
+ | print(a == b) #True | ||
+ | </syntaxhighlight> | ||
+ | |||
+ | === != === | ||
+ | |||
+ | The "not equal to" operator. | ||
+ | |||
+ | <syntaxhighlight lang="Python"> | ||
+ | a = RLPy.RVector3() | ||
+ | b = RLPy.RVector3(1, 2, 3) | ||
+ | |||
+ | print(a != b) #True | ||
+ | </syntaxhighlight> | ||
+ | |||
+ | === < === | ||
+ | |||
+ | The "less than" operator. Similar to string comparison: Returns '''True''' upon the first match that is less than and '''False''' if it is greater than. If the current comparison is equal, continue onto the next element. If all elements are equal then return '''False'''. | ||
+ | |||
+ | <syntaxhighlight lang="Python"> | ||
+ | a = RLPy.RVector3(0, 1, 5) | ||
+ | b = RLPy.RVector3(0, 2, 0) | ||
+ | c = RLPy.RVector3(1, 0, 1) | ||
+ | d = RLPy.RVector3(0, 1, 5) | ||
+ | |||
+ | print(a < b) #True | ||
+ | print(b < c) #True | ||
+ | print(a < d) #False | ||
+ | </syntaxhighlight> | ||
+ | |||
+ | === > === | ||
+ | |||
+ | The "greater than" operator. Similar to string comparison: Returns '''True''' upon the first match that is greater than and '''False''' if it is less than. If the current comparison is equal, continue onto the next element. If all elements are equal then return '''False'''. | ||
+ | |||
+ | <syntaxhighlight lang="Python"> | ||
+ | a = RLPy.RVector3(0, 1, 5) | ||
+ | b = RLPy.RVector3(0, 1, 7) | ||
+ | c = RLPy.RVector3(1, 0, 1) | ||
+ | d = RLPy.RVector3(0, 1, 5) | ||
+ | |||
+ | print(b > a) #True | ||
+ | print(c > b) #True | ||
+ | print(d > a) #False | ||
+ | </syntaxhighlight> | ||
+ | |||
+ | === <= === | ||
+ | |||
+ | The "less than or equal" operator. Similar to string comparison: Returns '''True''' upon the first match that is less than and '''False''' if it is greater than. If the current comparison is equal, continue onto the next element. If all elements are equal then return '''True'''. | ||
+ | |||
+ | <syntaxhighlight lang="Python"> | ||
+ | a = RLPy.RVector3(0, 1, 5) | ||
+ | b = RLPy.RVector3(0, 2, 0) | ||
+ | c = RLPy.RVector3(1, 0, 1) | ||
+ | d = RLPy.RVector3(0, 1, 5) | ||
+ | |||
+ | print(a<= b) #True | ||
+ | print(b<= c) #True | ||
+ | print(a<= d) #True | ||
+ | </syntaxhighlight> | ||
+ | |||
+ | === >= === | ||
+ | |||
+ | The "greater than or equal" operator. Similar to string comparison: Returns '''True''' upon the first match that is greater than and '''False''' if it is less than. If the current comparison is equal, continue onto the next element. If all elements are equal then return '''True'''. | ||
+ | |||
+ | <syntaxhighlight lang="Python"> | ||
+ | a = RLPy.RVector3(0, 1, 5) | ||
+ | b = RLPy.RVector3(0, 1, 7) | ||
+ | c = RLPy.RVector3(1, 0, 1) | ||
+ | d = RLPy.RVector3(0, 1, 5) | ||
+ | |||
+ | print(b >= a) #True | ||
+ | print(c >= b) #True | ||
+ | print(d >= a) #True | ||
+ | </syntaxhighlight> | ||
+ | |||
+ | === + === | ||
+ | |||
+ | The "addition" operator. Perform 3D vector addition. | ||
+ | |||
+ | <syntaxhighlight lang="Python"> | ||
+ | a = RLPy.RVector3(0, 1, 2) | ||
+ | b = RLPy.RVector3(1, 2, 3) | ||
+ | c = a + b | ||
+ | |||
+ | print(str(c.x) + ', ' + str(c.y) + ', ' + str(c.z)) #1.0, 3.0, 5.0 | ||
+ | </syntaxhighlight> | ||
+ | |||
+ | === - === | ||
+ | |||
+ | The "subtraction" operator. Perform 3D vector subtraction. | ||
+ | |||
+ | <syntaxhighlight lang="Python"> | ||
+ | a = RLPy.RVector3(0, 1, 2) | ||
+ | b = RLPy.RVector3(3, 2, 1) | ||
+ | c = b - a | ||
+ | |||
+ | print(str(c.x) + ', ' + str(c.y) + ', ' + str(c.z)) #3.0, 1.0, -1.0 | ||
+ | </syntaxhighlight> | ||
+ | |||
+ | === * === | ||
+ | |||
+ | The "multiplication" operator. Perform a scalar multiplication when the second operand is an integer or float. If the second operand is another 3D vector, then the respective x, y, z elements are multiplied. | ||
+ | |||
+ | <syntaxhighlight lang="Python"> | ||
+ | a = RLPy.RVector3(1, 2, 3) | ||
+ | b = a * 2 | ||
+ | c = a * a | ||
+ | |||
+ | print(str(b.x) + ', ' + str(b.y) + ', ' + str(b.z)) #2.0, 4.0, 6.0 | ||
+ | print(str(c.x) + ', ' + str(c.y) + ', ' + str(c.z)) #1.0, 4.0, 9.0 | ||
+ | </syntaxhighlight> | ||
+ | |||
+ | === / === | ||
+ | |||
+ | The "division" operator. Perform a scalar division when the second operand is an integer or float. If the second operand is another vector, then the respective x, y, z elements are divided. | ||
+ | |||
+ | <syntaxhighlight lang="Python"> | ||
+ | a = RLPy.RVector3(1, 2, 3) | ||
+ | b = a / 2 | ||
+ | c = RLPy.RVector3(2, 2, 10) | ||
+ | d = a / c | ||
+ | |||
+ | print(str(b.x) + ', ' + str(b.y) + ', ' + str(b.z)) #0.5, 1.0, 1.5 | ||
+ | print(str(d.x) + ', ' + str(d.y) + ', ' + str(d.z)) #0.5, 1.0, 0.3 | ||
+ | </syntaxhighlight> | ||
+ | |||
+ | === - === | ||
+ | |||
+ | The "unary minus" operator. Inverse the sign of each element. | ||
+ | |||
+ | <syntaxhighlight lang="Python"> | ||
+ | a = RLPy.RVector3(1, 2, 3) | ||
+ | b = -a | ||
+ | |||
+ | print(str(b.x) + ', ' + str(b.y) + ', ' + str(b.z)) #-1.0, -2.0, -3.0 | ||
+ | </syntaxhighlight> | ||
+ | |||
+ | === += === | ||
+ | |||
+ | The "addition assignment" operator. | ||
+ | |||
+ | <syntaxhighlight lang="Python"> | ||
+ | a = RLPy.RVector3(0, 1, 2) | ||
+ | b = RLPy.RVector3(1, 2, 3) | ||
+ | a += b | ||
+ | |||
+ | print(str(a.x) + ', ' + str(a.y)+ ', ' + str(a.z)) #1.0, 3.0, 5.0 | ||
+ | </syntaxhighlight> | ||
+ | |||
+ | === -= === | ||
+ | |||
+ | The "subtraction assignment" operator. | ||
+ | |||
+ | <syntaxhighlight lang="Python"> | ||
+ | a = RLPy.RVector3(0, 1, 4) | ||
+ | b = RLPy.RVector3(1, 2, 3) | ||
+ | a -= b | ||
+ | |||
+ | print(str(a.x) + ', ' + str(a.y) + ', ' + str(a.z)) #-1.0, -1.0, 1.0 | ||
+ | </syntaxhighlight> | ||
+ | |||
+ | === *= === | ||
+ | |||
+ | The "multiplication assignment" operator. Please refer to '''*''' operator for calculation method. | ||
+ | |||
+ | <syntaxhighlight lang="Python"> | ||
+ | a = RLPy.RVector3(1, 2, 3) | ||
+ | a *= 2 | ||
+ | b = RLPy.RVector3(1, 2, 3) | ||
+ | c = RLPy.RVector3(2, 3, 4) | ||
+ | b *= c | ||
+ | |||
+ | print(str(a.x) + ', ' + str(a.y) + ', ' + str(a.z)) #2.0, 4.0, 6.0 | ||
+ | print(str(b.x) + ', ' + str(b.y) + ', ' + str(b.z)) #2.0, 6.0, 12.0 | ||
+ | </syntaxhighlight> | ||
+ | |||
+ | === /= === | ||
+ | |||
+ | The "division assignment" operator. Please refer to '''/''' operator for calculation method. | ||
+ | |||
+ | <syntaxhighlight lang="Python"> | ||
+ | a = RLPy.RVector3(1, 2, 3) | ||
+ | a /= 2 | ||
+ | b = RLPy.RVector3(1, 2, 3) | ||
+ | c = RLPy.RVector3(2, 4, 2) | ||
+ | b /= c | ||
+ | |||
+ | print(str(a.x) + ', ' + str(a.y) + ', ' + str(a.z)) #0.5, 1.0, 1.5 | ||
+ | print(str(b.x) + ', ' + str(b.y) + ', ' + str(b.z)) #0.5, 0.5, 1.5 | ||
+ | </syntaxhighlight> | ||
+ | |||
+ | == Member Functions == | ||
+ | |||
+ | === AlmostTheSame ( self, vV ) === | ||
+ | |||
Determine the two vectors are the same with tolerance. | Determine the two vectors are the same with tolerance. | ||
− | ====Returns==== | + | |
+ | ==== Returns ==== | ||
:True if the vector is almost the same - bool | :True if the vector is almost the same - bool | ||
− | + | ||
− | + | ||
<syntaxhighlight lang="Python"> | <syntaxhighlight lang="Python"> | ||
− | RLPy.RVector3. | + | a = RLPy.RVector3(1, 2, 3) |
+ | b = RLPy.RVector3(1, 2, 3.000000001) | ||
+ | c = RLPy.RVector3(1, 2, 3.00001) | ||
+ | |||
+ | if a.AlmostTheSame(b): #True | ||
+ | print("a ≈ b") | ||
+ | if a.AlmostTheSame(c): #False | ||
+ | print("a ≈ c") | ||
</syntaxhighlight> | </syntaxhighlight> | ||
+ | |||
+ | === AlmostZero ( self ) === | ||
+ | |||
Determine the vector is zero vector. | Determine the vector is zero vector. | ||
− | ====Returns==== | + | |
+ | ==== Returns ==== | ||
:True if the vector is zero vector - bool | :True if the vector is zero vector - bool | ||
− | + | ||
− | ===Cross=== | + | <syntaxhighlight lang="Python"> |
+ | a = RLPy.RVector3(0.00000001, 0, 0.00000003) | ||
+ | b = RLPy.RVector3(0.00001, 0, 0) | ||
+ | |||
+ | if a.AlmostZero(): #True | ||
+ | print("a is ZERO vector.") | ||
+ | if b.AlmostZero(): #False | ||
+ | print("a is ZERO vector.") | ||
+ | </syntaxhighlight> | ||
+ | |||
+ | === Cross ( self, vV ) === | ||
+ | |||
<syntaxhighlight lang="Python"> | <syntaxhighlight lang="Python"> | ||
RLPy.RVector3.Cross ( self, vV ) | RLPy.RVector3.Cross ( self, vV ) | ||
</syntaxhighlight> | </syntaxhighlight> | ||
+ | |||
Calculate cross production of the two vectors. | Calculate cross production of the two vectors. | ||
− | ====Parameters==== | + | |
− | :vV[IN] The vector - RLPy.RVector3 | + | ==== Parameters ==== |
− | ====Returns==== | + | :'''vV''' [IN] The vector - RLPy.RVector3 |
+ | |||
+ | ==== Returns ==== | ||
:New vector which is the cross product of the two vectors - RLPy.RVector3 | :New vector which is the cross product of the two vectors - RLPy.RVector3 | ||
− | + | ||
− | + | ||
<syntaxhighlight lang="Python"> | <syntaxhighlight lang="Python"> | ||
− | RLPy.RVector3. | + | a = RLPy.RVector3(1, 0, 0) |
+ | b = RLPy.RVector3(0, 1, 0) | ||
+ | c = a.Cross(b) | ||
+ | |||
+ | print(str(c.x) + ', ' + str(c.y) + ', ' + str(c.z)) # 0.0, 0.0, 1.0 | ||
</syntaxhighlight> | </syntaxhighlight> | ||
+ | |||
+ | === Distance ( self, vV ) === | ||
+ | |||
Distance of the two vectors. | Distance of the two vectors. | ||
− | ====Parameters==== | + | |
− | :vV[IN] The vector - RLPy.RVector3 | + | ==== Parameters ==== |
− | ====Returns==== | + | :'''vV''' [IN] The vector - RLPy.RVector3 |
+ | |||
+ | ==== Returns ==== | ||
:The distance - float | :The distance - float | ||
− | + | ||
− | + | ||
<syntaxhighlight lang="Python"> | <syntaxhighlight lang="Python"> | ||
− | RLPy.RVector3. | + | a = RLPy.RVector3(3, 0, 0) |
+ | b = RLPy.RVector3(0, 4, 0) | ||
+ | |||
+ | print(a.Distance(b)) # 5.0 | ||
</syntaxhighlight> | </syntaxhighlight> | ||
+ | |||
+ | === Dot (self, vV) === | ||
+ | |||
Calculate dot production of the two vectors. | Calculate dot production of the two vectors. | ||
− | ====Parameters==== | + | |
− | :vV[IN] The vector - RLPy.RVector3 | + | ==== Parameters ==== |
− | ====Returns==== | + | :'''vV''' [IN] The 3D vector with which to compute the dot product - RLPy.RVector3 |
− | :The value of the dot | + | |
− | + | ==== Returns ==== | |
− | ===Interpolate=== | + | :The value of the dot product - float |
+ | |||
+ | <syntaxhighlight lang="Python"> | ||
+ | a = RLPy.RVector3(1, 2, 3) | ||
+ | b = RLPy.RVector3(1, 2, 3) | ||
+ | |||
+ | print(a.Dot(b)) # 14.0 | ||
+ | </syntaxhighlight> | ||
+ | |||
+ | === Interpolate === | ||
+ | |||
<syntaxhighlight lang="Python"> | <syntaxhighlight lang="Python"> | ||
RLPy.RVector3.Interpolate ( self, vRatio, vV ) | RLPy.RVector3.Interpolate ( self, vRatio, vV ) | ||
</syntaxhighlight> | </syntaxhighlight> | ||
+ | |||
Interpolate of the two vectors. | Interpolate of the two vectors. | ||
− | ====Parameters==== | + | |
− | :vRatio[IN] ratio value - float | + | ==== Parameters ==== |
− | :vV[IN] The vector - RLPy.RVector3 | + | :'''vRatio''' [IN] ratio value - float |
− | ====Returns==== | + | :'''vV''' [IN] The vector - RLPy.RVector3 |
+ | |||
+ | ==== Returns ==== | ||
:New vector which is the cross product of the two vectors - RLPy.RVector3 | :New vector which is the cross product of the two vectors - RLPy.RVector3 | ||
− | + | ||
− | + | ||
<syntaxhighlight lang="Python"> | <syntaxhighlight lang="Python"> | ||
− | RLPy.RVector3. | + | a = RLPy.RVector3(10, 20, 30) |
+ | b = RLPy.RVector3(1, 2, 3) | ||
+ | c = a.Interpolate(0.1, b) | ||
+ | |||
+ | print(str(c.x) + ', ' + str(c.y) + ', ' + str(c.z)) # 9.100000381469727, 18.200000762939453, 27.299999237060547 | ||
</syntaxhighlight> | </syntaxhighlight> | ||
− | Inverse this vector. | + | |
− | ====Returns==== | + | === Inverse ( self ) === |
+ | |||
+ | Invert all the elements of this 3D vector. | ||
+ | |||
+ | ==== Returns ==== | ||
:The inversed vector - RLPy.RVector3 | :The inversed vector - RLPy.RVector3 | ||
− | + | ||
− | ===Length=== | + | === Length ( self )=== |
− | + | ||
− | + | ||
− | + | ||
Length of the vector. | Length of the vector. | ||
− | ====Returns==== | + | |
− | : | + | ==== Returns ==== |
− | + | :Length of this vector - float | |
− | + | ||
<syntaxhighlight lang="Python"> | <syntaxhighlight lang="Python"> | ||
− | RLPy.RVector3. | + | a = RLPy.RVector3(1, 1, 1) |
+ | |||
+ | print(a.Length()) # 1.7320507764816284 | ||
</syntaxhighlight> | </syntaxhighlight> | ||
+ | |||
+ | === Normalize ( self ) === | ||
+ | |||
Normalizes this vector. | Normalizes this vector. | ||
− | ====Returns==== | + | |
+ | ==== Returns ==== | ||
:The normalized vector - float | :The normalized vector - float | ||
− | + | ||
− | + | ||
<syntaxhighlight lang="Python"> | <syntaxhighlight lang="Python"> | ||
− | RLPy.RVector3. | + | a = RLPy.RVector3(1, 1, 1) |
+ | |||
+ | print(a.Normalize()) #1.7320507764816284 | ||
+ | print(str(a.x) + ', ' + str(a.y) + ', ' + str(a.z)) # 0.5773502588272095, 0.5773502588272095, 0.5773502588272095 | ||
</syntaxhighlight> | </syntaxhighlight> | ||
+ | |||
+ | === SetX ( self, tX ) === | ||
+ | |||
Set the value of the x-axis. | Set the value of the x-axis. | ||
− | ====Parameters==== | + | |
− | :tX[IN] the value of the x-axis - float | + | ==== Parameters ==== |
− | + | :'''tX''' [IN] the value of the x-axis - float | |
− | + | ||
<syntaxhighlight lang="Python"> | <syntaxhighlight lang="Python"> | ||
− | RLPy.RVector3 | + | a = RLPy.RVector3(1, 1, 1) |
+ | a.SetX(10) | ||
+ | |||
+ | print(str(a.x) + ', ' + str(a.y) + ', ' + str(a.z)) # 10.0, 1.0, 1.0 | ||
</syntaxhighlight> | </syntaxhighlight> | ||
+ | |||
+ | === SetXYZ ( self, tX, tY, tZ ) === | ||
+ | |||
Set the value of the all axes. | Set the value of the all axes. | ||
− | ====Parameters==== | + | |
− | :tX[IN] the value of the x-axis - float | + | ==== Parameters ==== |
− | :tY[IN] the value of the y-axis - float | + | :'''tX''' [IN] the value of the x-axis - float |
− | :tZ[IN] the value of the z-axis - float | + | :'''tY''' [IN] the value of the y-axis - float |
− | + | :'''tZ''' [IN] the value of the z-axis - float | |
− | + | ||
<syntaxhighlight lang="Python"> | <syntaxhighlight lang="Python"> | ||
− | RLPy.RVector3. | + | a = RLPy.RVector3(1, 1, 1) |
+ | a.SetXYZ(10, 20, 30) | ||
+ | |||
+ | print(str(a.x) + ', ' + str(a.y) + ', ' + str(a.z)) # 10.0, 20.0, 30.0 | ||
</syntaxhighlight> | </syntaxhighlight> | ||
+ | |||
+ | === SetY ( self, tY ) === | ||
+ | |||
Set the value of the y-axis. | Set the value of the y-axis. | ||
− | ====Parameters==== | + | |
− | :tX[IN] the value of the y-axis. | + | ==== Parameters ==== |
− | + | :'''tX''' [IN] the value of the y-axis. | |
− | + | ||
<syntaxhighlight lang="Python"> | <syntaxhighlight lang="Python"> | ||
− | RLPy.RVector3. | + | a = RLPy.RVector3(1, 1, 1) |
+ | a.SetY(10) | ||
+ | |||
+ | print(str(a.x) + ', ' + str(a.y) + ', ' + str(a.z)) # 1.0, 10.0, 1.0 | ||
</syntaxhighlight> | </syntaxhighlight> | ||
+ | |||
+ | === SetZ ( self, tZ ) === | ||
+ | |||
Set the value of the z-axis. | Set the value of the z-axis. | ||
− | ====Parameters==== | + | |
+ | ==== Parameters ==== | ||
:tX[IN] the value of the z-axis. | :tX[IN] the value of the z-axis. | ||
− | + | ||
− | + | ||
<syntaxhighlight lang="Python"> | <syntaxhighlight lang="Python"> | ||
− | RLPy.RVector3. | + | a = RLPy.RVector3(1, 1, 1) |
+ | a.SetZ(10) | ||
+ | |||
+ | print(str(a.x) + ', ' + str(a.y) + ', ' + str(a.z)) # 1.0, 1.0, 10.0 | ||
</syntaxhighlight> | </syntaxhighlight> | ||
+ | |||
+ | === SquaredDistance ( self, vV ) === | ||
+ | |||
Squared distance of the two vectors. | Squared distance of the two vectors. | ||
− | ====Parameters==== | + | |
− | :vV[IN] The vector - RLPy.RVector3 | + | ==== Parameters ==== |
− | ====Returns==== | + | :'''vV''' [IN] The vector - RLPy.RVector3 |
+ | |||
+ | ==== Returns ==== | ||
:The Squared distance - float | :The Squared distance - float | ||
− | + | ||
− | + | ||
<syntaxhighlight lang="Python"> | <syntaxhighlight lang="Python"> | ||
− | RLPy.RVector3.SquaredLength ( | + | a = RLPy.RVector3(1, 1, 1) |
+ | |||
+ | print(a.SquaredLength()) # 3.0 | ||
</syntaxhighlight> | </syntaxhighlight> | ||
+ | |||
+ | === SquaredLength ( self ) === | ||
+ | |||
Squared length of the vector. | Squared length of the vector. | ||
− | ====Returns==== | + | |
+ | ==== Returns ==== | ||
:The squared length of this vector - float | :The squared length of this vector - float | ||
− | + | ||
− | + | ||
<syntaxhighlight lang="Python"> | <syntaxhighlight lang="Python"> | ||
− | RLPy.RVector3 | + | a = RLPy.RVector3(1, 1, 1) |
+ | |||
+ | print(a.SquaredLength()) # 3.0 | ||
</syntaxhighlight> | </syntaxhighlight> | ||
− | + | ||
− | + | === XY ( self ) === | |
− | + | ||
− | + | Get the x and y elements of the 2D vector. | |
− | ===XY=== | + | |
− | + | ==== Returns ==== | |
− | + | ||
− | + | ||
− | Get the | + | |
− | ====Returns==== | + | |
:Return the 2D vector - RLPy.RVector2 | :Return the 2D vector - RLPy.RVector2 | ||
− | + | ||
− | + | ||
<syntaxhighlight lang="Python"> | <syntaxhighlight lang="Python"> | ||
− | RLPy.RVector3 | + | a = RLPy.RVector3(1, 2, 3) |
− | + | b = a.XY() | |
− | + | ||
− | + | print(str(b.x) + ', ' + str(b.y)) # 1.0, 2.0 | |
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
</syntaxhighlight> | </syntaxhighlight> | ||
− | |||
− | |||
− |
Revision as of 00:25, 24 February 2020
Contents
- 1 Description
- 2 Constructor & Destructor
- 3 Operators
- 4 Member Functions
- 4.1 AlmostTheSame ( self, vV )
- 4.2 AlmostZero ( self )
- 4.3 Cross ( self, vV )
- 4.4 Distance ( self, vV )
- 4.5 Dot (self, vV)
- 4.6 Interpolate
- 4.7 Inverse ( self )
- 4.8 Length ( self )
- 4.9 Normalize ( self )
- 4.10 SetX ( self, tX )
- 4.11 SetXYZ ( self, tX, tY, tZ )
- 4.12 SetY ( self, tY )
- 4.13 SetZ ( self, tZ )
- 4.14 SquaredDistance ( self, vV )
- 4.15 SquaredLength ( self )
- 4.16 XY ( self )
- Main article: Modules.
- Last modified: 02/24/2020
Description
This class represent the 3D vector (x, y, z). This class provides access to RLPy's internal vector3 math library allowing vector3s to be handled easily, in a manner compatible with internal RLPy data structures. It also supports operators and provides some constants for your convenience:
Constant | Description |
---|---|
RVector3.ZERO | 3D zero vector: (0, 0, 0) |
RVector3.UNIT_X | 3D x unit vector: (1, 0, 0) |
RVector3.UNIT_Y | 3D y unit vector: (0, 1, 0) |
RVector3.UNIT_Z | 3D z unit vector: (0, 0, 1) |
RVector3.UNIT_XY | 3D vector: (1, 1, 0) |
RVector3.UNIT_YZ | 3D vector: (0, 1, 1) |
RVector3.UNIT_XZ | 3D vector: (1, 0, 1) |
RVector3.UNIT_XYZ | 3D vector: (1, 1, 1) |
Constructor & Destructor
__init__ ( self )
a = RLPy.RVector3()
__init__ ( self, x, y, z )
a = RLPy.RVector3(1, 2, 3)
Parameters
- x [IN] a numerical value for x coordinate - float or int
- y [IN] a numerical value for y coordinate - float or int
- z [IN] a numerical value for z coordinate - float or int
__init__ ( self, args )
The constructor. Initialize a new RVector3 object with another RVector3 object: args. This new RVector3 object has the same value as args.
a = RLPy.RVector3(1, 2, 3)
b = RLPy.RVector3(a)
Parameters
- args [IN] a RVector3 object - RLPy.RVector3
Operators
==
The "equal to" operator.
a = RLPy.RVector3(1, 2, 3)
b = a
print(a == b) #True
!=
The "not equal to" operator.
a = RLPy.RVector3()
b = RLPy.RVector3(1, 2, 3)
print(a != b) #True
<
The "less than" operator. Similar to string comparison: Returns True upon the first match that is less than and False if it is greater than. If the current comparison is equal, continue onto the next element. If all elements are equal then return False.
a = RLPy.RVector3(0, 1, 5)
b = RLPy.RVector3(0, 2, 0)
c = RLPy.RVector3(1, 0, 1)
d = RLPy.RVector3(0, 1, 5)
print(a < b) #True
print(b < c) #True
print(a < d) #False
>
The "greater than" operator. Similar to string comparison: Returns True upon the first match that is greater than and False if it is less than. If the current comparison is equal, continue onto the next element. If all elements are equal then return False.
a = RLPy.RVector3(0, 1, 5)
b = RLPy.RVector3(0, 1, 7)
c = RLPy.RVector3(1, 0, 1)
d = RLPy.RVector3(0, 1, 5)
print(b > a) #True
print(c > b) #True
print(d > a) #False
<=
The "less than or equal" operator. Similar to string comparison: Returns True upon the first match that is less than and False if it is greater than. If the current comparison is equal, continue onto the next element. If all elements are equal then return True.
a = RLPy.RVector3(0, 1, 5)
b = RLPy.RVector3(0, 2, 0)
c = RLPy.RVector3(1, 0, 1)
d = RLPy.RVector3(0, 1, 5)
print(a<= b) #True
print(b<= c) #True
print(a<= d) #True
>=
The "greater than or equal" operator. Similar to string comparison: Returns True upon the first match that is greater than and False if it is less than. If the current comparison is equal, continue onto the next element. If all elements are equal then return True.
a = RLPy.RVector3(0, 1, 5)
b = RLPy.RVector3(0, 1, 7)
c = RLPy.RVector3(1, 0, 1)
d = RLPy.RVector3(0, 1, 5)
print(b >= a) #True
print(c >= b) #True
print(d >= a) #True
+
The "addition" operator. Perform 3D vector addition.
a = RLPy.RVector3(0, 1, 2)
b = RLPy.RVector3(1, 2, 3)
c = a + b
print(str(c.x) + ', ' + str(c.y) + ', ' + str(c.z)) #1.0, 3.0, 5.0
-
The "subtraction" operator. Perform 3D vector subtraction.
a = RLPy.RVector3(0, 1, 2)
b = RLPy.RVector3(3, 2, 1)
c = b - a
print(str(c.x) + ', ' + str(c.y) + ', ' + str(c.z)) #3.0, 1.0, -1.0
*
The "multiplication" operator. Perform a scalar multiplication when the second operand is an integer or float. If the second operand is another 3D vector, then the respective x, y, z elements are multiplied.
a = RLPy.RVector3(1, 2, 3)
b = a * 2
c = a * a
print(str(b.x) + ', ' + str(b.y) + ', ' + str(b.z)) #2.0, 4.0, 6.0
print(str(c.x) + ', ' + str(c.y) + ', ' + str(c.z)) #1.0, 4.0, 9.0
/
The "division" operator. Perform a scalar division when the second operand is an integer or float. If the second operand is another vector, then the respective x, y, z elements are divided.
a = RLPy.RVector3(1, 2, 3)
b = a / 2
c = RLPy.RVector3(2, 2, 10)
d = a / c
print(str(b.x) + ', ' + str(b.y) + ', ' + str(b.z)) #0.5, 1.0, 1.5
print(str(d.x) + ', ' + str(d.y) + ', ' + str(d.z)) #0.5, 1.0, 0.3
-
The "unary minus" operator. Inverse the sign of each element.
a = RLPy.RVector3(1, 2, 3)
b = -a
print(str(b.x) + ', ' + str(b.y) + ', ' + str(b.z)) #-1.0, -2.0, -3.0
+=
The "addition assignment" operator.
a = RLPy.RVector3(0, 1, 2)
b = RLPy.RVector3(1, 2, 3)
a += b
print(str(a.x) + ', ' + str(a.y)+ ', ' + str(a.z)) #1.0, 3.0, 5.0
-=
The "subtraction assignment" operator.
a = RLPy.RVector3(0, 1, 4)
b = RLPy.RVector3(1, 2, 3)
a -= b
print(str(a.x) + ', ' + str(a.y) + ', ' + str(a.z)) #-1.0, -1.0, 1.0
*=
The "multiplication assignment" operator. Please refer to * operator for calculation method.
a = RLPy.RVector3(1, 2, 3)
a *= 2
b = RLPy.RVector3(1, 2, 3)
c = RLPy.RVector3(2, 3, 4)
b *= c
print(str(a.x) + ', ' + str(a.y) + ', ' + str(a.z)) #2.0, 4.0, 6.0
print(str(b.x) + ', ' + str(b.y) + ', ' + str(b.z)) #2.0, 6.0, 12.0
/=
The "division assignment" operator. Please refer to / operator for calculation method.
a = RLPy.RVector3(1, 2, 3)
a /= 2
b = RLPy.RVector3(1, 2, 3)
c = RLPy.RVector3(2, 4, 2)
b /= c
print(str(a.x) + ', ' + str(a.y) + ', ' + str(a.z)) #0.5, 1.0, 1.5
print(str(b.x) + ', ' + str(b.y) + ', ' + str(b.z)) #0.5, 0.5, 1.5
Member Functions
AlmostTheSame ( self, vV )
Determine the two vectors are the same with tolerance.
Returns
- True if the vector is almost the same - bool
a = RLPy.RVector3(1, 2, 3)
b = RLPy.RVector3(1, 2, 3.000000001)
c = RLPy.RVector3(1, 2, 3.00001)
if a.AlmostTheSame(b): #True
print("a ≈ b")
if a.AlmostTheSame(c): #False
print("a ≈ c")
AlmostZero ( self )
Determine the vector is zero vector.
Returns
- True if the vector is zero vector - bool
a = RLPy.RVector3(0.00000001, 0, 0.00000003)
b = RLPy.RVector3(0.00001, 0, 0)
if a.AlmostZero(): #True
print("a is ZERO vector.")
if b.AlmostZero(): #False
print("a is ZERO vector.")
Cross ( self, vV )
RLPy.RVector3.Cross ( self, vV )
Calculate cross production of the two vectors.
Parameters
- vV [IN] The vector - RLPy.RVector3
Returns
- New vector which is the cross product of the two vectors - RLPy.RVector3
a = RLPy.RVector3(1, 0, 0)
b = RLPy.RVector3(0, 1, 0)
c = a.Cross(b)
print(str(c.x) + ', ' + str(c.y) + ', ' + str(c.z)) # 0.0, 0.0, 1.0
Distance ( self, vV )
Distance of the two vectors.
Parameters
- vV [IN] The vector - RLPy.RVector3
Returns
- The distance - float
a = RLPy.RVector3(3, 0, 0)
b = RLPy.RVector3(0, 4, 0)
print(a.Distance(b)) # 5.0
Dot (self, vV)
Calculate dot production of the two vectors.
Parameters
- vV [IN] The 3D vector with which to compute the dot product - RLPy.RVector3
Returns
- The value of the dot product - float
a = RLPy.RVector3(1, 2, 3)
b = RLPy.RVector3(1, 2, 3)
print(a.Dot(b)) # 14.0
Interpolate
RLPy.RVector3.Interpolate ( self, vRatio, vV )
Interpolate of the two vectors.
Parameters
- vRatio [IN] ratio value - float
- vV [IN] The vector - RLPy.RVector3
Returns
- New vector which is the cross product of the two vectors - RLPy.RVector3
a = RLPy.RVector3(10, 20, 30)
b = RLPy.RVector3(1, 2, 3)
c = a.Interpolate(0.1, b)
print(str(c.x) + ', ' + str(c.y) + ', ' + str(c.z)) # 9.100000381469727, 18.200000762939453, 27.299999237060547
Inverse ( self )
Invert all the elements of this 3D vector.
Returns
- The inversed vector - RLPy.RVector3
Length ( self )
Length of the vector.
Returns
- Length of this vector - float
a = RLPy.RVector3(1, 1, 1)
print(a.Length()) # 1.7320507764816284
Normalize ( self )
Normalizes this vector.
Returns
- The normalized vector - float
a = RLPy.RVector3(1, 1, 1)
print(a.Normalize()) #1.7320507764816284
print(str(a.x) + ', ' + str(a.y) + ', ' + str(a.z)) # 0.5773502588272095, 0.5773502588272095, 0.5773502588272095
SetX ( self, tX )
Set the value of the x-axis.
Parameters
- tX [IN] the value of the x-axis - float
a = RLPy.RVector3(1, 1, 1)
a.SetX(10)
print(str(a.x) + ', ' + str(a.y) + ', ' + str(a.z)) # 10.0, 1.0, 1.0
SetXYZ ( self, tX, tY, tZ )
Set the value of the all axes.
Parameters
- tX [IN] the value of the x-axis - float
- tY [IN] the value of the y-axis - float
- tZ [IN] the value of the z-axis - float
a = RLPy.RVector3(1, 1, 1)
a.SetXYZ(10, 20, 30)
print(str(a.x) + ', ' + str(a.y) + ', ' + str(a.z)) # 10.0, 20.0, 30.0
SetY ( self, tY )
Set the value of the y-axis.
Parameters
- tX [IN] the value of the y-axis.
a = RLPy.RVector3(1, 1, 1)
a.SetY(10)
print(str(a.x) + ', ' + str(a.y) + ', ' + str(a.z)) # 1.0, 10.0, 1.0
SetZ ( self, tZ )
Set the value of the z-axis.
Parameters
- tX[IN] the value of the z-axis.
a = RLPy.RVector3(1, 1, 1)
a.SetZ(10)
print(str(a.x) + ', ' + str(a.y) + ', ' + str(a.z)) # 1.0, 1.0, 10.0
SquaredDistance ( self, vV )
Squared distance of the two vectors.
Parameters
- vV [IN] The vector - RLPy.RVector3
Returns
- The Squared distance - float
a = RLPy.RVector3(1, 1, 1)
print(a.SquaredLength()) # 3.0
SquaredLength ( self )
Squared length of the vector.
Returns
- The squared length of this vector - float
a = RLPy.RVector3(1, 1, 1)
print(a.SquaredLength()) # 3.0
XY ( self )
Get the x and y elements of the 2D vector.
Returns
- Return the 2D vector - RLPy.RVector2
a = RLPy.RVector3(1, 2, 3)
b = a.XY()
print(str(b.x) + ', ' + str(b.y)) # 1.0, 2.0