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

From Reallusion Wiki!
Jump to: navigation, search
m (-=)
m
 
(24 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}}
==Description==
+
{{last_modified}}
  
2D Vector math class for float values. This class provides access to RLPy's internal vector math library allowing vectors to be handled effortlessly, and in a manner compatible with internal RLPy data structures.  It also supports operators and vector related functions.
+
== Description ==
  
RVector2 also provides some constants for your convenience:
+
This class represents a 2D vector (x, y). This class provides access to RLPy's internal 2D vector math library allowing 2D 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"
Line 12: Line 12:
 
|-
 
|-
 
|RVector2.ZERO
 
|RVector2.ZERO
|2D zero vector: (0, 0)
+
|2D zero vector2: (0, 0)
 
|-
 
|-
 
|RVector2.UNIT_X
 
|RVector2.UNIT_X
|2D x unit vector: (1, 0)
+
|2D x unit vector2: (1, 0)
 
|-
 
|-
 
|RVector2.UNIT_Y
 
|RVector2.UNIT_Y
|2D y unit vector: (0, 1)
+
|2D y unit vector2: (0, 1)
 
|-
 
|-
 
|RVector2.UNIT_XY
 
|RVector2.UNIT_XY
|2D vector: (1, 1)
+
|2D unit vector2: (1, 1)
 
|}
 
|}
  
==Constructors & Destructors==
+
== Constructors & Destructors ==
  
===__init__===
+
=== __init__ ===
  
Initialize a new RVector2 object as a 2D zero vector: (0, 0).
+
Initialize a new RVector2 object as a zeroed 2D vector: (0, 0).
  
<syntaxhighlight lang="Python">
+
<syntaxhighlight lang="python">
 
a = RLPy.RVector2()
 
a = RLPy.RVector2()
 
</syntaxhighlight>
 
</syntaxhighlight>
  
----
+
=== __init__( self, x, y ) ===
  
===__init__( self, x, y )===
+
Initialize a new [[IC_Python_API:RLPy_RVector2|RVector2]] object as a 2D vector: (x, y).
  
Initialize a new RVector2 object as a 2D vector: (x, y).
+
==== Parameters ====
 +
:'''x''' [IN] a numerical value for x coordinate - float / int
 +
:'''y''' [IN] a numerical value for y coordinate - float / int
  
<syntaxhighlight lang="Python">
+
<syntaxhighlight lang="python">
 
a = RLPy.RVector2(1, 2)
 
a = RLPy.RVector2(1, 2)
 
</syntaxhighlight>
 
</syntaxhighlight>
  
====Parameters====
+
=== __init__( self, args ) ===
  
x  [IN]    a numerical value for x coordinate - float or int<br/>
+
Initialize a new [[IC_Python_API:RLPy_RVector2|RVector2]] object with another [[IC_Python_API:RLPy_RVector2|RVector2]] object: args. This new [[IC_Python_API:RLPy_RVector2|RVector2]] object has the same value as args.
y  [IN]    a numerical value for y coordinate - float or int<br/>
+
 
+
----
+
 
+
===__init__( self, args )===
+
 
+
Initialize a new RVector2 object with another RVector2 object: args. This new RVector2 object has the same value as args.
+
 
+
<syntaxhighlight lang="Python">
+
  
 +
<syntaxhighlight lang="python">
 
a = RLPy.RVector2(1, 2)
 
a = RLPy.RVector2(1, 2)
 
b = RLPy.RVector2(a)
 
b = RLPy.RVector2(a)
 
 
</syntaxhighlight>
 
</syntaxhighlight>
  
==Operators==
+
== Operators ==
 +
 
 
=== == ===
 
=== == ===
The “equal to” operator.
+
 
<syntaxhighlight lang="Python">
+
The "equal to" operator.
 +
 
 +
