StanfordCPPLib
Public Member Functions | List of all members
GRectangle Class Reference

This class contains real-valued x, y, width, and height fields. More...

#include "gtypes.h"

Public Member Functions

 GRectangle(double x=0, double y=0, double width=0, double height=0)
 Creates a GRectangle object with the specified position and size. More...
 
 GRectangle(double x, double y, const GDimension &size)
 Creates a GRectangle object with the specified position and size. More...
 
 GRectangle(const GPoint &p, double width=0, double height=0)
 Creates a GRectangle object with the specified position and size. More...
 
 GRectangle(const GPoint &p, const GDimension &size)
 Creates a GRectangle object with the specified position and size. More...
 
bool contains(const GPoint &pt) const
 Returns true if the rectangle contains the given point, which may be specified either as a point or as distinct coordinates. More...
 
bool contains(double x, double y) const
 
GRectangle enlargedBy(double amount)
 Returns a new rectangle with its boundaries shifted outward by the given amount on all 4 sides. More...
 
double getHeight() const
 Returns the height component of the rectangle. More...
 
double getWidth() const
 Returns the width component of the rectangle. More...
 
double getX() const
 Returns the x component of the rectangle. More...
 
double getY() const
 Returns the y component of the rectangle. More...
 
bool isEmpty() const
 Returns true if the rectangle is empty, meaning that it has a width and height that are both 0 or negative. More...
 
string toString() const
 Converts the GRectangle to a string in the form "(</code><i>x</i><code>,</code>&nbsp;<i>y</i><code>,</code> <i>width</i><code>,</code>&nbsp;<i>height</i><code>)". More...
 

Detailed Description

This class contains real-valued x, y, width, and height fields.

It is used to represent the bounding box of a graphical object.

Constructor & Destructor Documentation

◆ GRectangle() [1/4]

GRectangle ( double  x = 0,
double  y = 0,
double  width = 0,
double  height = 0 
)

Creates a GRectangle object with the specified position and size.

If these parameters are not supplied, the constructor sets these fields to 0.

◆ GRectangle() [2/4]

GRectangle ( double  x,
double  y,
const GDimension size 
)

Creates a GRectangle object with the specified position and size.

If these parameters are not supplied, the constructor sets these fields to 0.

◆ GRectangle() [3/4]

GRectangle ( const GPoint p,
double  width = 0,
double  height = 0 
)

Creates a GRectangle object with the specified position and size.

If these parameters are not supplied, the constructor sets these fields to 0.

◆ GRectangle() [4/4]

GRectangle ( const GPoint p,
const GDimension size 
)

Creates a GRectangle object with the specified position and size.

If these parameters are not supplied, the constructor sets these fields to 0.

Member Function Documentation

◆ contains() [1/2]

bool contains ( const GPoint pt) const

Returns true if the rectangle contains the given point, which may be specified either as a point or as distinct coordinates.

◆ contains() [2/2]

bool contains ( double  x,
double  y 
) const

◆ enlargedBy()

GRectangle enlargedBy ( double  amount)

Returns a new rectangle with its boundaries shifted outward by the given amount on all 4 sides.

e.g. a 10x10 rectangle at position (55, 42) enlarged by 1 will become a 12x12 rectangle at position (54, 41).

◆ getHeight()

double getHeight ( ) const

Returns the height component of the rectangle.

◆ getWidth()

double getWidth ( ) const

Returns the width component of the rectangle.

◆ getX()

double getX ( ) const

Returns the x component of the rectangle.

◆ getY()

double getY ( ) const

Returns the y component of the rectangle.

◆ isEmpty()

bool isEmpty ( ) const

Returns true if the rectangle is empty, meaning that it has a width and height that are both 0 or negative.

◆ toString()

string toString ( ) const

Converts the GRectangle to a string in the form "(</code><i>x</i><code>,</code>&nbsp;<i>y</i><code>,</code> <i>width</i><code>,</code>&nbsp;<i>height</i><code>)".

Friends And Related Function Documentation

◆ hashCode

int hashCode ( const GRectangle r)
friend

Hashing function for GRectangle objects.

◆ operator!=

bool operator!= ( const GRectangle r1,
const GRectangle r2 
)
friend

Compares two rectangles for inequality.

◆ operator<

bool operator< ( const GRectangle r1,
const GRectangle r2 
)
friend

Relational operators that compare rectangles by x, y, then width, then height.

◆ operator<<

std::ostream& operator<< ( std::ostream &  os,
const GRectangle rect 
)
friend

Writes the given rectangle to the given output stream.

◆ operator<=

bool operator<= ( const GRectangle r1,
const GRectangle r2 
)
friend

Relational operators that compare rectangles by x, y, then width, then height.

◆ operator==

bool operator== ( const GRectangle r1,
const GRectangle r2 
)
friend

Compares two rectangles for equality.

◆ operator>

bool operator> ( const GRectangle r1,
const GRectangle r2 
)
friend

Relational operators that compare rectangles by x, y, then width, then height.

◆ operator>=

bool operator>= ( const GRectangle r1,
const GRectangle r2 
)
friend

Relational operators that compare rectangles by x, y, then width, then height.