CRect Class Operators


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

R2 = 0.5 * R

-- creates a new rectangle R2 half as large as R.

R3 = (R1 - R2) * 2.5

-- subtracts R2 from R1 and multiples the result by 2.5.

Several operators have class method counterparts. For example, the alternative to using the + operator would be CRect:Offset. A primary difference between the operators and methods is that many of the operators create the result as a new CRect object, whereas methods operate on the object that calls them. Operators are provided for the arithmetic operations +-*/% and for comparisons.

Addition

R1 + R2

Adds two rectangles. For example,

R3 = R1 + R2

is equivalent to

R3 = NewRect( R1 )

R3:Offset( R2 )

R1 + t

Adds 4 Lua array elements to a rectangle. For example,

R2 = R + t

is equivalent to

R2 = NewRect()

R2.xmin = R1.xmin + t[0]

R2.xmax = R1.xmax + t[1]

R2.ymin = R1.ymin + t[2]

R2.ymax = R1.ymax + t[3]

R1 + P

Adds a CPoint to a rectangle. This offsets the rectangle by P.x and P.y. For example,

R2 = R + P

is equivalent to

R2 = NewRect()

R2.xmin = R.xmin + P.x

R2.xmax = R.xmax + P.x

R2.ymin = R.ymin + P.y

R2.ymax = R.ymax + P.y

R1 + num

Adds a number to a rectangle. For example,

R2 = R + 0.5

is equivalent to

R2 = NewRect()

R2.xmin = R.xmin + 0.5

R2.xmax = R.xmax + 0.5

R2.ymin = R.ymin + 0.5

R2.ymax = R.ymax + 0.5

Subtraction

R1 - R2

Subtracts two rectangles. For example,

R3 = R1 - R2

is equivalent to

R3 = NewRect()

R3.xmin = R2.xmin - R1.xmin

R3.xmax = R2.xmax - R1.xmax

R3.ymin = R2.ymin - R1.ymin

R3.ymax = R2.ymax - R1.ymax

R1 - t

Subtracts 4 Lua array elements from a rectangle. For example,

R2 = R - t

is equivalent to

R2 = NewRect()

R2.xmin = R1.xmin - t[0]

R2.xmax = R1.xmax - t[1]

R2.ymin = R1.ymin - t[2]

R2.ymax = R1.ymax - t[3]

R1 - P

Subtracts a CPoint from a rectangle. For example,

R2 = R - P

is equivalent to

R2 = NewRect()

R2.xmin = R.xmin - P.x

R2.xmax = R.xmax - P.x

R2.ymin = R.ymin - P.y

R2.ymax = R.ymax - P.y

R1 - num

Subtracts a number from a rectangle. For example,

R2 = R - 0.5

is equivalent to

R2 = NewRect()

R2.xmin = R.xmin - 0.5

R2.xmax = R.xmax - 0.5

R2.ymin = R.ymin - 0.5

R2.ymax = R.ymax - 0.5

Multiplication

R1 * R2

Multiplies two rectangles. For example,

R3 = R1 * R2

is equivalent to

R3 = NewRect()

R3.xmin = R2.xmin * R1.xmin

R3.xmax = R2.xmax * R1.xmax

R3.ymin = R2.ymin * R1.ymin

R3.ymax = R2.ymax * R1.ymax

R1 * t

Multiplies a rectangle by 4 Lua array elements. For example,

R2 = R * t

is equivalent to

R2 = NewRect()

R2.xmin = R1.xmin * t[0]

R2.xmax = R1.xmax * t[1]

R2.ymin = R1.ymin * t[2]

R2.ymax = R1.ymax * t[3]

R1 * P

Multiplies a rectangle by a CPoint. For example,

R2 = R * P

is equivalent to

R2 = NewRect()

R2.xmin = R.xmin * P.x

R2.xmax = R.xmax * P.x

R2.ymin = R.ymin * P.y

R2.ymax = R.ymax * P.y

R1 * num

Multiples a rectangle by a number. For example,

R2 = R * 0.5

is equivalent to

R2 = NewRect()

R2.xmin = R.xmin * 0.5

R2.xmax = R.xmax * 0.5