See Also: [[#!=|!=]]
 +
 
 +
<syntaxhighlight lang="python">
 
a = RLPy.RVector2(1, 2)
 
a = RLPy.RVector2(1, 2)
 
b = a
 
b = a
print(a == b)                         #True
+
 
 +
print(a == b) #True
 
</syntaxhighlight>
 
</syntaxhighlight>
  
----
 
 
=== != ===
 
=== != ===
The “not equal to” operator.
+
 
<syntaxhighlight lang="Python">
+
The "not equal to" operator.
 +
 
 +
See Also: [[#==|==]]
 +
 
 +
<syntaxhighlight lang="python">
 
a = RLPy.RVector2()
 
a = RLPy.RVector2()
 
b = RLPy.RVector2(1, 2)
 
b = RLPy.RVector2(1, 2)
print(a != b)                         #True
+
 
 +
print(a != b) #True
 
</syntaxhighlight>
 
</syntaxhighlight>
  
----
 
 
=== < ===
 
=== < ===
  
The “less than” operator. Similar to string comparison, the process is as follows:
+
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'''.
  
# Compare the first element (x).
+
See Also: [[#<=|<=]]
# If the first vector2.x is greater than the second vector2.x then return '''False'''.
+
# If the first vector2.x is less than the second vector2.x then return '''True'''.
+
# If the first and second vector2.x are equal then continue to compare the second element (y).
+
# If the first vector2.y is greater than the second vector2.y then return '''False'''.
+
# If the first vector2.y is less than the second vector2.y then return '''True'''.
+
# If the first and second vector2.y are equal then return '''False'''.
+
  
<syntaxhighlight lang="Python">
+
<syntaxhighlight lang="python">
 
a = RLPy.RVector2(0, 0)
 
a = RLPy.RVector2(0, 0)
 
b = RLPy.RVector2(0, 1)
 
b = RLPy.RVector2(0, 1)
Line 99: Line 95:
 
d = RLPy.RVector2(0, 0)
 
d = RLPy.RVector2(0, 0)
  
print(a < b)                       #True
+
print(a< b) #True
print(b < c)                       #True
+
print(b< c) #True
print(a < d)                       #False
+
print(a< d) #False
 
</syntaxhighlight>
 
</syntaxhighlight>
 
----
 
  
 
=== > ===
 
=== > ===
The “greater than” operator. 這個運算子作用在 RVector2 的比對方式類似字串比對. 先比對一個元素(x), 比較大就回傳 true, 比較小就回傳 false, 如果數值一樣就繼續比對第二個元素(y), 比對方式與第一個元素相同, 但如果數值一樣就回傳 false.
+
 
<syntaxhighlight lang="Python">
+
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: [[#>=|>=]]
 +
 
 +
<syntaxhighlight lang="python">
 
a = RLPy.RVector2(0, 0)
 
a = RLPy.RVector2(0, 0)
 
b = RLPy.RVector2(0, 1)
 
b = RLPy.RVector2(0, 1)
Line 114: Line 112:
 
d = RLPy.RVector2(0, 0)
 
d = RLPy.RVector2(0, 0)
  
print(b > a)                       #True
+
print(b >a) #True
print(c > b)                       #True
+
print(c >b) #True
print(d > a)                       #False
+
print(d >a) #False
 
</syntaxhighlight>
 
</syntaxhighlight>
  
----
 
 
=== <= ===
 
=== <= ===
The “less than or equal to” operator. 這個運算子作用在 RVector2 的比對方式類似字串比對. 先比對一個元素(x), 比較小就回傳 true, 比較大就回傳 false, 如果數值一樣就繼續比對第二個元素(y), 比對方式與第一個元素相同, 但如果數值一樣就回傳 true.
+
 
<syntaxhighlight lang="Python">
+
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: [[#<|<]]
 +
 
 +
<syntaxhighlight lang="python">
 
a = RLPy.RVector2(0, 0)
 
a = RLPy.RVector2(0, 0)
 
b = RLPy.RVector2(0, 1)
 
b = RLPy.RVector2(0, 1)
Line 128: Line 129:
 
d = RLPy.RVector2(0, 0)
 
d = RLPy.RVector2(0, 0)
  
print(a <= b)                     #True
+
print(a<= b) #True
print(b <= c)                     #True
+
print(b<= c) #True
print(a <= d)                     #True
+
print(a<= d) #True
 
</syntaxhighlight>
 
</syntaxhighlight>
  
----
 
 
=== >= ===
 
=== >= ===
The “greater than or equal to” operator. 這個運算子作用在 RVector2 的比對方式類似字串比對. 先比對一個元素(x), 比較大就回傳 true, 比較小就回傳 false, 如果數值一樣就繼續比對第二個元素(y), 比對方式與第一個元素相同, 但如果數值一樣就回傳 true.
+
 
<syntaxhighlight lang="Python">
+
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: [[#>|>]]
 +
 
 +
<syntaxhighlight lang="python">
 
a = RLPy.RVector2(0, 0)
 
a = RLPy.RVector2(0, 0)
 
b = RLPy.RVector2(0, 1)
 
b = RLPy.RVector2(0, 1)
Line 142: Line 146:
 
d = RLPy.RVector2(0, 0)
 
d = RLPy.RVector2(0, 0)
  
print(b >= a)                       #True
+
print(b >= a) #True
print(c >= b)                       #True
+
print(c >= b) #True
print(d >= a)                       #True
+
print(d >= a) #True
 
</syntaxhighlight>
 
</syntaxhighlight>
  
----
 
 
=== + ===
 
=== + ===
The “addition” operator. Perform 2D vector addition.
+
 
<syntaxhighlight lang="Python">
+
The "addition" operator. Perform 2D vector addition.
 +
 
 +
See Also: [[#+=|+=]]
 +
 
 +
<syntaxhighlight lang="python">
 
a = RLPy.RVector2(0, 1)
 
a = RLPy.RVector2(0, 1)
 
b = RLPy.RVector2(1, 2)
 
b = RLPy.RVector2(1, 2)
 
c = a + b
 
c = a + b
print(str(c.x) + ', ' + str(c.y))       #1.0, 3.0
+
 
 +
print(str(c.x) + ', ' + str(c.y)) # 1.0, 3.0
 
</syntaxhighlight>
 
</syntaxhighlight>
  
----
 
 
=== - ===
 
=== - ===
The “subtraction” operator. Perform 2D vector subtraction.
+
 
<syntaxhighlight lang="Python">
+
The "subtraction" operator. Perform 2D vector subtraction.
 +
 
 +
See Also: [[#-=|-=]]
 +
 
 +
<syntaxhighlight lang="python">
 
a = RLPy.RVector2(0, 1)
 
a = RLPy.RVector2(0, 1)
 
b = RLPy.RVector2(1, 3)
 
b = RLPy.RVector2(1, 3)
c = b - a  
+
c = b - a
print(str(c.x) + ', ' + str(c.y))       #1.0, 2.0
+
 
 +
print(str(c.x) + ', ' + str(c.y)) # 1.0, 2.0
 
</syntaxhighlight>
 
</syntaxhighlight>
  
----
 
 
=== * ===
 
=== * ===
The “multiplication” operator. 當第二個運算元是scalar (int or float)時, perform 2D vector multiplication with scalar. 當第二個運算元是 RVector2 時則會把對應元素相乘.
+
 
<syntaxhighlight lang="Python">
+
The "multiplication" operator. Perform a scalar multiplication when the second operand is an integer or float. If the second operand is another 2D vector, then the corresponding elements are multiplied.
 +
 
 +
See Also: [[#*=|*=]]
 +
 
 +
<syntaxhighlight lang="python">
 
a = RLPy.RVector2(1, 2)
 
a = RLPy.RVector2(1, 2)
 
b = a * 2
 
b = a * 2
 
c = RLPy.RVector2(2, 3)
 
c = RLPy.RVector2(2, 3)
 
d = a * c
 
d = a * c
print(str(b.x) + ', ' + str(b.y))     #2.0, 4.0
+
 
print(str(d.x) + ', ' + str(d.y))     #2.0, 6.0
+
print(str(b.x) + ', ' + str(b.y)) # 2.0, 4.0
 +
print(str(d.x) + ', ' + str(d.y)) # 2.0, 6.0
 
</syntaxhighlight>
 
</syntaxhighlight>
  
----
 
 
=== / ===
 
=== / ===
The “division” operator. 當第二個運算元是scalar (int or float)時, perform 2D vector multiplication with scalar (multiply with 1/2nd-operand). 當第二個運算元是 RVector2 時則會把對應元素相除.
+
 
<syntaxhighlight lang="Python">
+
The "division" operator. Perform a scalar division when the second operand is an integer or float. If the second operand is another 2D vector, then the corresponding elements are divided.
 +
 
 +
See Also: [[#/=|/=]]
 +
 
 +
<syntaxhighlight lang="python">
 
a = RLPy.RVector2(1, 2)
 
a = RLPy.RVector2(1, 2)
 
b = a / 2
 
b = a / 2
 
c = RLPy.RVector2(2, 3)
 
c = RLPy.RVector2(2, 3)
 
d = a / c
 
d = a / c
print(str(b.x) + ', ' + str(b.y))   #0.5, 1.0
+
 
print(str(d.x) + ', ' + str(d.y))   #0.5, 0.6666666865348816
+
print(str(b.x) + ', ' + str(b.y)) # 0.5, 1.0
 +
print(str(d.x) + ', ' + str(d.y)) # 0.5, 0.6666666865348816
 
</syntaxhighlight>
 
</syntaxhighlight>
  
----
 
 
=== - ===
 
=== - ===
The “unary minus” operator. Inverse the sign of each element.
+
 
<syntaxhighlight lang="Python">
+
The "unary minus" operator. Inverse the sign of each element.
 +
 
 +
<syntaxhighlight lang="python">
 
a = RLPy.RVector2(1, 2)
 
a = RLPy.RVector2(1, 2)
 
b = -a
 
b = -a
print(str(b.x) + ', ' + str(b.y))     #-1.0, -2.0
+
 
 +
print(str(b.x) + ', ' + str(b.y)) # -1.0, -2.0
 
</syntaxhighlight>
 
</syntaxhighlight>
  
----
 
 
=== += ===
 
=== += ===
The “addition assignment” operator.
+
 
<syntaxhighlight lang="Python">
+
The "addition assignment" operator.
 +
 
 +
See Also: [[#+|+]]
 +
 
 +
<syntaxhighlight lang="python">
 
a = RLPy.RVector2(0, 1)
 
a = RLPy.RVector2(0, 1)
 
b = RLPy.RVector2(1, 2)
 
b = RLPy.RVector2(1, 2)
 
a += b
 
a += b
print(str(a.x) + ', ' + str(a.y))     #1.0, 3.0
+
 
 +
print(str(a.x) + ', ' + str(a.y)) # 1.0, 3.0
 
</syntaxhighlight>
 
</syntaxhighlight>
  
---- 
 
 
=== -= ===
 
=== -= ===
The “subtraction assignment” operator.
+
 
<syntaxhighlight lang="Python">
+
The "subtraction assignment" operator.
 +
 
 +
See Also: [[#-|-]]
 +
 
 +
<syntaxhighlight lang="python">
 
a = RLPy.RVector2(0, 1)
 
a = RLPy.RVector2(0, 1)
 
b = RLPy.RVector2(1, 2)
 
b = RLPy.RVector2(1, 2)
 
a -= b
 
a -= b
print(str(a.x) + ', ' + str(a.y))     #-1.0, -1.0
+
 
 +
print(str(a.x) + ', ' + str(a.y)) # -1.0, -1.0
 
</syntaxhighlight>
 
</syntaxhighlight>
 
----
 
  
 
=== *= ===
 
=== *= ===
The “multiplication assignment” operator. 計算方式請參考 * 運算子.
+
 
<syntaxhighlight lang="Python">
+
The "multiplication assignment" operator. For the calculation method, refer to the '''*''' operator.
 +
 
 +
See Also: [[#*|*]]
 +
 
 +
<syntaxhighlight lang="python">
 
a = RLPy.RVector2(1, 2)
 
a = RLPy.RVector2(1, 2)
 
a *= 2
 
a *= 2
Line 230: Line 262:
 
c = RLPy.RVector2(2, 3)
 
c = RLPy.RVector2(2, 3)
 
b *= c
 
b *= c
print(str(a.x) + ', ' + str(a.y))     #2.0, 4.0
+
 
print(str(b.x) + ', ' + str(b.y))     #2.0, 6.0
+
print(str(a.x) + ', ' + str(a.y)) # 2.0, 4.0
 +
print(str(b.x) + ', ' + str(b.y)) # 2.0, 6.0
 
</syntaxhighlight>
 
</syntaxhighlight>
  
----
 
 
=== /= ===
 
=== /= ===
The “division assignment” operator. Please refer to the '''/''' operator for calculation method.
+
 
<syntaxhighlight lang="Python">
+
The "division assignment" operator. For the calculation method, refer to the '''/''' operator.
 +
 
 +
See Also: [[#/|/]]
 +
 
 +
<syntaxhighlight lang="python">
 
a = RLPy.RVector2(1, 2)
 
a = RLPy.RVector2(1, 2)
 
a /= 2
 
a /= 2
Line 243: Line 279:
 
c = RLPy.RVector2(2, 3)
 
c = RLPy.RVector2(2, 3)
 
b /= c
 
b /= c
print(str(a.x) + ', ' + str(a.y))    #0.5, 1.0
 
print(str(b.x) + ', ' + str(b.y))    #0.5, 0.6666666865348816
 
</syntaxhighlight>
 
  
==Member Functions==
+
print(str(a.x) + ', ' + str(a.y)) # 0.5, 1.0
===AddWithWeight===
+
print(str(b.x) + ', ' + str(b.y)) # 0.5, 0.6666666865348816
<syntaxhighlight lang="Python">
+
RLPy.RVector2.AddWithWeight ( self, vSrc, fWeight )
+
 
</syntaxhighlight>
 
</syntaxhighlight>
Add a vector with weight.
 
====Parameters====
 
<div style="margin-left: 2em;">
 
  
'''vSrc''' [IN] The vector to add - RLPy.RVector2
+
== Member Functions ==
  
'''fWeight''' [IN] The value of weight - float
+
=== AddWithWeight ( self, vSrc, fWeight ) ===
</div>
+
 
-----
+
Add this and another 2D vector with weighting.
===Dot===
+
 
<syntaxhighlight lang="Python">
+
==== Parameters ====
RLPy.RVector2.Dot ( self, vV )
+
:'''vSrc''' [IN] The 2D vector to add - [[IC_Python_API:RLPy_RVector2|RVector2]]
 +
:'''fWeight''' [IN] The weight value - float
 +
 
 +
<syntaxhighlight lang="python">
 +
a = RLPy.RVector2(0, 1)
 +
b = RLPy.RVector2(1, 2)
 +
a.AddWithWeight(b, 2)
 +
 
 +
print(str(a.x) + ', ' + str(a.y)) # 2.0, 5.0
 
</syntaxhighlight>
 
</syntaxhighlight>
 +
 +
=== Dot ( self, vV ) ===
 +
 
Calculate dot production of the two vectors.
 
Calculate dot production of the two vectors.
====Parameters====
 
<div style="margin-left: 2em;">
 
  
'''vV''' [IN] The vector - RLPy.RVector2
+
==== Parameters ====
</div>
+
:'''vV''' [IN] Another 2D vector to compute dot product - [[IC_Python_API:RLPy_RVector2|RVector2]]
====Returns====
+
 
<div style="margin-left: 2em;">The value of the dot production - float
+
==== Returns ====
</div>
+
:The value of the dot product - float
-----
+
 
===Inverse===
+
<syntaxhighlight lang="python">
<syntaxhighlight lang="Python">
+
a = RLPy.RVector2(1, 2)
RLPy.RVector2.Inverse ( self )
+
dot = a.Dot( RLPy.RVector2(3, 5) )
 +
 
 +
print(dot) #13.0
 
</syntaxhighlight>
 
</syntaxhighlight>
Inverse this vector.
+
 
====Returns====
+
=== Inverse ( self ) ===
<div style="margin-left: 2em;">The inversed vector - RLPy.RVector2
+
 
</div>
+
Return the inverse of this 2D vector by inverting its x, y elements.
-----
+
 
===Length===
+
==== Returns ====
<syntaxhighlight lang="Python">
+
:A new 2D vector that is the inverse of this 2D vector - [[IC_Python_API:RLPy_RVector2|RVector2]]
RLPy.RVector2.Length ( self )
+
 
 +
<syntaxhighlight lang="python">
 +
a = RLPy.RVector2(0.5, 4)
 +
b = a.Inverse()
 +
 
 +
print(str(b.x) + ', ' + str(b.y)) # 2.0, 0.25
 
</syntaxhighlight>
 
</syntaxhighlight>
Length of the vector.
+
 
====Returns====
+
=== Length ( self ) ===
<div style="margin-left: 2em;">The length of this vector - float
+
 
</div>
+
Get the length of this 2D vector.
-----
+
 
===Normalize===
+
==== Returns ====
<syntaxhighlight lang="Python">
+
:The length of this 2D vector - float
RLPy.RVector2.Normalize ( self )
+
 
 +
<syntaxhighlight lang="python">
 +
a = RLPy.RVector2(3, 4)
 +
 
 +
print( str(a.Length()) ) # 5.0
 
</syntaxhighlight>
 
</syntaxhighlight>
Normalizes this vector.
+
 
====Returns====
+
=== Normalize ( self ) ===
<div style="margin-left: 2em;">The normalized vector - float
+
 
</div>
+
Normalize this 2D vector.
-----
+
 
===SetX===
+
==== Returns ====
<syntaxhighlight lang="Python">
+
:The length of this 2D vector before normalization - float
RLPy.RVector2.SetX ( self, tX )
+
 
 +
<syntaxhighlight lang="python">
 +
a = RLPy.RVector2(1, 1)
 +
b = a.Normalize()
 +
 
 +
print(str(a.x) + ', ' + str(a.y)) # 0.7071067690849304, 0.7071067690849304
 +
print(b) # 1.4142135381698608
 
</syntaxhighlight>
 
</syntaxhighlight>
Set the value of the x-axis.
 
====Parameters====
 
<div style="margin-left: 2em;">
 
  
'''tX''' [IN] the value of the x-axis - float
+
=== SetX ( self, tX ) ===
</div>
+
 
-----
+
Set the value of the x-axis on this 2D vector.
===SetY===
+
 
<syntaxhighlight lang="Python">
+
==== Parameters ====
RLPy.RVector2.SetY ( self, tY )
+
:'''tX''' [IN] the value of the x-axis - float
 +
 
 +
<syntaxhighlight lang="python">
 +
a = RLPy.RVector2(1, 1)
 +
a.SetX(10)
 +
 
 +
print(str(a.x) + ', ' + str(a.y)) # 10.0, 1.0
 
</syntaxhighlight>
 
</syntaxhighlight>
Set the value of the y-axis.
 
====Parameters====
 
<div style="margin-left: 2em;">
 
  
'''tX''' [IN] the value of the y-axis.
+
=== SetY ( self, tY ) ===
</div>
+
 
-----
+
Set the value of the y-axis on this 2D vector.
===SquaredLength===
+
 
<syntaxhighlight lang="Python">
+
==== Parameters ====
RLPy.RVector2.SquaredLength ( self )
+
:'''tY''' [IN] The value of the y-axis.
 +
 
 +
<syntaxhighlight lang="python">
 +
a = RLPy.RVector2(1, 1)
 +
a.SetY(10)
 +
 
 +
print(str(a.x) + ', ' + str(a.y)) # 1.0, 10.0
 
</syntaxhighlight>
 
</syntaxhighlight>
Squared length of the vector.
+
 
====Returns====
+
=== SquaredLength ( self ) ===
<div style="margin-left: 2em;">The squared length of this vector - float
+
 
</div>
+
Get the squared length of this 2D vector.
-----
+
 
===X===
+
==== Returns ====
<syntaxhighlight lang="Python">
+
:The squared length of this 2D vector - float
RLPy.RVector2.X ( self, args )
+
 
</syntaxhighlight>
+
<syntaxhighlight lang="python">
Get the value of the x-axis.
+
a = RLPy.RVector2(1, 1)
====Returns====
+
 
<div style="margin-left: 2em;">The value of the x-axis - float
+
print(a.SquaredLength()) # 2.0
</div>
+
-----
+
===Y===
+
<syntaxhighlight lang="Python">
+
RLPy.RVector2.Y ( self, args )
+
 
</syntaxhighlight>
 
</syntaxhighlight>
Get the value of the y-axis.
 
====Returns====
 
<div style="margin-left: 2em;">The value of the y-axis - float
 
</div>
 

Latest revision as of 20:25, 13 April 2020

Main article: Modules.
Last modified: 04/13/2020

Description

This class represents a 2D vector (x, y). This class provides access to RLPy's internal 2D vector math library allowing 2D 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
RVector2.ZERO 2D zero vector2: (0, 0)
RVector2.UNIT_X 2D x unit vector2: (1, 0)
RVector2.UNIT_Y 2D y unit vector2: (0, 1)
RVector2.UNIT_XY 2D unit vector2: (1, 1)

Constructors & Destructors

__init__

Initialize a new RVector2 object as a zeroed 2D vector: (0, 0).

a = RLPy.RVector2()

__init__( self, x, y )

Initialize a new RVector2 object as a 2D vector: (x, y).

Parameters

x [IN] a numerical value for x coordinate - float / int
y [IN] a numerical value for y coordinate - float / int
a = RLPy.RVector2(1, 2)

__init__( self, args )

Initialize a new RVector2 object with another RVector2 object: args. This new RVector2 object has the same value as args.

a = RLPy.RVector2(1, 2)
b = RLPy.RVector2(a)

Operators

==

The "equal to" operator.

See Also: !=

a = RLPy.RVector2(1, 2)
b = a

print(a == b) #True

!=

The "not equal to" operator.

See Also: ==

a = RLPy.RVector2()
b = RLPy.RVector2(1, 2)

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

a = RLPy.RVector2(0, 0)
b = RLPy.RVector2(0, 1)
c = RLPy.RVector2(1, 0)
d = RLPy.RVector2(0, 0)

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.

See Also: >=

a = RLPy.RVector2(0, 0)
b = RLPy.RVector2(0, 1)
c = RLPy.RVector2(1, 0)
d = RLPy.RVector2(0, 0)

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.

See Also: <

a = RLPy.RVector2(0, 0)
b = RLPy.RVector2(0, 1)
c = RLPy.RVector2(1, 0)
d = RLPy.RVector2(0, 0)

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.

See Also: >

a = RLPy.RVector2(0, 0)
b = RLPy.RVector2(0, 1)
c = RLPy.RVector2(1, 0)
d = RLPy.RVector2(0, 0)

print(b >= a) #True
print(c >= b) #True
print(d >= a) #True

+

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

See Also: +=

a = RLPy.RVector2(0, 1)
b = RLPy.RVector2(1, 2)
c = a + b

print(str(c.x) + ', ' + str(c.y)) # 1.0, 3.0

-

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

See Also: -=

a = RLPy.RVector2(0, 1)
b = RLPy.RVector2(1, 3)
c = b - a

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

*

The "multiplication" operator. Perform a scalar multiplication when the second operand is an integer or float. If the second operand is another 2D vector, then the corresponding elements are multiplied.

See Also: *=

a = RLPy.RVector2(1, 2)
b = a * 2
c = RLPy.RVector2(2, 3)
d = a * c

print(str(b.x) + ', ' + str(b.y)) # 2.0, 4.0
print(str(d.x) + ', ' + str(d.y)) # 2.0, 6.0

/

The "division" operator. Perform a scalar division when the second operand is an integer or float. If the second operand is another 2D vector, then the corresponding elements are divided.

See Also: /=

a = RLPy.RVector2(1, 2)
b = a / 2
c = RLPy.RVector2(2, 3)
d = a / c

print(str(b.x) + ', ' + str(b.y)) # 0.5, 1.0
print(str(d.x) + ', ' + str(d.y)) # 0.5, 0.6666666865348816

-

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

a = RLPy.RVector2(1, 2)
b = -a

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

+=

The "addition assignment" operator.

See Also: +

a = RLPy.RVector2(0, 1)
b = RLPy.RVector2(1, 2)
a += b

print(str(a.x) + ', ' + str(a.y)) # 1.0, 3.0

-=

The "subtraction assignment" operator.

See Also: -

a = RLPy.RVector2(0, 1)
b = RLPy.RVector2(1, 2)
a -= b

print(str(a.x) + ', ' + str(a.y)) # -1.0, -1.0

*=

The "multiplication assignment" operator. For the calculation method, refer to the * operator.

See Also: *

a = RLPy.RVector2(1, 2)
a *= 2
b = RLPy.RVector2(1, 2)
c = RLPy.RVector2(2, 3)
b *= c

print(str(a.x) + ', ' + str(a.y)) # 2.0, 4.0
print(str(b.x) + ', ' + str(b.y)) # 2.0, 6.0

/=

The "division assignment" operator. For the calculation method, refer to the / operator.

See Also: /

a = RLPy.RVector2(1, 2)
a /= 2
b = RLPy.RVector2(1, 2)
c = RLPy.RVector2(2, 3)
b /= c

print(str(a.x) + ', ' + str(a.y)) # 0.5, 1.0
print(str(b.x) + ', ' + str(b.y)) # 0.5, 0.6666666865348816

Member Functions

AddWithWeight ( self, vSrc, fWeight )

Add this and another 2D vector with weighting.

Parameters

vSrc [IN] The 2D vector to add - RVector2
fWeight [IN] The weight value - float
a = RLPy.RVector2(0, 1)
b = RLPy.RVector2(1, 2)
a.AddWithWeight(b, 2)

print(str(a.x) + ', ' + str(a.y)) # 2.0, 5.0

Dot ( self, vV )

Calculate dot production of the two vectors.

Parameters

vV [IN] Another 2D vector to compute dot product - RVector2

Returns

The value of the dot product - float
a = RLPy.RVector2(1, 2)
dot = a.Dot( RLPy.RVector2(3, 5) )

print(dot) #13.0

Inverse ( self )

Return the inverse of this 2D vector by inverting its x, y elements.

Returns

A new 2D vector that is the inverse of this 2D vector - RVector2
a = RLPy.RVector2(0.5, 4)
b = a.Inverse()

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

Length ( self )

Get the length of this 2D vector.

Returns

The length of this 2D vector - float
a = RLPy.RVector2(3, 4)

print( str(a.Length()) ) # 5.0

Normalize ( self )

Normalize this 2D vector.

Returns

The length of this 2D vector before normalization - float
a = RLPy.RVector2(1, 1)
b = a.Normalize()

print(str(a.x) + ', ' + str(a.y)) # 0.7071067690849304, 0.7071067690849304
print(b) # 1.4142135381698608

SetX ( self, tX )

Set the value of the x-axis on this 2D vector.

Parameters

tX [IN] the value of the x-axis - float
a = RLPy.RVector2(1, 1)
a.SetX(10)

print(str(a.x) + ', ' + str(a.y)) # 10.0, 1.0

SetY ( self, tY )

Set the value of the y-axis on this 2D vector.

Parameters

tY [IN] The value of the y-axis.
a = RLPy.RVector2(1, 1)
a.SetY(10)

print(str(a.x) + ', ' + str(a.y)) # 1.0, 10.0

SquaredLength ( self )

Get the squared length of this 2D vector.

Returns

The squared length of this 2D vector - float
a = RLPy.RVector2(1, 1)

print(a.SquaredLength()) # 2.0