TRect / TPoint

Fonte: source/classes/rect.prg

Standalone classes (no inheritance)

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

DATATypeDescription
aRectArrayCore data: { nTop, nLeft, nBottom, nRight }
nTopNumericTop edge (accessor for aRect[1])
nLeftNumericLeft edge (accessor for aRect[2])
nBottomNumericBottom edge (accessor for aRect[3])
nRightNumericRight edge (accessor for aRect[4])
nWidthNumericComputed width
nHeightNumericComputed height
cRectCharacterBinary RECT structure (8 bytes) for Win32 API calls
CenterPtArrayCenter point as { nTop, nLeft }
lWinRectLogicalWindows-style rect (exclusive right/bottom). Default .T.

TRect Methods

MethodDescription
New( nTop, nLeft, nBottom, nRight [, nWidth, nHeight] )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

OperatorDescription
==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

DATATypeDescription
nRowNumericRow (Y) coordinate
nColNumericColumn (X) coordinate
XNumericAlias for nCol
YNumericAlias for nRow
aPtArray{ nRow, nCol }
aXYArray{ X, Y } = { nCol, nRow }

TPoint Methods

MethodDescription
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

OperatorDescription
+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

Veja Também