Rect Class Reference

This class is used to represent a rectangular region. It uses a union to enable the client to access the rectangle as two Coords (lo and hi) or as four INT32s (lox, loy, hix, hiy). An anonymous union is used, which is a defined C++ feature and so portable. More...

#include <rect.h>

Inheritance diagram for Rect:

OilRect List of all members.

Public Member Functions

 Rect ()
 To construct an empty Rect.
 Rect (INT32 LowX, INT32 LowY, INT32 HighX, INT32 HighY)
 To construct a Rect with an inclusive lower left hand corner position of (Left, Lower) and an exclusive upper right hand corner position of (Right, Upper).
 Rect (const Coord &Low, UINT32 Width, UINT32 Height)
 To construct a rectangle with an inclusive lower left hand corner position of Low and a width and height as specified.
 Rect (const Coord &Low, const Coord &High)
 To construct a rectangle with an inclusive lower left hand corner position of Low and an exclusive upper right hand corner position of High.
 Rect (const Rect &Rect)
 Copy constructor.
Rectoperator= (const Rect &Rect)
 Equals operator.
INT32 Width () const
 To find the width of the Rect.
INT32 Height () const
 To find the height of the Rect.
Coord LowCorner () const
 To find the lower left hand coordinates of the Rect.
Coord HighCorner () const
 To find the upper right hand coordinates of the Rect.
Coord Centre () const
 To find the centre of the Rect It calculates Coord(lox+(width/2),loy+(height/2)).
BOOL IsIntersectedWith (const Rect &) const
 To check for rectangle intersection.
Rect Intersection (const Rect &) const
Rect Union (const Rect &) const
INT32 SplitRect (const Rect &R, Rect *SubRects)
BOOL ContainsCoord (const Coord &) const
 To check for coordinate containment.
BOOL ContainsRectCoord (const Coord &) const
 To check for coordinate containment. This will work for coordinates which have been extracted from other rectangles (i.e. the top right corner is considered inclusive for this operation, not exclusive).
BOOL ContainsRect (const Rect &) const
 To check for rectangle containment.
BOOL IsAdjacent (const Rect &, MILLIPOINT Fuzzy) const
void MakeEmpty ()
 Make the rectangle an empty one (all coordinates are set to 0).
BOOL IsEmpty () const
 To check for empty rectangle.
BOOL IsValid () const
 To check for a valid rectangle.
void Inflate (INT32 XInc, INT32 YInc)
 Inflate a rectangle by given amounts. Negative values will deflate the rectangle.
void Inflate (INT32 XInc)
 Inflate a rectangle by given amount. Negative values will deflate the rectangle.
void Translate (INT32 XOfs, INT32 YOfs)
 Translate a rectangle by given offset.
void IncludePoint (const Coord &)
INT32 operator== (const Rect &) const
 Test for equality of two rectangles. As all invalid rectangles have the same results when used for Union/Intersection, any two invalid rectangles are considered equal.
INT32 operator!= (const Rect &) const
 Test for inequality of two rectangles. As all invalid rectangles have the same results when used for Union/Intersection, any two invalid rectangles are considered equal.

Public Attributes

Coord lo
Coord hi

Detailed Description

This class is used to represent a rectangular region. It uses a union to enable the client to access the rectangle as two Coords (lo and hi) or as four INT32s (lox, loy, hix, hiy). An anonymous union is used, which is a defined C++ feature and so portable.

Author:
Simon_Maneggio (Xara Group Ltd) <camelotdev@xara.com> Date: 11/5/93
The lo coordinates are inclusive, whilst the hi coordinates are exclusive. In the document space, lo.y <= hi.y and lo.x <= hi.x (the inequalities are not strict because rectangles can have zero width and/or height - such rectangles are considered perfectly valid).

The inclusivity can be explained as follows:

A point at (lo.x, lo.y) is inside the rectangle A point at (hi.x, hi.y) is outside the rectangle

Any rectangle that does not obey the inequalites lo.y <= hi.y and lo.x <= hi.x is deemed to be 'invalid'. Invalid rectangles have special properties. They will cause errors when used in some operations, but not in others. The general rule is that if the rectangle is used as an entity, then the operation will succeed (taking account of the 'invalidity' of the rectangle). If, however, the actual coordinates of the invalid rectangle must be used/changed in the operation, then an error (usually an assertion failure) will occur.

For example, using invalid rectangles in Unions and Intersections is ok, because the invalid rectangle is ignored, and the 'other' rectangle is returned as the result. This means that if both rectangles are invalid, then an invalid rectangle is returned.

