Difference between revisions of "IC Python API:RLPy RVector4"

From Reallusion Wiki!
Jump to: navigation, search
m
m
Line 1: Line 1:
 
{{TOC}}
 
{{TOC}}
 
{{Parent|IC_Python_API:RL_Python_Modules|Modules}}
 
{{Parent|IC_Python_API:RL_Python_Modules|Modules}}
==Detailed Description==
+
{{last_modified}}
This class represent the 4D vector.
+
 
==Operators==
+
== Description ==
This class supports the following operators:
+
 
 +
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, in a manner compatible with internal RLPy data structures. It also supports operators and provides some constants for your convenience:
 +
 
 
{| class="wikitable"
 
{| class="wikitable"
!Member
+
!Constant
!Operation
+
!Syntax
+
 
!Description
 
!Description
!Example
 
 
|-
 
|-
! scope="row"|__add__
+
|RVector4.ZERO
|Addition
+
|4D zero vector: (0, 0, 0, 0)
|a + b
+
|Adds values on either side of the operator.
+
|a + b = 30
+
 
|-
 
|-
! scope="row"|__sub__
+
|RVector4.UNIT_X
|Subtraction
+
|4D x unit vector: (1, 0, 0, 0)
|a - b
+
|Subtracts right hand operand from left hand operand.
+
|a – b = -10
+
 
|-
 
|-
! scope="row"|__mul__
+
|RVector4.UNIT_Y
|Multiplication
+
|4D y unit vector: (0, 1, 0, 0)
|a * b
+
|Multiplies values on either side of the operator.
+
|a * b = 200
+
 
|-
 
|-
! scope="row"|__truediv__
+
|RVector4.UNIT_Z
|Division
+
|4D z unit vector: (0, 0, 1, 0)
|a / b
+
|Divides left hand operand by right hand operand.
+
|b / a = 2
+
 
|-
 
|-
! scope="row"|__neg__
+
|RVector4.UNIT_W
|Negation
+
|4D w unit vector: (0, 0, 0, 1)
| -a
+
|Return the value negated.
+
| a = -b
+
 
|-
 
|-
! scope="row"|__eq__
+
|RVector4.UNIT_XYZW
|Equality
+
|4D vector: (1, 1, 1, 1)
|a == b
+
|If the values of two operands are equal, then the condition becomes true.
+
|(a == b) is not true.
+
|-
+
! scope="row"|__ne__
+
|Difference
+
|a != b
+
|If values of two operands are not equal, then condition becomes true.
+
|(a != b) is true.
+
|-
+
! scope="row"|__gt__
+
|Greater Than
+
|a > b
+
|If the value of left operand is greater than the value of right operand, then condition becomes true.
+
|(a > b) is not true.
+
|-
+
! scope="row"|__lt__
+
|Less Than
+
|a < b
+
|If the value of left operand is less than the value of right operand, then condition becomes true.
+
|(a < b) is true.
+
|-
+
! scope="row"|__ge__
+
|Greater Than or Equal
+
|a >= b
+
|If the value of left operand is greater than or equal to the value of right operand, then condition becomes true.
+
|(a >= b) is not true.
+
|-
+
! scope="row"|__le__
+
|Less or Equal
+
|a <= b
+
|If the value of left operand is less than or equal to the value of right operand, then condition becomes true.
+
|(a <= b) is true.
+
|-
+
! scope="row"|__iadd__
+
|Addition (Inplace)
+
|a += b
+
|It adds right operand to the left operand and assign the result to left operand.
+
|c += a is equivalent to c = c + a
+
|-
+
! scope="row"|__isub__
+
|Subtraction (Inplace)
+
|a -= b
+
|It subtracts right operand from the left operand and assign the result to left operand.
+
|c -= a is equivalent to c = c - a
+
|-
+
! scope="row"|__imul__
+
|Multiply (Inplace)
+
|a *= b
+
|It multiplies right operand with the left operand and assign the result to left operand.
+
|c *= a is equivalent to c = c * a
+
|-
+
! scope="row"|__itruediv__
+
|Divide (Inplace)
+
|a /= b
+
|It divides left operand with the right operand and assign the result to left operand.
+
|c /= a is equivalent to c = c / ac /= a is equivalent to c = c / a
+
 
|}
 
|}
==Member Functions==
+
 
===Dot===
+
== Constructor & Destructor ==
 +
 
 +
