CRect:Offset


The Offset method moves a rectangle to new coordinates. The offset can be specified in several different ways so that this method can work as a move, resize, or addition of the corresponding edge coordinates. The offsets can be positive or negative. The + Operator gives similar results when a CPoint is added.

Syntax

CRect:Offset( CRect2 )

CRect:Offset( x )

CRect:Offset( x, y )

CRect:Offset( x1, x2, y1, y2 )

where

    CRect2 is another CRect object.

    x is a single offset applied to all edges.

    x and y are offsets applied equally to parallel sides.

    x1, x2, y1, y2 are applied independently to all 4 sides.

  

The specified offset values are added to the edge coordinates of the rectangle. Using the 3 different argument lists gives great flexibility in changing the rectangle dimensions:

    Use the 1 argument form with a CRect to add the coordinates of one rectangle to another, edge by edge.

    Use the 1 argument version with a number argument to move the rectangule using equal offsets for all 4 edges.

    Use the 2 argument version to move the rectangle by offsetting opposite sides equally.

    Use the 4 argument version to resize the rectangle or to do a combination of move and resize. If the x1 and y1 arguments are both 0, the rectangle is only resized. This version works like the CRect version except that the offsets are given as numbers rather than by using another CRect.

The difference between Offset and Inflate is that Offset adds positive offsets to all 4 edges, whereas Inflate subtracts positive offsets from the minimum edges while adding positive offsets to the maximum edges.

Example

The following script moves and resizes the CRect R using various argument lists. Note that CRect:Get returns 4 arguments, which can be automatically filled into the 4 fields of the Printf function as shown.

R = NewRect(100, 200, 300, 600)

-- create a CRect object R

Printf("%lg:%lg, %lg:%lg\n", R:Get())

-- result: 100:200, 300:600.

 

 

R:Offset( 2, 10, 4, 20 )

-- move and resize the rectangle R

Printf("%lg:%lg, %lg:%lg\n", R:Get())

-- result: 102:210, 304:620.

 

 

R:Offset( 0, 30, 0, 40 )

-- resize the rectangle R

Printf("%lg:%lg, %lg:%lg\n", R:Get())

-- result: 102:240, 310:660.

 

 

R:Offset( 100, -200 )

-- move the rectangle R

Printf("%lg:%lg, %lg:%lg\n", R:Get())

-- result: 202:340, 110:460.

 

 

R:Offset( 10 )

-- move the rectangle R

Printf("%lg:%lg, %lg:%lg\n", R:Get())

-- result: 212:350, 120:470.

 

 

Rnew = NewRect( 0, 60, 0, 80 )

-- create another CRect object

R:Offset( Rnew )

-- resize R using another rectangle

Printf("%lg:%lg, %lg:%lg\n", R:Get())

-- result: 212:410, 120:550.

Related Topics

CRect Operators

Union

Intersection

Inflate

CRect class