Conversely, trying to use operations like Translate(), Inflate(), Width() or Height() on invalid rectangles is considered to be an error, and will cause an assertion failure.

See the individual function descriptions for more details.

Returns:
Errors: -
See also:
Coord

Rect

DocRect

WorkRect

OSRect

Definition at line 159 of file rect.h.


Constructor & Destructor Documentation

Rect::Rect  )  [inline]
 

To construct an empty Rect.

Author:
Simon_Maneggio (Xara Group Ltd) <camelotdev@xara.com>
Date:
13/5/93
Parameters:
None [INPUTS]
- [OUTPUTS]
Returns:
-

Errors:

Definition at line 398 of file rect.h.

00399 {
00400     // An empty rectangle
00401     hi.x = hi.y = lo.x = lo.y = 0;
00402 }

Rect::Rect INT32  LowX,
INT32  LowY,
INT32  HighX,
INT32  HighY
[inline]
 

To construct a Rect with an inclusive lower left hand corner position of (Left, Lower) and an exclusive upper right hand corner position of (Right, Upper).

Author:
Simon_Maneggio (Xara Group Ltd) <camelotdev@xara.com>
Date:
13/5/93
Parameters:
LowX : Lower X coord of rectangle (inclusive) [INPUTS] HighX: Higher X coord of rectangle (exclusive) LowY : Lower Y coord of rectangle (inclusive) HighY: Higher Y coord of rectangle (exclusive)
- [OUTPUTS]
Returns:
-

Errors: An assertion failure will occur if the lower left hand coordinates are not lower than and to the left of the upper right coordinate.

Definition at line 429 of file rect.h.

00430 {                               
00431     // Defensive programming, detect an invalid rectangle
00432     ENSURE((LowX <= HighX) && (LowY <= HighY),
00433            "Rect::Rect(INT32, INT32, INT32, INT32) was\n passed invalid coordinates");  
00434     
00435     lo.x = LowX;
00436     lo.y = LowY; 
00437     
00438     hi.x = HighX; 
00439     hi.y = HighY;   
00440 } 

Rect::Rect const Coord Low,
UINT32  Width,
UINT32  Height
[inline]
 

To construct a rectangle with an inclusive lower left hand corner position of Low and a width and height as specified.

Author:
Tim_Browse (Xara Group Ltd) <camelotdev@xara.com>
Date:
17/5/93
Parameters:
Low,: Coordinates of the inclusive lower left hand corner. [INPUTS] Width, Height : Desired dimensions of the rectangle.
- [OUTPUTS]
Returns:
-

Errors: None.

Definition at line 494 of file rect.h.

00495 {         
00496     lo = Low;
00497     
00498     hi.x = lo.x + Width;
00499     hi.y = lo.y + Height;
00500 }       

Rect::Rect const Coord Low,
const Coord High
[inline]
 

To construct a rectangle with an inclusive lower left hand corner position of Low and an exclusive upper right hand corner position of High.

Author:
Simon_Maneggio (Xara Group Ltd) <camelotdev@xara.com>
Date:
13/5/93
Parameters:
Low : Coordinates of the lower left hand corner (inclusive) [INPUTS] High: Coordinates of the upper right hand corner (exclusive)
- [OUTPUTS]
Returns:
-

Errors: An assertion failure will occur if the lower left hand coordinates are not lower than and to the left of the upper right coordinates.

Definition at line 464 of file rect.h.

00465 {         
00466     // Defensive programming, detect an invalid rectangle
00467     ENSURE((Low.x <= High.x) && (Low.y <= High.y),
00468            "Rect::Rect(Coord, Coord) was\n passed invalid coordinates");  
00469     
00470     lo = Low;
00471     hi = High;
00472 }       

Rect::Rect const Rect R  )  [inline]
 

Copy constructor.

Author:
Simon_Maneggio (Xara Group Ltd) <camelotdev@xara.com>
Date:
13/5/93
Parameters:
R,: The copy of the Rect [INPUTS]
- [OUTPUTS]
Returns:
-

Definition at line 519 of file rect.h.

00520 {
00521     lo = R.lo;
00522     hi = R.hi;
00523 }


Member Function Documentation

Coord Rect::Centre  )  const [inline]
 

To find the centre of the Rect It calculates Coord(lox+(width/2),loy+(height/2)).