=== __init__ ( ) ===
 +
 
 +
The constructor. Initialize a new RVector4 object as a zeroed 4D vector: (0, 0, 0, 0).
 +
 
 
<syntaxhighlight lang="Python">
 
<syntaxhighlight lang="Python">
RLPy.RVector4.Dot ( self, vV )
+
a = RLPy.RVector4()
 
</syntaxhighlight>
 
</syntaxhighlight>
Calculate dot production of the two vectors.
 
====Parameters====
 
<div style="margin-left: 2em;">
 
  
'''vV''' [IN] The vector - RLPy.RVector4
+
=== __init__ ( a, b, c, d ) ===
</div>
+
 
====Returns====
+
The constructor. Initialize a new RVector4 object as a 4D vector: (x, y, z, w).
<div style="margin-left: 2em;">The value of the dot production - float
+
 
</div>
+
==== Parameters ====
-----
+
:'''a'''[IN] A numerical value for x coordinate - float or int
===Inverse===
+
:'''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
 +
 
 
<syntaxhighlight lang="Python">
 
<syntaxhighlight lang="Python">
RLPy.RVector4.Inverse ( self )
+
a = RLPy.RVector4(1, 2, 3, 4)
 
</syntaxhighlight>
 
</syntaxhighlight>
Inverse this vector.
+
 
====Returns====
+
=== __init__ ( *args ) ===
<div style="margin-left: 2em;">The inversed vector - RLPy.RVector4
+
 
</div>
+
The constructor. Initialize a new RVector4 object with another RVector4 object: args. This new RVector4 object has the same value as args.
-----
+
 
===Length===
+
==== Parameters ====
 +
:'''args'''[IN] a RVector4 object - RLPy.RVector4
 +
 
 
<syntaxhighlight lang="Python">
 
<syntaxhighlight lang="Python">
RLPy.RVector4.Length ( self )
+
a = RLPy.RVector4(1, 2, 3, 4)
 +
b = RLPy.RVector4(a)
 
</syntaxhighlight>
 
</syntaxhighlight>
Length of the vector.
+
 
====Returns====
+
== Operators ==
<div style="margin-left: 2em;">The length of this vector - float
+
 
</div>
+
==== = ===
-----
+
 
===Normalize===
+
The "equal to" operator.
 +
 
 
<syntaxhighlight lang="Python">
 
<syntaxhighlight lang="Python">
RLPy.RVector4.Normalize ( self )
+
a = RLPy.RVector4(1, 2, 3, 4)
 +
b = a
 +
 
 +
print(a == b) #True
 
</syntaxhighlight>
 
</syntaxhighlight>
Normalizes this vector.
+
 
====Returns====
+
=== != ===
<div style="margin-left: 2em;">The normalized vector - float
+
 
</div>
+
The "not equal to" operator.
-----
+
 
===SetW===
+
 
<syntaxhighlight lang="Python">
 
<syntaxhighlight lang="Python">
RLPy.RVector4.SetW ( self, tW )
+
a = RLPy.RVector4()
 +
b = RLPy.RVector4(1, 2, 3, 4)
 +
 
 +
print(a != b) # True
 
</syntaxhighlight>
 
</syntaxhighlight>
Set the value of the w.
 
====Parameters====
 
<div style="margin-left: 2em;">
 
  
'''tX''' [IN] the value of the w.
+
=== < ===
</div>
+
 
-----
+
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'''.
===SetX===
+
 
 
<syntaxhighlight lang="Python">
 
<syntaxhighlight lang="Python">
RLPy.RVector4.SetX ( self, tX )
+
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>
Set the value of the x-axis.
 
====Parameters====
 
<div style="margin-left: 2em;">
 
  
'''tX''' [IN] the value of the x-axis - float
+
=== > ===
</div>
+
 
-----
+
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'''.
===SetY===
+
 
 
<syntaxhighlight lang="Python">
 
<syntaxhighlight lang="Python">
RLPy.RVector4.SetY ( self, tY )
+
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>
Set the value of the y-axis.
 
====Parameters====
 
<div style="margin-left: 2em;">
 
  
'''tX''' [IN] the value of the y-axis.
+
=== <= ===
</div>
+
 
-----
+
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'''.
===SetZ===
+
 
 
<syntaxhighlight lang="Python">
 
<syntaxhighlight lang="Python">
RLPy.RVector4.SetZ ( self, tZ )
+
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>
Set the value of the z-axis.
 
