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

From Reallusion Wiki!
Jump to: navigation, search
m (*=)
m (*=)
Line 223: Line 223:
  
 
=== *= ===
 
=== *= ===
The “multiplication assignment” operator. Please refer to the '''*''' operator for calculation method.  
+
The “multiplication assignment” operator. Please refer to '''*''' operator for calculation method.  
 
<syntaxhighlight lang="Python">
 
<syntaxhighlight lang="Python">
 
a = RLPy.RVector2(1, 2)
 
a = RLPy.RVector2(1, 2)

Revision as of 22:24, 19 February 2020

Main article: Modules.

Description

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.

RVector2 also provides some constants for your convenience:

Constant Description
RVector2.ZERO 2D zero vector: (0, 0)
RVector2.UNIT_X 2D x unit vector: (1, 0)
RVector2.UNIT_Y 2D y unit vector: (0, 1)
RVector2.UNIT_XY 2D vector: (1, 1)

Constructors & Destructors

__init__

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

a = RLPy.RVector2()

__init__( self, x, y )

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

a = RLPy.RVector2(1, 2)

Parameters

x [IN] a numerical value for x coordinate - float or int
y [IN] a numerical value for y coordinate - float or int


__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.

a = RLPy.RVector2(1, 2)
b = a
print(a == b)                         #True

!=

The “not equal to” operator.

a = RLPy.RVector2()
b = RLPy.RVector2(1, 2)
print(a != b)                         #True

<

The “less than” operator. Similar to string comparison, the process is as follows:

  1. Compare the first element (x).
  2. If the first vector2.x is greater than the second vector2.x then return False.
  3. If the first vector2.x is less than the second vector2.x then return True.
  4. If the first and second vector2.x are equal then continue to compare the second element (y).
  5. If the first vector2.y is greater than the second vector2.y then return False.
  6. If the first vector2.y is less than the second vector2.y then return True.
  7. If the first and second vector2.y are equal then return False.
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. 這個運算子作用在 RVector2 的比對方式類似字串比對. 先比對一個元素(x), 比較大就回傳 true, 比較小就回傳 false, 如果數值一樣就繼續比對第二個元素(y), 比對方式與第一個元素相同, 但如果數值一樣就回傳 false.

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 to” operator. 這個運算子作用在 RVector2 的比對方式類似字串比對. 先比對一個元素(x), 比較小就回傳 true, 比較大就回傳 false, 如果數值一樣就繼續比對第二個元素(y), 比對方式與第一個元素相同, 但如果數值一樣就回傳 true.

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 to” operator. 這個運算子作用在 RVector2 的比對方式類似字串比對. 先比對一個元素(x), 比較大就回傳 true, 比較小就回傳 false, 如果數值一樣就繼續比對第二個元素(y), 比對方式與第一個元素相同, 但如果數值一樣就回傳 true.

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.

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.

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. 當第二個運算元是scalar (int or float)時, perform 2D vector multiplication with scalar. 當第二個運算元是 RVector2 時則會把對應元素相乘.

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. 當第二個運算元是scalar (int or float)時, perform 2D vector multiplication with scalar (multiply with 1/2nd-operand). 當第二個運算元是 RVector2 時則會把對應元素相除.

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.

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.

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. Please refer to * operator for calculation method.

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. Please refer to the / operator for calculation method.

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

RLPy.RVector2.AddWithWeight ( self, vSrc, fWeight )

Add a vector with weight.

Parameters

vSrc [IN] The vector to add - RLPy.RVector2

fWeight [IN] The value of weight - float


Dot

RLPy.RVector2.Dot ( self, vV )

Calculate dot production of the two vectors.

Parameters

vV [IN] The vector - RLPy.RVector2

Returns

The value of the dot production - float

Inverse

RLPy.RVector2.Inverse ( self )

Inverse this vector.

Returns

The inversed vector - RLPy.RVector2

Length

RLPy.RVector2.Length ( self )

Length of the vector.

Returns

The length of this vector - float

Normalize

RLPy.RVector2.Normalize ( self )

Normalizes this vector.

Returns

The normalized vector - float

SetX

RLPy.RVector2.SetX ( self, tX )

Set the value of the x-axis.

Parameters

tX [IN] the value of the x-axis - float


SetY

RLPy.RVector2.SetY ( self, tY )

Set the value of the y-axis.

Parameters

tX [IN] the value of the y-axis.


SquaredLength

RLPy.RVector2.SquaredLength ( self )

Squared length of the vector.

Returns

The squared length of this vector - float

X

RLPy.RVector2.X ( self, args )

Get the value of the x-axis.

Returns

The value of the x-axis - float

Y

RLPy.RVector2.Y ( self, args )

Get the value of the y-axis.

Returns

The value of the y-axis - float