R2.ymin = R.ymin * 0.5

R2.ymax = R.ymax * 0.5

Division

R1 / R2

Divides a rectangle by another rectangle. For example,

R3 = R1 / R2

is equivalent to

R3 = NewRect()

R3.xmin = R2.xmin / R1.xmin

R3.xmax = R2.xmax / R1.xmax

R3.ymin = R2.ymin / R1.ymin

R3.ymax = R2.ymax / R1.ymax

R1 / t

Divides a rectangle by 4 Lua array elements. For example,

R2 = R / t

is equivalent to

R2 = NewRect()

R2.xmin = R1.xmin / t[0]

R2.xmax = R1.xmax / t[1]

R2.ymin = R1.ymin / t[2]

R2.ymax = R1.ymax / t[3]

R1 / P

Divides a rectangle by a CPoint. For example,

R2 = R / P

is equivalent to

R2 = NewRect()

R2.xmin = R.xmin / P.x

R2.xmax = R.xmax / P.x

R2.ymin = R.ymin / P.y

R2.ymax = R.ymax / P.y

R1 / num

Divides a rectangle by a number. For example,

R2 = R / 0.5

is equivalent to

R2 = NewRect()

R2.xmin = R.xmin / 0.5

R2.xmax = R.xmax / 0.5

R2.ymin = R.ymin / 0.5

R2.ymax = R.ymax / 0.5

Modulus (Remainder)

R1 % R2

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

R3 = R1 % R2

is equivalent to

R3 = NewRect( R1 )

R3.xmin = R2.xmin % R1.xmin

R3.xmax = R2.xmax % R1.xmax

R3.ymin = R2.ymin % R1.ymin

R3.ymax = R2.ymax % R1.ymax

R1 % t

Calculates the remainder of dividing a rectangle by 4 Lua array elements. For example,

R2 = R % t

is equivalent to

R2 = NewRect()

R2.xmin = R1.xmin % t[0]

R2.xmax = R1.xmax % t[1]

R2.ymin = R1.ymin % t[2]

R2.ymax = R1.ymax % t[3]

R1 % P

Calculates the remainder of dividing a rectangle by a CPoint. For example,

R2 = R % P

is equivalent to

R2 = NewRect()

R2.xmin = R.xmin % P.x

R2.xmax = R.xmax % P.x

R2.ymin = R.ymin % P.y

R2.ymax = R.ymax % P.y

R1 % num

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

R2 = R % 0.5

is equivalent to

R2 = NewRect()

R2.xmin = R.xmin % 0.5

R2.xmax = R.xmax % 0.5

R2.ymin = R.ymin % 0.5

R2.ymax = R.ymax % 0.5

Unary Minus (Negation)

–R

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

R2 = -R

is equivalent to

R2 = NewRect()

R2.xmin = -R.xmin

R2.xmax = -R.xmax

R2.ymin = -R.ymin

R2.ymax = -R.ymax

and

-R

is equivalent to

R.xmin = -R.xmin

R.xmax = -R.xmax

R.ymin = -R.ymin

R.ymax = -R.ymax

Comparison

==

For two rectanges, R1 and R2, this operator returns true if each property of R1 is equal to the corresponding property of R2. For example,

if R2 == R1 then Func() end

is equivalent to

if R2.xmin == R1.xmin and R2.xmax == R1.xmax and

   R2.ymin == R1.ymin and R2.ymax == R1.ymax then

  Func()

end

<=

For two rectanges, R1 and R2, this operator returns true if each property of R1 is less than or equal to the corresponding property of R2. For example,

if R2 <= R1 then Func() end

is equivalent to

if R2.xmin <= R1.xmin and R2.xmax <= R1.xmax and

   R2.ymin <= R1.ymin and R2.ymax <= R1.ymax then

  Func()

end

<

For two rectanges, R1 and R2, this operator returns true if each property of R1 is less than the corresponding property of R2. For example,

if R2 < R1 then Func() end

is equivalent to

if R2.xmin < R1.xmin and R2.xmax < R1.xmax and

   R2.ymin < R1.ymin and R2.ymax < R1.ymax then

  Func()

end

Related Topics

CRect class