CPoint Class Operators


The CPoint class supports several math operations between points and other objects or numbers. Operators make it possible for a script to contain expressions like the following:

P2 = 0.5 * P

-- creates a new point P2 half as large as P.

P3 = (P1 - P2) * 2.5

-- subtracts P2 from P1 and multiples the result by 2.5.

A primary difference between the operators and methods is that many of the operators create the result as a new CPoint object, whereas methods operate on the object that calls them. Operators are provided for the arithmetic operations +-*/% and for comparisons.

Addition

P1 + P2

Adds two points. For example,

P3 = P1 + P2

is equivalent to

P3 = NewPoint( P1 )

P3:Offset( P2 )

P + t

Adds 2 Lua array elements to a point. For example,

P2 = P + t

is equivalent to

P2 = NewPoint()

P2.x = P.x + t[0]

P2.y = P.y + t[1]

P + R

Adds a CRect to a point. This offsets the point by R:Width() and R:Height(). For example,

P2 = P + R

is equivalent to

P2 = NewPoint()

P2.x = P.x + R:Width()

P2.y = P.y + R:Height()

P + num

Adds a number to a point. For example,

P2 = P + 0.5

is equivalent to

P2 = NewPoint()

P2.x = P.x + 0.5

P2.y = P.y + 0.5

Subtraction

P1 - P2

Subtracts two points. For example,

P3 = P1 - P2

is equivalent to

P3 = NewPoint()

P3.x = P1.x - P2.x

P3.y = P1.y - P2.y

P - t

Subtracts 2 Lua array elements to a point. For example,

P2 = P - t

is equivalent to

P2 = NewPoint()

P2.x = P.x - t[0]

P2.y = P.y - t[1]

P - R

Subtracts a CRect size from the point. This offsets the point by -R:Width() and -R:Height(). For example,

P2 = P - R

is equivalent to

P2 = NewPoint()

P2.x = P.x - R:Width()

P2.y = P.y - R:Height()

P - num

Subtracts a number from a point. For example,

P2 = P - 0.5

is equivalent to

P2 = NewPoint()

P2.x = P.x - 0.5

P2.y = P.y - 0.5

Multiplication

P1 * P2

Multiples two points. For example,

P3 = P1 * P2

is equivalent to

P3 = NewPoint()

P3.x = P1.x * P2.x

P3.y = P1.y * P2.y

P * t

Multiples the point values by 2 Lua array elements. For example,

P2 = P * t

is equivalent to

P2 = NewPoint()

P2.x = P.x * t[0]

P2.y = P.y * t[1]

P * R

Multiples the point values by a CRect size. For example,

P2 = P * R

is equivalent to

P2 = NewPoint()

P2.x = P.x * R:Width()

P2.y = P.y * R:Height()

P * num

Multiples the point values by a number. For example,

P2 = P * 0.5

is equivalent to

P2 = NewPoint()

P2.x = P.x * 0.5

P2.y = P.y * 0.5

Division

P1 / P2

Divides two points. For example,

P3 = P1 / P2

is equivalent to

P3 = NewPoint()

P3.x = P1.x / P2.x

P3.y = P1.y / P2.y

P / t

Divides the point values by 2 Lua array elements. For example,

P2 = P / t

is equivalent to

P2 = NewPoint()

P2.x = P.x / t[0]

P2.y = P.y / t[1]

P / R

Divides the point values by a CRect size. For example,

P2 = P / R

is equivalent to

P2 = NewPoint()

P2.x = P.x / R:Width()

P2.y = P.y / R:Height()

P / num

Divides the point values by a number. For example,

P2 = P / 0.5

is equivalent to

P2 = NewPoint()

P2.x = P.x / 0.5

P2.y = P.y / 0.5

Modulus (Remainder)

P1 % P2

Calculates the remainder of dividing a point by another point. For example,

P3 = P1 % P2

is equivalent to

P3 = NewPoint()

P3.x = P1.x % P2.x

P3.y = P1.y % P2.y

P % t

Calculates the remainder of dividing a point by 2 Lua array elements. For example,

P2 = P % t

is equivalent to

P2 = NewPoint()

P2.x = P.x % t[0]

P2.y = P.y % t[1]

P % R

Calculates the remainder of dividing a point by a CRect size. For example,

P2 = P % R

is equivalent to

P2 = NewPoint()

P2.x = P.x % R:Width()

P2.y = P.y % R:Height()

P % num

Calculates the remainder of dividing a point by a number. For example,

P2 = P % 0.5

is equivalent to

P2 = NewPoint()

P2.x = P.x % 0.5

P2.y = P.y % 0.5

Unary Minus (Negation)

–P

Computes the unary minus (or negative) of the point values. For example,

P2 = -P

is equivalent to

P2 = NewPoint()

P2.x = -P.x

P2.y = -P.y

and

-P

is equivalent to

P.x = -P.x

P.y = -P.y

Comparison

==

For two points, P1 and P2, this operator returns true if each property of P1 is equal to the corresponding property of P2. For example,

if P2 == P1 then Func() end

is equivalent to

if P2.x == P1.x and P2.y == P1.y then

  Func()

end

<=

For two points, P1 and P2, this operator returns true if each property of P1 is less than or equal to the corresponding property of P2. For example,

if P2 <= P1 then Func() end

is equivalent to

if P2.x <= P1.x and P2.y <= P1.y then

  Func()

end

<

For two points, P1 and P2, this operator returns true if each property of P1 is less than the corresponding property of P2. For example,

if P2 < P1 then Func() end

is equivalent to

if P2.x < P1.x and P2.y < P1.y then

  Func()

end

Related Topics

CPoint class