Author:
Mark_Neves (Xara Group Ltd) <camelotdev@xara.com>
Date:
19/5/99
Parameters:
- [INPUTS]
- [OUTPUTS]
Returns:
The centre coord of this Rect

Errors: Assertion failure if the rectangle is invalid.

Definition at line 667 of file rect.h.

00668 {     
00669     // Detect an invalid rectangle
00670     ENSURE(IsValid(), "Rect::Centre() was called on\nan invalid rectangle.");  
00671 
00672     return Coord(lo.x + Width() / 2, lo.y + Height() / 2); 
00673 }        

BOOL Rect::ContainsCoord const Coord Point  )  const [inline]
 

To check for coordinate containment.

Author:
Tim_Browse (Xara Group Ltd) <camelotdev@xara.com>
Date:
17/5/93
Parameters:
- [INPUTS]
- [OUTPUTS]
Returns:
TRUE if the coordinate is within the rectangle, FALSE otherwise.

Errors: Assertion failure if the rectangle is invalid.

See also:
ContainsRectCoord; ContainsRect

Definition at line 725 of file rect.h.

00726 {
00727     // Check for an an empty rectangle
00728     if (IsEmpty())
00729         return FALSE;
00730 
00731     // Detect an invalid rectangle
00732     ENSURE(IsValid(), "Rect::ContainsCoord() was called on\nan invalid rectangle.");  
00733 
00734     return ((Point.x >= lo.x) && (Point.x < hi.x) &&
00735             (Point.y >= lo.y) && (Point.y < hi.y));
00736 }

BOOL Rect::ContainsRect const Rect Rect  )  const [inline]
 

To check for rectangle containment.

Author:
Tim_Browse (Xara Group Ltd) <camelotdev@xara.com>
Date:
17/5/93
Parameters:
- [INPUTS]
- [OUTPUTS]
Returns:
TRUE if the rectangle 'Rect' is within the rectangle, FALSE otherwise.

Errors: Assertion failure if the rectangle is invalid.

See also:
ContainsRectCoord; ContainsCoord

Definition at line 795 of file rect.h.

00796 {
00797     // Check for an an empty rectangle
00798     if (IsEmpty())
00799         return FALSE;
00800 
00801     // Detect an invalid rectangle
00802     ENSURE(IsValid(), "Rect::ContainsRect() was called on\nan invalid rectangle.");  
00803 
00804     return ((Rect.lo.x >= lo.x) && (Rect.hi.x <= hi.x) &&
00805             (Rect.lo.y >= lo.y) && (Rect.hi.y <= hi.y));
00806 }

BOOL Rect::ContainsRectCoord const Coord Point  )  const [inline]
 

To check for coordinate containment. This will work for coordinates which have been extracted from other rectangles (i.e. the top right corner is considered inclusive for this operation, not exclusive).

Author:
Tim_Browse (Xara Group Ltd) <camelotdev@xara.com>
Date:
17/5/93
Parameters:
- [INPUTS]
- [OUTPUTS]
Returns:
TRUE if the coordinate is within the rectangle, FALSE otherwise.

Errors: Assertion failure if the rectangle is invalid.

See also:
ContainsCoord; ContainsRect

Definition at line 761 of file rect.h.

00762 {
00763     // Check for an an empty rectangle
00764     if (IsEmpty())
00765         return FALSE;
00766 
00767     // Detect an invalid rectangle
00768     ENSURE(IsValid(), "Rect::ContainsRectCoord() was called on\nan invalid rectangle.");  
00769 
00770     return ((Point.x >= lo.x) && (Point.x <= hi.x) &&
00771             (Point.y >= lo.y) && (Point.y <= hi.y));
00772 }

INT32 Rect::Height  )  const [inline]
 

To find the height of the Rect.

Author:
Simon_Maneggio (Xara Group Ltd) <camelotdev@xara.com>
Date:
13/5/93
Parameters:
- [INPUTS]
- [OUTPUTS]
Returns:
The height of the Rect

Definition at line 589 of file rect.h.

00590 {                      
00591     // Detect an invalid rectangle
00592     ENSURE(IsValid(), "Rect::Height() was called on\nan invalid rectangle.");  
00593 
00594     return(hi.y - lo.y); 
00595 }

Coord Rect::HighCorner  )  const [inline]
 

To find the upper right hand coordinates of the Rect.

Author:
Simon_Maneggio (Xara Group Ltd) <camelotdev@xara.com>
Date:
13/5/93
Parameters:
- [INPUTS]
- [OUTPUTS]
Returns:
The exclusive upper right hand coordinates of the Rect