====Parameters====
 
<div style="margin-left: 2em;">
 
  
'''tX''' [IN] the value of the z-axis.
+
=== >= ===
</div>
+
 
-----
+
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'''.
===SquaredLength===
+
 
 
<syntaxhighlight lang="Python">
 
<syntaxhighlight lang="Python">
RLPy.RVector4.SquaredLength ( self )
+
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>
Squared length of the vector.
+
 
====Returns====
+
=== + ===
<div style="margin-left: 2em;">The squared length of this vector - float
+
 
</div>
+
The "addition" operator. Perform 4D vector addition.
-----
+
 
===W===
+
 
<syntaxhighlight lang="Python">
 
<syntaxhighlight lang="Python">
RLPy.RVector4.W ( self, args )
+
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>
Get the value of the w.
+
 
====Returns====
+
=== - ===
<div style="margin-left: 2em;">The value of the w - float
+
 
</div>
+
The "subtraction" operator. Perform 4D vector subtraction.
-----
+
 
===X===
+
 
<syntaxhighlight lang="Python">
 
<syntaxhighlight lang="Python">
RLPy.RVector4.X ( self, args )
+
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>
Get the value of the x-axis.
+
 
====Returns====
+
=== * ===
<div style="margin-left: 2em;">The value of the x-axis - float
+
 
</div>
+
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.
-----
+
 
===XY===
+
 
<syntaxhighlight lang="Python">
 
<syntaxhighlight lang="Python">
RLPy.RVector4.XY ( self )
+
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>
Get the element of the 2D vector.
+
 
====Returns====
+
=== / ===
<div style="margin-left: 2em;">Return the 2D vector - RLPy.RVector2
+
 
</div>
+
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.
-----
+
 
===XYZ===
+
 
<syntaxhighlight lang="Python">
 
<syntaxhighlight lang="Python">
RLPy.RVector4.XYZ ( self )
+
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>
Get the element of the 3D vector.
+
 
====Returns====
+
=== - ===
<div style="margin-left: 2em;">Return the 3D vector - RLPy.RVector3
+
 
</div>
+
The "unary minus" operator. Inverse the sign of each element.
-----
+
 
===Y===
+
 
<syntaxhighlight lang="Python">
 
<syntaxhighlight lang="Python">
RLPy.RVector4.Y ( self, args )
+
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>
Get the value of the y-axis.
+
 
====Returns====
+
=== += ===
<div style="margin-left: 2em;">The value of the y-axis - float
+
 
</div>
+
The "addition assignment" operator.
-----
+
 
===Z===
+
 
<syntaxhighlight lang="Python">
 
<syntaxhighlight lang="Python">
RLPy.RVector4.Z ( self, args )
+
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.
 +
 
 +
<syntaxhighlight lang="Python">
 +
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. 計算方式請參考 * 運算子.
 +
 
 +
<syntaxhighlight lang="Python">
 +
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. 計算方式請參考 / 運算子.
 +
 
 +
<syntaxhighlight lang="Python">
 +
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">
 +
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 - RLPy.RVector4
 +
 
 +
==== Returns ====
 +
Returns the value of the dot product - float
 +
 
 +
=== Inverse (self) ===
 +
 
 +
Invert every element of this 4D vector.
 +
 
 +
<syntaxhighlight lang="Python">
 +
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 - RLPy.RVector4
 +
 
 +
=== Length (self) ===
 +
 
 +
Length of the 4D vector. norm
 +
 
 +
==== Returns ====
 +
Returns the length of this 4D vector - float
 +
 
 +
<syntaxhighlight lang="Python">
 +
a = RLPy.RVector4(1, 1, 1, 1)
 +
 
 +
print(a.Length()) # 2.0
 +
</syntaxhighlight>
 +
 
 +
=== Normalize (self) ===
 +
 
 +
Normalizes this 4D vector.
 +
 
 +
==== Returns ====
 +
Returns the length of the 4D vector before normalization - float
 +
 
 +
=== SetW ===
 +
 
 +
Set the value of the w-axis.
 +
 
 +
<syntaxhighlight lang="Python">
 +
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">
 +
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">
 +
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">
 +
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">
 +
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 the 4D vector.
 +
 
 +
==== Returns ====
 +
:Returns the squared length of this 4D vector - float
 +
 
 +
<syntaxhighlight lang="Python">
 +
a = RLPy.RVector4(1, 1, 1, 1)
 +
 
 +
print(a.SquaredLength()) # 4.0
 +
</syntaxhighlight>
 +
 
 +
=== XY (self) ===
 +
 
 +
Get the first 2 elements of the 4D vector (X, Y) as a 2D vector.
 +
 
 +
==== Returns ====
 +
:Returns a 2D vector - RLPy.RVector2
 +
 
 +
<syntaxhighlight lang="Python">
 +
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 the 4D vector (X, Y, Z) as a 3D vector.
 +
 
 +
==== Returns ====
 +
:Returns a 3D vector - RLPy.RVector3
 +
 
 +
<syntaxhighlight lang="Python">
 +
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>
Get the value of the z-axis.
 
====Returns====
 
<div style="margin-left: 2em;">The value of the z-axis - float
 
</div>
 

Revision as of 19:26, 24 February 2020

Main article: Modules.
Last modified: 02/24/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, in a manner compatible with internal RLPy data structures. It also supports operators and provides some constants for your convenience:

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__ ( )

The constructor. Initialize a new RVector4 object as a zeroed 4D vector: (0, 0, 0, 0).

a = RLPy.RVector4()

__init__ ( a, b, c, d )

The constructor. Initialize a new RVector4 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
a = RLPy.RVector4(1, 2, 3, 4)

__init__ ( *args )

The constructor. Initialize a new RVector4 object with another RVector4 object: args. This new RVector4 object has the same value as args.

Parameters

args[IN] a RVector4 object - RLPy.RVector4
a = RLPy.RVector4(1, 2, 3, 4)
b = RLPy.RVector4(a)

Operators

= =

The "equal to" operator.

a = RLPy.RVector4(1, 2, 3, 4)
b = a

print(a == b) #True

!=

The "not equal to" operator.

a = RLPy.RVector4()
b = RLPy.RVector4(1, 2, 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.

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

>

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.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')

<=

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.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')

>=

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.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')

+

The "addition" operator. Perform 4D vector addition.

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

-

The "subtraction" operator. Perform 4D vector subtraction.

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

*

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.

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

/

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.

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

-

The "unary minus" operator. Inverse the sign of each element.

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

+=

The "addition assignment" operator.

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

-=

The "subtraction assignment" operator.

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

*=

The "multiplication assignment" operator. 計算方式請參考 * 運算子.

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

/=

The "division assignment" operator. 計算方式請參考 / 運算子.

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

Member Functions

Dot (self, vV)

Calculate dot product with the given vector.

a = RLPy.RVector4(1, 2, 3, 4)
b = RLPy.RVector4(1, 2, 3, 4)

print(a.Dot(b)) #30.0

Parameters

vV [IN] The vector to compute dot product - RLPy.RVector4

Returns

Returns the value of the dot product - float

Inverse (self)

Invert every element of this 4D vector.

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

Returns

Returns the inversed vector - RLPy.RVector4

Length (self)

Length of the 4D vector. norm

Returns

Returns the length of this 4D vector - float

a = RLPy.RVector4(1, 1, 1, 1)

print(a.Length()) # 2.0

Normalize (self)

Normalizes this 4D vector.

Returns

Returns the length of the 4D vector before normalization - float

SetW

Set the value of the w-axis.

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

Parameters

tW [IN] the value of the w-axis - float

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

SetX (self, tX)

Set the value of the x-axis.

Parameters

tX [IN] the value of the x-axis - float
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

SetY (self, tY)

Set the value of the y-axis.

Parameters

tY [IN] the value of the y-axis - float
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

SetZ (self, tZ)

Set the value of the z-axis.

Parameters

tZ [IN] the value of the z-axis - float
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

SquaredLength (self)

Squared length of the 4D vector.

Returns

Returns the squared length of this 4D vector - float
a = RLPy.RVector4(1, 1, 1, 1)

print(a.SquaredLength()) # 4.0

XY (self)

Get the first 2 elements of the 4D vector (X, Y) as a 2D vector.

Returns

Returns a 2D vector - RLPy.RVector2
a = RLPy.RVector4(1, 2, 3, 4)
b = a.XY()

print(str(b.x) + ', ' + str(b.y)) # 1.0, 2.0

XYZ (self)

Get the first 3 elements of the 4D vector (X, Y, Z) as a 3D vector.

Returns

Returns a 3D vector - RLPy.RVector3
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