Difference between revisions of "IC Python API:RLPy RVector4"
Chuck (RL) (Talk | contribs) m |
Chuck (RL) (Talk | contribs) m |
||
(5 intermediate revisions by the same user not shown) | |||
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 | + | |
− | + | == Description == | |
− | This class supports | + | |
+ | This class represent a 4D vector (x, y, z, w). This class provides access to RLPy's internal 4D vector math library allowing 4D vectors to be handled easily, and in a manner compatible with internal RLPy data structures. It also supports operators and provides some convenient constants: | ||
+ | |||
{| class="wikitable" | {| class="wikitable" | ||
− | ! | + | !Constant |
− | + | ||
− | + | ||
!Description | !Description | ||
− | |||
|- | |- | ||
− | + | |RVector4.ZERO | |
− | + | |4D zero vector: (0, 0, 0, 0) | |
− | + | ||
− | + | ||
− | | | + | |
|- | |- | ||
− | + | |RVector4.UNIT_X | |
− | + | |4D x unit vector: (1, 0, 0, 0) | |
− | + | ||
− | + | ||
− | | | + | |
|- | |- | ||
− | + | |RVector4.UNIT_Y | |
− | + | |4D y unit vector: (0, 1, 0, 0) | |
− | + | ||
− | + | ||
− | | | + | |
|- | |- | ||
− | + | |RVector4.UNIT_Z | |
− | + | |4D z unit vector: (0, 0, 1, 0) | |
− | + | ||
− | + | ||
− | | | + | |
|- | |- | ||
− | + | |RVector4.UNIT_W | |
− | + | |4D w unit vector: (0, 0, 0, 1) | |
− | + | ||
− | + | ||
− | | | + | |
|- | |- | ||
− | + | |RVector4.UNIT_XYZW | |
− | + | |4D vector: (1, 1, 1, 1) | |
− | + | ||
− | + | ||
− | |( | + | |
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
|} | |} | ||
− | == | + | |
− | === | + | == Constructor & Destructor == |
− | <syntaxhighlight lang=" | + | |
− | RLPy.RVector4 | + | === __init__ ( ) === |
+ | |||
+ | Initialize a new 4D vector object that is zeroed out: (0, 0, 0, 0). | ||
+ | |||
+ | <syntaxhighlight lang="python" line='line'> | ||
+ | a = RLPy.RVector4() | ||
</syntaxhighlight> | </syntaxhighlight> | ||
− | |||
− | |||
− | |||
− | + | === __init__ ( a, b, c, d ) === | |
− | + | ||
− | ==== | + | Initialize a new 4D vector object as a 4D vector: (x, y, z, w). |
− | + | ||
− | + | ==== Parameters ==== | |
− | - | + | :'''a''' [IN] A numerical value for x coordinate - float or int |
− | + | :'''b''' [IN] A numerical value for y coordinate - float or int | |
− | <syntaxhighlight lang=" | + | :'''c''' [IN] A numerical value for z coordinate - float or int |
− | RLPy.RVector4 | + | :'''d''' [IN] A numerical value for w coordinate - float or int |
+ | |||
+ | <syntaxhighlight lang="python" line='line'> | ||
+ | a = RLPy.RVector4(1, 2, 3, 4) | ||
</syntaxhighlight> | </syntaxhighlight> | ||
− | + | ||
− | === | + | === __init__ ( *args ) === |
− | + | ||
− | + | Initialize a new 4D vector object with another 4D vector object: args. This new 4D vector object has the same value as '''*args'''. | |
− | + | ||
− | === | + | ==== Parameters ==== |
− | <syntaxhighlight lang=" | + | :'''args''' [IN] a 4D vector object - [[IC_Python_API:RLPy_RVector4|RVector4]] |
− | RLPy.RVector4. | + | |
+ | <syntaxhighlight lang="python" line='line'> | ||
+ | a = RLPy.RVector4(1, 2, 3, 4) | ||
+ | b = RLPy.RVector4(a) | ||
</syntaxhighlight> | </syntaxhighlight> | ||
− | + | ||
− | == | + | == Operators == |
− | + | ||
− | + | === = === | |
− | + | ||
− | === | + | The "equal to" operator. |
− | <syntaxhighlight lang=" | + | |
− | RLPy.RVector4 | + | <syntaxhighlight lang="python" line='line'> |
+ | a = RLPy.RVector4(1, 2, 3, 4) | ||
+ | b = a | ||
+ | |||
+ | print(a == b) #True | ||
</syntaxhighlight> | </syntaxhighlight> | ||
− | + | ||
− | === | + | === != === |
− | + | ||
− | + | The "not equal to" operator. | |
− | + | ||
− | + | See Also: [[#==|==]] | |
− | <syntaxhighlight lang=" | + | |
− | RLPy.RVector4. | + | <syntaxhighlight lang="python" line='line'> |
+ | a = RLPy.RVector4() | ||
+ | b = RLPy.RVector4(1, 2, 3, 4) | ||
+ | |||
+ | print(a != b) # True | ||
</syntaxhighlight> | </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=" | + | See Also: [[#<=|<=]] |
− | RLPy.RVector4. | + | |
+ | <syntaxhighlight lang="python" line='line'> | ||
+ | a = RLPy.RVector4(0, 1, 5, 2) | ||
+ | b = RLPy.RVector4(0, 1, 5, 3) | ||
+ | c = RLPy.RVector4(1, 0, 1, 0) | ||
+ | d = RLPy.RVector4(0, 1, 5, 2) | ||
+ | |||
+ | print(a < b) # True | ||
+ | print(b < c) # True | ||
+ | print('b < c') | ||
+ | if a < d: # False | ||
+ | print('a < d') | ||
</syntaxhighlight> | </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=" | + | See Also: [[#>=|>=]] |
− | RLPy.RVector4. | + | |
+ | <syntaxhighlight lang="python" line='line'> | ||
+ | a = RLPy.RVector4(0, 1, 5, 2) | ||
+ | b = RLPy.RVector4(0, 1, 5, 3) | ||
+ | c = RLPy.RVector4(1, 0, 1, 0) | ||
+ | d = RLPy.RVector4(0, 1, 5, 2) | ||
+ | |||
+ | if b >a: # True | ||
+ | print('b >a') | ||
+ | if c >b: # True | ||
+ | print('c >b') | ||
+ | if a >d: # True | ||
+ | print('a >d') | ||
</syntaxhighlight> | </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=" | + | See Also: [[#<|<]] |
− | RLPy.RVector4. | + | |
+ | <syntaxhighlight lang="python" line='line'> | ||
+ | a = RLPy.RVector4(0, 1, 5, 2) | ||
+ | b = RLPy.RVector4(0, 1, 5, 3) | ||
+ | c = RLPy.RVector4(1, 0, 1, 0) | ||
+ | d = RLPy.RVector4(0, 1, 5, 2) | ||
+ | |||
+ | if a<= b: # True | ||
+ | print('a<= b') | ||
+ | if b<= c: # True | ||
+ | print('b<= c') | ||
+ | if a<= d: # True | ||
+ | print('a<= d') | ||
</syntaxhighlight> | </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=" | + | See Also: [[#>|>]] |
− | RLPy.RVector4. | + | |
+ | <syntaxhighlight lang="python" line='line'> | ||
+ | a = RLPy.RVector4(0, 1, 5, 2) | ||
+ | b = RLPy.RVector4(0, 1, 5, 3) | ||
+ | c = RLPy.RVector4(1, 0, 1, 0) | ||
+ | d = RLPy.RVector4(0, 1, 5, 2) | ||
+ | |||
+ | if b >= a: # True | ||
+ | print('b >= a') | ||
+ | if c >= b: # True | ||
+ | print('c >= b') | ||
+ | if a >= d: # False | ||
+ | print('a >= d') | ||
</syntaxhighlight> | </syntaxhighlight> | ||
− | + | ||
− | === | + | === + === |
− | + | ||
− | + | The "addition" operator. Perform a 4D vector addition. | |
− | + | ||
− | + | See Also: [[#+=|+=]] | |
− | <syntaxhighlight lang=" | + | |
− | RLPy.RVector4. | + | <syntaxhighlight lang="python" line='line'> |
+ | a = RLPy.RVector4(0, 1, 2, 3) | ||
+ | b = RLPy.RVector4(1, 2, 3, 4) | ||
+ | c = a + b | ||
+ | |||
+ | print(str(c.x) + ', ' + str(c.y) + ', ' + str(c.z) + ', ' + str(c.w)) # 1.0, 3.0, 5.0, 7.0 | ||
</syntaxhighlight> | </syntaxhighlight> | ||
− | + | ||
− | === | + | === - === |
− | + | ||
− | + | The "subtraction" operator. Perform a 4D vector subtraction. | |
− | -- | + | |
− | + | See Also: [[#-=|-=]] | |
− | <syntaxhighlight lang=" | + | |
− | RLPy.RVector4. | + | <syntaxhighlight lang="python" line='line'> |
+ | a = RLPy.RVector4(0, 1, 2, 3) | ||
+ | b = RLPy.RVector4(3, 2, 1, 0) | ||
+ | c = b - a | ||
+ | |||
+ | print(str(c.x) + ', ' + str(c.y) + ', ' + str(c.z) + ', ' + str(c.w)) # 3.0, 1.0, -1.0, -3.0 | ||
</syntaxhighlight> | </syntaxhighlight> | ||
− | + | ||
− | === | + | === * === |
− | + | ||
− | + | The "multiplication" operator. Perform a scalar multiplication when the second operand is an integer or float. If the second operand is another 4D vector, then the corresponding elements are multiplied. | |
− | + | ||
− | + | See Also: [[#*=|*=]] | |
− | <syntaxhighlight lang=" | + | |
− | RLPy.RVector4. | + | <syntaxhighlight lang="python" line='line'> |
+ | a = RLPy.RVector4(1, 2, 3, 4) | ||
+ | b = a * 2 | ||
+ | c = a * a | ||
+ | |||
+ | print(str(b.x) + ', ' + str(b.y) + ', ' + str(b.z) + ', ' + str(b.w)) # 2.0, 4.0, 6.0, 8.0 | ||
+ | print(str(c.x) + ', ' + str(c.y) + ', ' + str(c.z) + ', ' + str(c.w)) # 1.0, 4.0, 9.0, 16.0 | ||
</syntaxhighlight> | </syntaxhighlight> | ||
− | + | ||
− | === | + | === / === |
− | + | ||
− | + | The "division" operator. Perform a scalar division when the second operand is an integer or float. If the second operand is another 4D vector, then the corresponding elements are divided. | |
− | + | ||
− | == | + | See Also: [[#/=|/=]] |
− | <syntaxhighlight lang=" | + | |
− | RLPy.RVector4. | + | <syntaxhighlight lang="python" line='line'> |
+ | a = RLPy.RVector4(1, 2, 3, 4) | ||
+ | b = a / 2 | ||
+ | c = RLPy.RVector4(2, 2, 10, 2) | ||
+ | d = a / c | ||
+ | |||
+ | print(str(b.x) + ', ' + str(b.y) + ', ' + str(b.z) + ', ' + str(b.w)) # 0.5, 1.0, 1.5, 2.0 | ||
+ | print(str(d.x) + ', ' + str(d.y) + ', ' + str(d.z) + ', ' + str(d.w)) # 0.5, 1.0, 0.3, 2.0 | ||
</syntaxhighlight> | </syntaxhighlight> | ||
− | + | ||
− | === | + | === - === |
− | + | ||
− | + | The "unary minus" operator. Inverse the sign of each element. | |
− | -- | + | |
− | + | See Also: [[#-=|-=]] | |
− | <syntaxhighlight lang=" | + | |
− | RLPy.RVector4. | + | <syntaxhighlight lang="python" line='line'> |
+ | a = RLPy.RVector4(1, 2, 3, 4) | ||
+ | b = -a | ||
+ | |||
+ | print(str(b.x) + ', ' + str(b.y) + ', ' + str(b.z) + ', ' + str(b.w)) # -1.0, -2.0, -3.0, -4.0 | ||
</syntaxhighlight> | </syntaxhighlight> | ||
− | + | ||
− | ====Returns==== | + | === += === |
− | < | + | |
− | </ | + | The "addition assignment" operator. |
− | ----- | + | |
− | ===Z=== | + | See Also: [[#+|+]] |
− | <syntaxhighlight lang=" | + | |
− | RLPy.RVector4. | + | <syntaxhighlight lang="python" line='line'> |
+ | a = RLPy.RVector4(0, 1, 2, 3) | ||
+ | b = RLPy.RVector4(1, 2, 3, 4) | ||
+ | a += b | ||
+ | |||
+ | print(str(a.x) + ', ' + str(a.y) + ', ' + str(a.z) + ', ' + str(a.w)) # 1.0, 3.0, 5.0, 7.0 | ||
+ | </syntaxhighlight> | ||
+ | |||
+ | === -= === | ||
+ | |||
+ | The "subtraction assignment" operator. | ||
+ | |||
+ | See Also: [[#-|-]] | ||
+ | |||
+ | <syntaxhighlight lang="python" line='line'> | ||
+ | a = RLPy.RVector4(0, 1, 4, 5) | ||
+ | b = RLPy.RVector4(1, 2, 3, 1) | ||
+ | a -= b | ||
+ | |||
+ | print(str(a.x) + ', ' + str(a.y) + ', ' + str(a.z) + ', ' + str(a.w)) # -1.0, -1.0, 1.0, 4.0 | ||
+ | </syntaxhighlight> | ||
+ | |||
+ | === *= === | ||
+ | |||
+ | The "multiplication assignment" operator. | ||
+ | |||
+ | See Also: [[#*|*]] | ||
+ | |||
+ | <syntaxhighlight lang="python" line='line'> | ||
+ | a = RLPy.RVector4(1, 2, 3, 4) | ||
+ | a *= 2 | ||
+ | b = RLPy.RVector4(1, 2, 3, 4) | ||
+ | c = RLPy.RVector4(2, 3, 4, 5) | ||
+ | b *= c | ||
+ | |||
+ | print(str(a.x) + ', ' + str(a.y) + ', ' + str(a.z) + ', ' + str(a.w)) # 2.0, 4.0, 6.0, 8.0 | ||
+ | print(str(b.x) + ', ' + str(b.y) + ', ' + str(b.z) + ', ' + str(b.w)) # 2.0, 6.0, 12.0, 20.0 | ||
+ | </syntaxhighlight> | ||
+ | |||
+ | === /= === | ||
+ | |||
+ | The "division assignment" operator. | ||
+ | |||
+ | See Also: [[#/|/]] | ||
+ | |||
+ | <syntaxhighlight lang="python" line='line'> | ||
+ | a = RLPy.RVector4(1, 2, 3, 4) | ||
+ | a /= 2 | ||
+ | b = RLPy.RVector4(1, 2, 3, 4) | ||
+ | c = RLPy.RVector4(2, 4, 2, 2) | ||
+ | b /= c | ||
+ | |||
+ | print(str(a.x) + ', ' + str(a.y) + ', ' + str(a.z) + ', ' + str(a.w)) # 0.5, 1.0, 1.5, 2.0 | ||
+ | print(str(b.x) + ', ' + str(b.y) + ', ' + str(b.z) + ', ' + str(b.w)) # 0.5, 0.5, 1.5, 2.0 | ||
+ | </syntaxhighlight> | ||
+ | |||
+ | == Member Functions == | ||
+ | |||
+ | === Dot (self, vV) === | ||
+ | |||
+ | Calculate dot product with the given vector. | ||
+ | |||
+ | <syntaxhighlight lang="python" line='line'> | ||
+ | a = RLPy.RVector4(1, 2, 3, 4) | ||
+ | b = RLPy.RVector4(1, 2, 3, 4) | ||
+ | |||
+ | print(a.Dot(b)) #30.0 | ||
+ | </syntaxhighlight> | ||
+ | |||
+ | ==== Parameters ==== | ||
+ | '''vV''' [IN] The vector to compute dot product - [[IC_Python_API:RLPy_RVector4|RVector4]] | ||
+ | |||
+ | ==== Returns ==== | ||
+ | Returns the value of the dot product - float | ||
+ | |||
+ | === Inverse (self) === | ||
+ | |||
+ | Invert every element of this 4D vector. | ||
+ | |||
+ | <syntaxhighlight lang="python" line='line'> | ||
+ | a = RLPy.RVector4(0.5, 2, 4, 1) | ||
+ | b = a.Inverse() | ||
+ | |||
+ | print(str(b.x) + ', ' + str(b.y) + ', ' + str(b.z) + ', ' + str(b.w)) # 2.0, 0.5, 0.25, 1.0 | ||
+ | </syntaxhighlight> | ||
+ | |||
+ | ==== Returns ==== | ||
+ | Returns the inversed vector - [[IC_Python_API:RLPy_RVector4|RVector4]] | ||
+ | |||
+ | === Length (self) === | ||
+ | |||
+ | Get the length (magnitude) of this 4D vector. | ||
+ | |||
+ | ==== Returns ==== | ||
+ | 4D vector magnitude - float | ||
+ | |||
+ | <syntaxhighlight lang="python" line='line'> | ||
+ | a = RLPy.RVector4(1, 1, 1, 1) | ||
+ | |||
+ | print(a.Length()) # 2.0 | ||
+ | </syntaxhighlight> | ||
+ | |||
+ | === Normalize (self) === | ||
+ | |||
+ | Normalize this 4D vector. | ||
+ | |||
+ | ==== Returns ==== | ||
+ | Length of this 4D vector before normalization - float | ||
+ | |||
+ | === SetW === | ||
+ | |||
+ | Set the value of the w-axis. | ||
+ | |||
+ | <syntaxhighlight lang="python" line='line'> | ||
+ | a = RLPy.RVector4(1, 1, 1, 1) | ||
+ | |||
+ | print(a.Normalize()) # 2.0 | ||
+ | print(str(a.x) + ', ' + str(a.y) + ', ' + str(a.z) + ', ' + str(a.w)) # 0.5, 0.5, 0.5, 0.5 | ||
+ | </syntaxhighlight> | ||
+ | |||
+ | ==== Parameters ==== | ||
+ | '''tW''' [IN] the value of the w-axis - float | ||
+ | |||
+ | <syntaxhighlight lang="python" line='line'> | ||
+ | a = RLPy.RVector4(1, 1, 1, 1) | ||
+ | a.SetW(10) | ||
+ | |||
+ | print(str(a.x) + ', ' + str(a.y) + ', ' + str(a.z) + ', ' + str(a.w)) # 1.0, 1.0, 1.0, 10.0 | ||
+ | </syntaxhighlight> | ||
+ | |||
+ | === SetX (self, tX) === | ||
+ | |||
+ | Set the value of the x-axis. | ||
+ | |||
+ | ==== Parameters ==== | ||
+ | :'''tX''' [IN] the value of the x-axis - float | ||
+ | |||
+ | <syntaxhighlight lang="python" line='line'> | ||
+ | a = RLPy.RVector4(1, 1, 1, 1) | ||
+ | a.SetX(10) | ||
+ | |||
+ | print(str(a.x) + ', ' + str(a.y) + ', ' + str(a.z) + ', ' + str(a.w)) # 10.0, 1.0, 1.0, 1.0 | ||
+ | </syntaxhighlight> | ||
+ | |||
+ | === SetY (self, tY) === | ||
+ | |||
+ | Set the value of the y-axis. | ||
+ | |||
+ | ==== Parameters ==== | ||
+ | :'''tY''' [IN] the value of the y-axis - float | ||
+ | |||
+ | <syntaxhighlight lang="python" line='line'> | ||
+ | a = RLPy.RVector4(1, 1, 1, 1) | ||
+ | a.SetY(10) | ||
+ | |||
+ | print(str(a.x) + ', ' + str(a.y) + ', ' + str(a.z) + ', ' + str(a.w)) # 1.0, 10.0, 1.0, 1.0 | ||
+ | </syntaxhighlight> | ||
+ | |||
+ | === SetZ (self, tZ) === | ||
+ | |||
+ | Set the value of the z-axis. | ||
+ | |||
+ | ==== Parameters ==== | ||
+ | :'''tZ''' [IN] the value of the z-axis - float | ||
+ | |||
+ | <syntaxhighlight lang="python" line='line'> | ||
+ | a = RLPy.RVector4(1, 1, 1, 1) | ||
+ | a.SetZ(10) | ||
+ | |||
+ | print(str(a.x) + ', ' + str(a.y) + ', ' + str(a.z) + ', ' + str(a.w)) # 1.0, 1.0, 10.0, 1.0 | ||
+ | </syntaxhighlight> | ||
+ | |||
+ | === SquaredLength (self) === | ||
+ | |||
+ | Squared length of this 4D vector. | ||
+ | |||
+ | ==== Returns ==== | ||
+ | :Returns the squared length of this 4D vector - float | ||
+ | |||
+ | <syntaxhighlight lang="python" line='line'> | ||
+ | a = RLPy.RVector4(1, 1, 1, 1) | ||
+ | |||
+ | print(a.SquaredLength()) # 4.0 | ||
+ | </syntaxhighlight> | ||
+ | |||
+ | === XY (self) === | ||
+ | |||
+ | Get the first 2 elements of this 4D vector (X, Y) as a 2D vector. | ||
+ | |||
+ | ==== Returns ==== | ||
+ | :Returns a 2D vector - [[IC_Python_API:RLPy_RVector2|RVector2]] | ||
+ | |||
+ | <syntaxhighlight lang="python" line='line'> | ||
+ | a = RLPy.RVector4(1, 2, 3, 4) | ||
+ | b = a.XY() | ||
+ | |||
+ | print(str(b.x) + ', ' + str(b.y)) # 1.0, 2.0 | ||
+ | </syntaxhighlight> | ||
+ | |||
+ | === XYZ (self) === | ||
+ | |||
+ | Get the first 3 elements of this 4D vector (X, Y, Z) as a 3D vector. | ||
+ | |||
+ | ==== Returns ==== | ||
+ | :Returns a 3D vector - [[IC_Python_API:RLPy_RVector3|RVector3]] | ||
+ | |||
+ | <syntaxhighlight lang="python" line='line'> | ||
+ | a = RLPy.RVector4(1, 2, 3, 4) | ||
+ | b = a.XYZ() | ||
+ | |||
+ | print(str(b.x) + ', ' + str(b.y) + ', ' + str(b.z)) # 1.0, 2.0, 3.0 | ||
</syntaxhighlight> | </syntaxhighlight> | ||
− | |||
− | |||
− | |||
− |
Latest revision as of 18:47, 13 April 2020
- Main article: Modules.
- Last modified: 04/13/2020
Description
This class represent a 4D vector (x, y, z, w). This class provides access to RLPy's internal 4D vector math library allowing 4D vectors to be handled easily, and in a manner compatible with internal RLPy data structures. It also supports operators and provides some convenient constants:
Constant | Description |
---|---|
RVector4.ZERO | 4D zero vector: (0, 0, 0, 0) |
RVector4.UNIT_X | 4D x unit vector: (1, 0, 0, 0) |
RVector4.UNIT_Y | 4D y unit vector: (0, 1, 0, 0) |
RVector4.UNIT_Z | 4D z unit vector: (0, 0, 1, 0) |
RVector4.UNIT_W | 4D w unit vector: (0, 0, 0, 1) |
RVector4.UNIT_XYZW | 4D vector: (1, 1, 1, 1) |
Constructor & Destructor
__init__ ( )
Initialize a new 4D vector object that is zeroed out: (0, 0, 0, 0).
1 a = RLPy.RVector4()
__init__ ( a, b, c, d )
Initialize a new 4D vector object as a 4D vector: (x, y, z, w).
Parameters
- a [IN] A numerical value for x coordinate - float or int
- b [IN] A numerical value for y coordinate - float or int
- c [IN] A numerical value for z coordinate - float or int
- d [IN] A numerical value for w coordinate - float or int
1 a = RLPy.RVector4(1, 2, 3, 4)
__init__ ( *args )
Initialize a new 4D vector object with another 4D vector object: args. This new 4D vector object has the same value as *args.
Parameters
- args [IN] a 4D vector object - RVector4
1 a = RLPy.RVector4(1, 2, 3, 4)
2 b = RLPy.RVector4(a)
Operators
=
The "equal to" operator.
1 a = RLPy.RVector4(1, 2, 3, 4)
2 b = a
3
4 print(a == b) #True
!=
The "not equal to" operator.
See Also: ==
1 a = RLPy.RVector4()
2 b = RLPy.RVector4(1, 2, 3, 4)
3
4 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.
See Also: <=
1 a = RLPy.RVector4(0, 1, 5, 2)
2 b = RLPy.RVector4(0, 1, 5, 3)
3 c = RLPy.RVector4(1, 0, 1, 0)
4 d = RLPy.RVector4(0, 1, 5, 2)
5
6 print(a < b) # True
7 print(b < c) # True
8 print('b < c')
9 if a < d: # False
10 print('a < d')
>
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.
See Also: >=
1 a = RLPy.RVector4(0, 1, 5, 2)
2 b = RLPy.RVector4(0, 1, 5, 3)
3 c = RLPy.RVector4(1, 0, 1, 0)
4 d = RLPy.RVector4(0, 1, 5, 2)
5
6 if b >a: # True
7 print('b >a')
8 if c >b: # True
9 print('c >b')
10 if a >d: # True
11 print('a >d')
<=
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.
See Also: <
1 a = RLPy.RVector4(0, 1, 5, 2)
2 b = RLPy.RVector4(0, 1, 5, 3)
3 c = RLPy.RVector4(1, 0, 1, 0)
4 d = RLPy.RVector4(0, 1, 5, 2)
5
6 if a<= b: # True
7 print('a<= b')
8 if b<= c: # True
9 print('b<= c')
10 if a<= d: # True
11 print('a<= d')
>=
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.
See Also: >
1 a = RLPy.RVector4(0, 1, 5, 2)
2 b = RLPy.RVector4(0, 1, 5, 3)
3 c = RLPy.RVector4(1, 0, 1, 0)
4 d = RLPy.RVector4(0, 1, 5, 2)
5
6 if b >= a: # True
7 print('b >= a')
8 if c >= b: # True
9 print('c >= b')
10 if a >= d: # False
11 print('a >= d')
+
The "addition" operator. Perform a 4D vector addition.
See Also: +=
1 a = RLPy.RVector4(0, 1, 2, 3)
2 b = RLPy.RVector4(1, 2, 3, 4)
3 c = a + b
4
5 print(str(c.x) + ', ' + str(c.y) + ', ' + str(c.z) + ', ' + str(c.w)) # 1.0, 3.0, 5.0, 7.0
-
The "subtraction" operator. Perform a 4D vector subtraction.
See Also: -=
1 a = RLPy.RVector4(0, 1, 2, 3)
2 b = RLPy.RVector4(3, 2, 1, 0)
3 c = b - a
4
5 print(str(c.x) + ', ' + str(c.y) + ', ' + str(c.z) + ', ' + str(c.w)) # 3.0, 1.0, -1.0, -3.0
*
The "multiplication" operator. Perform a scalar multiplication when the second operand is an integer or float. If the second operand is another 4D vector, then the corresponding elements are multiplied.
See Also: *=
1 a = RLPy.RVector4(1, 2, 3, 4)
2 b = a * 2
3 c = a * a
4
5 print(str(b.x) + ', ' + str(b.y) + ', ' + str(b.z) + ', ' + str(b.w)) # 2.0, 4.0, 6.0, 8.0
6 print(str(c.x) + ', ' + str(c.y) + ', ' + str(c.z) + ', ' + str(c.w)) # 1.0, 4.0, 9.0, 16.0
/
The "division" operator. Perform a scalar division when the second operand is an integer or float. If the second operand is another 4D vector, then the corresponding elements are divided.
See Also: /=
1 a = RLPy.RVector4(1, 2, 3, 4)
2 b = a / 2
3 c = RLPy.RVector4(2, 2, 10, 2)
4 d = a / c
5
6 print(str(b.x) + ', ' + str(b.y) + ', ' + str(b.z) + ', ' + str(b.w)) # 0.5, 1.0, 1.5, 2.0
7 print(str(d.x) + ', ' + str(d.y) + ', ' + str(d.z) + ', ' + str(d.w)) # 0.5, 1.0, 0.3, 2.0
-
The "unary minus" operator. Inverse the sign of each element.
See Also: -=
1 a = RLPy.RVector4(1, 2, 3, 4)
2 b = -a
3
4 print(str(b.x) + ', ' + str(b.y) + ', ' + str(b.z) + ', ' + str(b.w)) # -1.0, -2.0, -3.0, -4.0
+=
The "addition assignment" operator.
See Also: +
1 a = RLPy.RVector4(0, 1, 2, 3)
2 b = RLPy.RVector4(1, 2, 3, 4)
3 a += b
4
5 print(str(a.x) + ', ' + str(a.y) + ', ' + str(a.z) + ', ' + str(a.w)) # 1.0, 3.0, 5.0, 7.0
-=
The "subtraction assignment" operator.
See Also: -
1 a = RLPy.RVector4(0, 1, 4, 5)
2 b = RLPy.RVector4(1, 2, 3, 1)
3 a -= b
4
5 print(str(a.x) + ', ' + str(a.y) + ', ' + str(a.z) + ', ' + str(a.w)) # -1.0, -1.0, 1.0, 4.0
*=
The "multiplication assignment" operator.
See Also: *
1 a = RLPy.RVector4(1, 2, 3, 4)
2 a *= 2
3 b = RLPy.RVector4(1, 2, 3, 4)
4 c = RLPy.RVector4(2, 3, 4, 5)
5 b *= c
6
7 print(str(a.x) + ', ' + str(a.y) + ', ' + str(a.z) + ', ' + str(a.w)) # 2.0, 4.0, 6.0, 8.0
8 print(str(b.x) + ', ' + str(b.y) + ', ' + str(b.z) + ', ' + str(b.w)) # 2.0, 6.0, 12.0, 20.0
/=
The "division assignment" operator.
See Also: /
1 a = RLPy.RVector4(1, 2, 3, 4)
2 a /= 2
3 b = RLPy.RVector4(1, 2, 3, 4)
4 c = RLPy.RVector4(2, 4, 2, 2)
5 b /= c
6
7 print(str(a.x) + ', ' + str(a.y) + ', ' + str(a.z) + ', ' + str(a.w)) # 0.5, 1.0, 1.5, 2.0
8 print(str(b.x) + ', ' + str(b.y) + ', ' + str(b.z) + ', ' + str(b.w)) # 0.5, 0.5, 1.5, 2.0
Member Functions
Dot (self, vV)
Calculate dot product with the given vector.
1 a = RLPy.RVector4(1, 2, 3, 4)
2 b = RLPy.RVector4(1, 2, 3, 4)
3
4 print(a.Dot(b)) #30.0
Parameters
vV [IN] The vector to compute dot product - RVector4
Returns
Returns the value of the dot product - float
Inverse (self)
Invert every element of this 4D vector.
1 a = RLPy.RVector4(0.5, 2, 4, 1)
2 b = a.Inverse()
3
4 print(str(b.x) + ', ' + str(b.y) + ', ' + str(b.z) + ', ' + str(b.w)) # 2.0, 0.5, 0.25, 1.0
Returns
Returns the inversed vector - RVector4
Length (self)
Get the length (magnitude) of this 4D vector.
Returns
4D vector magnitude - float
1 a = RLPy.RVector4(1, 1, 1, 1)
2
3 print(a.Length()) # 2.0
Normalize (self)
Normalize this 4D vector.
Returns
Length of this 4D vector before normalization - float
SetW
Set the value of the w-axis.
1 a = RLPy.RVector4(1, 1, 1, 1)
2
3 print(a.Normalize()) # 2.0
4 print(str(a.x) + ', ' + str(a.y) + ', ' + str(a.z) + ', ' + str(a.w)) # 0.5, 0.5, 0.5, 0.5
Parameters
tW [IN] the value of the w-axis - float
1 a = RLPy.RVector4(1, 1, 1, 1)
2 a.SetW(10)
3
4 print(str(a.x) + ', ' + str(a.y) + ', ' + str(a.z) + ', ' + str(a.w)) # 1.0, 1.0, 1.0, 10.0
SetX (self, tX)
Set the value of the x-axis.
Parameters
- tX [IN] the value of the x-axis - float
1 a = RLPy.RVector4(1, 1, 1, 1)
2 a.SetX(10)
3
4 print(str(a.x) + ', ' + str(a.y) + ', ' + str(a.z) + ', ' + str(a.w)) # 10.0, 1.0, 1.0, 1.0
SetY (self, tY)
Set the value of the y-axis.
Parameters
- tY [IN] the value of the y-axis - float
1 a = RLPy.RVector4(1, 1, 1, 1)
2 a.SetY(10)
3
4 print(str(a.x) + ', ' + str(a.y) + ', ' + str(a.z) + ', ' + str(a.w)) # 1.0, 10.0, 1.0, 1.0
SetZ (self, tZ)
Set the value of the z-axis.
Parameters
- tZ [IN] the value of the z-axis - float
1 a = RLPy.RVector4(1, 1, 1, 1)
2 a.SetZ(10)
3
4 print(str(a.x) + ', ' + str(a.y) + ', ' + str(a.z) + ', ' + str(a.w)) # 1.0, 1.0, 10.0, 1.0
SquaredLength (self)
Squared length of this 4D vector.
Returns
- Returns the squared length of this 4D vector - float
1 a = RLPy.RVector4(1, 1, 1, 1)
2
3 print(a.SquaredLength()) # 4.0
XY (self)
Get the first 2 elements of this 4D vector (X, Y) as a 2D vector.
Returns
- Returns a 2D vector - RVector2
1 a = RLPy.RVector4(1, 2, 3, 4)
2 b = a.XY()
3
4 print(str(b.x) + ', ' + str(b.y)) # 1.0, 2.0
XYZ (self)
Get the first 3 elements of this 4D vector (X, Y, Z) as a 3D vector.
Returns
- Returns a 3D vector - RVector3
1 a = RLPy.RVector4(1, 2, 3, 4)
2 b = a.XYZ()
3
4 print(str(b.x) + ', ' + str(b.y) + ', ' + str(b.z)) # 1.0, 2.0, 3.0