Errors: Assertion failure if the rectangle is invalid.

Definition at line 640 of file rect.h.

00641 {      
00642     // Detect an invalid rectangle
00643     ENSURE(IsValid(), "Rect::HighCorner() was called on\nan invalid rectangle.");  
00644 
00645     return(hi); 
00646 }                  

void Rect::IncludePoint const Coord  ) 
 

void Rect::Inflate INT32  Inc  )  [inline]
 

Inflate a rectangle by given amount. Negative values will deflate the rectangle.

Author:
Tim_Browse (Xara Group Ltd) <camelotdev@xara.com>
Date:
17/5/93
Parameters:
Inc - the amount to inflate (or deflate) the rectangle by. [INPUTS]
- [OUTPUTS]
Returns:
N/A.

Errors: An assertion failure if the rectangle is invalid.

Definition at line 367 of file rect.h.

00368 {
00369     // Detect an invalid rectangle
00370     ENSURE(IsValid(), "Rect::Inflate(INT32) was called on an \ninvalid rectangle.");  
00371 
00372     lo.x -= Inc;
00373     lo.y -= Inc;
00374     
00375     hi.x += Inc;
00376     hi.y += Inc;
00377 }

void Rect::Inflate INT32  XInc,
INT32  YInc
[inline]
 

Inflate a rectangle by given amounts. Negative values will deflate the rectangle.

Author:
Tim_Browse (Xara Group Ltd) <camelotdev@xara.com>
Date:
17/5/93
Parameters:
XInc,YInc - the amount to inflate (or deflate) the rectangle by. [INPUTS]
- [OUTPUTS]
Returns:
N/A.

Errors: An assertion failure if the rectangle is invalid.

Definition at line 335 of file rect.h.

00336 {
00337     // Detect an invalid rectangle
00338     ENSURE(IsValid(), "Rect::Inflate(INT32, INT32) was called on an \ninvalid rectangle.");  
00339 
00340     lo.x -= XInc;
00341     lo.y -= YInc;
00342     
00343     hi.x += XInc;
00344     hi.y += YInc;
00345 }

Rect Rect::Intersection const Rect  )  const
 

BOOL Rect::IsAdjacent const Rect ,
MILLIPOINT  Fuzzy
const
 

BOOL Rect::IsEmpty  )  const [inline]
 

To check for empty rectangle.

Author:
Tim_Browse (Xara Group Ltd) <camelotdev@xara.com>
Date:
17/5/93
Parameters:
- [INPUTS]
- [OUTPUTS]
Returns:
TRUE if the rectangle is empty.

Errors:

Definition at line 845 of file rect.h.

00846 {
00847     return ((lo.x == hi.x) || (lo.y == hi.y));
00848 }

BOOL Rect::IsIntersectedWith const Rect R  )  const [inline]
 

To check for rectangle intersection.

Author:
Tim_Browse (Xara Group Ltd) <camelotdev@xara.com>
Date:
17/5/93
Parameters:
- [INPUTS]
- [OUTPUTS]
Returns:
TRUE if the rectangles intersect, FALSE otherwise.

Errors:

Definition at line 694 of file rect.h.

00695 {
00696     // Detect an invalid rectangle
00697     if ((!IsValid()) || (!R.IsValid()))
00698         return FALSE;
00699         
00700     return ((hi.x > R.lo.x) && (lo.x < R.hi.x) &&
00701             (hi.y > R.lo.y) && (lo.y < R.hi.y));
00702 }

BOOL Rect::IsValid  )  const [inline]
 

To check for a valid rectangle.

Author:
Tim_Browse (Xara Group Ltd) <camelotdev@xara.com>
Date:
9/6/93
Parameters:
- [INPUTS]
- [OUTPUTS]
Returns:
TRUE if the rectangle is valid.

Errors:

Definition at line 869 of file rect.h.

00870 {
00871     return ((lo.x <= hi.x) && (lo.y <= hi.y));
00872 }

Coord Rect::LowCorner  )  const [inline]
 

To find the lower left hand coordinates of the Rect.

Returns:
Errors: Assertion failure if the rectangle is invalid.

Definition at line 614 of file rect.h.

00615 {     
00616     // Detect an invalid rectangle
00617     ENSURE(IsValid(), "Rect::LowCorner() was called on\nan invalid rectangle.");  
00618 
00619     return(lo); 
00620 }        

void Rect::MakeEmpty  )  [inline]
 

Make the rectangle an empty one (all coordinates are set to 0).

