TRect and TPoint are utility classes for 2D geometry operations. TRect represents a
rectangle defined by its top, left, bottom, and right edges. TPoint represents a single
point with row and column (or Y and X) coordinates. Both classes support arithmetic operators
and geometric transformations commonly needed in UI layout and drawing code.
TRect
Key DATA Members
DATA
Type
Description
aRect
Array
Core data: { nTop, nLeft, nBottom, nRight }
nTop
Numeric
Top edge (accessor for aRect[1])
nLeft
Numeric
Left edge (accessor for aRect[2])
nBottom
Numeric
Bottom edge (accessor for aRect[3])
nRight
Numeric
Right edge (accessor for aRect[4])
nWidth
Numeric
Computed width
nHeight
Numeric
Computed height
cRect
Character
Binary RECT structure (8 bytes) for Win32 API calls
CenterPt
Array
Center point as { nTop, nLeft }
lWinRect
Logical
Windows-style rect (exclusive right/bottom). Default .T.
Create a new rectangle. Accepts array, binary string, or four coordinates. nWidth/nHeight override nRight/nBottom.
MoveBy( nRows, nCols )
Offset the entire rectangle by the given deltas
MoveTo( nNewTop, nNewLeft )
Move rectangle so its top-left corner is at the given position
Contains( aPoint )
Check if a point { row, col } or another rectangle is inside this one
Resize( nRatio [, aPoint] )
Resize by a ratio relative to center (or specified anchor point)
FitInside( oRect, nType, cAlign )
Fit this rectangle inside another, optionally enlarging. nType: 1=exact, 2=fill outer, 3=fit inside.
AsSquare( lOuter, cAlign )
Adjust to make a square by expanding or contracting the larger dimension
Modify( ... )
Return a new TRect with modified edges; supports absolute, fractional, and relative changes
TRect Operators
Operator
Description
==
Equality comparison (all four edges match)
+, -, *, /, %
Arithmetic on all four edges by a value or array
+=, -=, *=, /=, %=
In-place arithmetic operators
[]
Indexed access: oRect[1] = nTop, oRect[2] = nLeft, etc.
TPoint
TPoint Key DATA Members
DATA
Type
Description
nRow
Numeric
Row (Y) coordinate
nCol
Numeric
Column (X) coordinate
X
Numeric
Alias for nCol
Y
Numeric
Alias for nRow
aPt
Array
{ nRow, nCol }
aXY
Array
{ X, Y } = { nCol, nRow }
TPoint Methods
Method
Description
New( nRow, nCol )
Create from row/col; also accepts an array {row,col}
NewXY( x, y )
Create from X/Y coordinates (stored as nCol/nRow)
Rect( nRadius [, yRadius] )
Return a TRect centered at this point with the given radius
Distance( oPt )
Euclidean distance to another point
Plus( r, c [, lNew] )
Add deltas and return a new point (or modify Self if lNew is .F.)
TPoint Operators
Operator
Description
+
Create a new point offset by the given delta
+=
In-place offset
-
Return the distance between two points
Example
#include "FiveWin.ch"
function Main()
local oRect, oPt, oInner
// Create a 200x100 rectangle at position (10,10)
oRect := TRect():New( 10, 10, 110, 210 )
// Test if a point is inside
oPt := TPoint():New( 50, 100 )
if oRect:Contains( oPt:aPt )
MsgInfo( "Point is inside the rectangle" )
endif
// Create a square and fit it inside the rectangle
oInner := TRect():New( 0, 0, 100, 100 )
oInner:FitInside( oRect, 3, "C" ) // Fit inside, centered
// Move the rectangle by 10 pixels in each direction
oRect:MoveBy( 10, 10 )
return nil
Notes
TRect can be constructed from an array, from a binary RECT structure, or from four numeric coordinates.
The lWinRect flag controls whether width/height calculations use Windows convention (exclusive bottom/right edges, default .T.) or inclusive convention (.F.).
TPoint's - operator returns the Euclidean distance (a numeric value), not a new point.
Both classes provide OPERATOR overloading for natural arithmetic and indexing syntax.
Helper Funciones PointXY( x, y ) and PointRowCol( r, c ) create TPoint objects inline.