Author:
Tim_Browse (Xara Group Ltd) <camelotdev@xara.com>
Date:
03/03/94
See also:
Rect::MakeEmpty

Definition at line 821 of file rect.h.

00822 {
00823   lo.x = lo.y = hi.x = hi.y = 0;
00824 }

INT32 Rect::operator!= const Rect R  )  const [inline]
 

Test for inequality of two rectangles. As all invalid rectangles have the same results when used for Union/Intersection, any two invalid rectangles are considered equal.

Author:
Tim_Browse (Xara Group Ltd) <camelotdev@xara.com>
Date:
17/5/93
Parameters:
R - the rectangle to compare against. [INPUTS]
- [OUTPUTS]
Returns:
TRUE if R does not describe the same rectangle as the object.
Friend: Rect

Returns:
Errors: None.

Definition at line 305 of file rect.h.

00306 {
00307     // Invalid rectangles are equal
00308     if ((!IsValid()) && (!R.IsValid()))
00309         return FALSE;
00310         
00311     return ((lo.x != R.lo.x) || (lo.y != R.lo.y) ||
00312             (hi.x != R.hi.x) || (hi.y != R.hi.y));
00313 }

Rect & Rect::operator= const Rect Rect  )  [inline]
 

Equals operator.

Author:
Simon_Maneggio (Xara Group Ltd) <camelotdev@xara.com>
Date:
13/5/93
Parameters:
Rect,: Rect to copy [INPUTS]
- [OUTPUTS]
Returns:
Reference to this Rect

Definition at line 541 of file rect.h.

00542 {                                                    
00543     lo = Rect.lo; 
00544     hi = Rect.hi;
00545     
00546     return *this; 
00547 }

INT32 Rect::operator== const Rect R  )  const [inline]
 

Test for equality of two rectangles. As all invalid rectangles have the same results when used for Union/Intersection, any two invalid rectangles are considered equal.

Author:
Tim_Browse (Xara Group Ltd) <camelotdev@xara.com>
Date:
17/5/93
Parameters:
R - the rectangle to compare against. [INPUTS]
- [OUTPUTS]
Returns:
TRUE if R is describes the same rectangle as the object.
Friend: Rect

Returns:
Errors: None.

Definition at line 270 of file rect.h.

00271 {
00272     // Invalid rectangles are equal
00273     if ((!IsValid()) && (!R.IsValid()))
00274         return TRUE;
00275         
00276     // Could use structure compare? Would it be portable? Probably not...
00277     
00278     return ((lo.x == R.lo.x) && (lo.y == R.lo.y) &&
00279             (hi.x == R.hi.x) && (hi.y == R.hi.y));
00280 }

INT32 Rect::SplitRect const Rect R,
Rect SubRects
 

void Rect::Translate INT32  XOfs,
INT32  YOfs
[inline]
 

Translate a rectangle by given offset.

Author:
Tim_Browse (Xara Group Ltd) <camelotdev@xara.com>
Date:
17/5/93
Parameters:
(XOfs,YOfs) - the offset to translate the rectangle by. [INPUTS]
- [OUTPUTS]
Returns:
N/A.

Errors: An assertion failure if the rectangle is invalid.

Definition at line 235 of file rect.h.

00236 {
00237     // Detect an invalid rectangle
00238     ENSURE(IsValid(), "Rect::Translate() was called on an \ninvalid rectangle.");  
00239 
00240     lo.x += XOfs;
00241     lo.y += YOfs;
00242     
00243     hi.x += XOfs;
00244     hi.y += YOfs;
00245 }

Rect Rect::Union const Rect  )  const
 

INT32 Rect::Width  )  const [inline]
 

To find the width of the Rect.

Author:
Simon_Maneggio (Xara Group Ltd) <camelotdev@xara.com>
Date:
13/5/93
Parameters:
- [INPUTS]
- [OUTPUTS]
Returns:
The width of the Rect

Definition at line 565 of file rect.h.

00566 {
00567     // Detect an invalid rectangle
00568     ENSURE(IsValid(), "Rect::Width() was called on\nan invalid rectangle.");  
00569 
00570     return (hi.x - lo.x);   
00571 }       


Member Data Documentation

Coord Rect::hi
 

Definition at line 164 of file rect.h.

Coord Rect::lo
 

Definition at line 163 of file rect.h.


The documentation for this class was generated from the following file:
Generated on Sat Nov 10 04:00:22 2007 for Camelot by  doxygen 1.4.4