SDSU CS 596 OODP
Rectangle Class

[To Lecture Notes Index]
San Diego State University -- This page last updated Oct. 10, 1995
----------

class Rectangle {
	public:
		Rectangle( const Point& UpperLeftCorner, 
				const Point& LowerLeftCorner );
		
		void print ( ostream& output ) const;
		
		Point getUpperLeftCorner() const;
		Point getUpperRightCorner() const;
		Point getLowerLeftCorner() const;
		Point getLowerRightCorner() const;
		Point* getCorners() const;
		
		int contains( const Point& a ) const;
		int intersects( const Rectangle a ) const;
		
	private:
		Point	UpperLeft;
		int	width;
		int	height;

		int containsCornerOf( const Rectangle a ) const;
};

Rectangle :: Rectangle( const Point& UpperLeftCorner, 
			const Point& LowerLeftCorner ) {

	UpperLeft = UpperLeftCorner;
	width = LowerLeftCorner.x() - UpperLeftCorner.x();
	height = LowerLeftCorner.y() - UpperLeftCorner.y();
}

void Rectangle :: print ( ostream& output ) const {

	Point* Corners = getCorners();
	output << '['; 
	
	for (int K = 0; K < 4; K++ )
		Corners[ K ].print( output );
		
	output << "]";
}

Point Rectangle :: getUpperLeftCorner() const {
	return UpperLeft;
}

Point Rectangle :: getUpperRightCorner() const {
	Point UpperRightCorner( UpperLeft.x() + width, UpperLeft.y() );
	return UpperRightCorner;
}

Point Rectangle :: getLowerLeftCorner() const {
	Point LowerLeftCorner( UpperLeft.x() , UpperLeft.y() + height );
	return LowerLeftCorner;
}

Point Rectangle :: getLowerRightCorner() const {
	Point LowerRightCorner( UpperLeft.x()  + width , 
				UpperLeft.y() + height );
	return LowerRightCorner;
}

Point* Rectangle :: getCorners() const{
	Point* corners = new Point[4];
	
		// Store in clockwise direction from UpperLeftCorner
	corners[0] = getUpperLeftCorner();
	corners[1] = getUpperRightCorner();
	corners[2] = getLowerRightCorner();
	corners[3] = getLowerLeftCorner();
	
	return corners;
}

int Rectangle :: contains( const Point& a ) const {
	if (	( getUpperLeftCorner() <= a ) && 
		( a <= getLowerRightCorner() )
	    )
		return TRUE;
	else
		return FALSE;
};

int Rectangle :: containsCornerOf( const Rectangle a ) const {

	Point* CornersOfA = a.getCorners();
	int ContainsCorner = FALSE;	
	
	for (int K = 0; K < 4; K++ )
		if ( contains( CornersOfA[ K ] ) == TRUE )
			ContainsCorner = TRUE;
	
	delete CornersOfA;
	return ContainsCorner;
}

int Rectangle :: intersects( const Rectangle a ) const {
	
	if ( containsCornerOf( a ) == TRUE )
		return TRUE;
		
	if ( a.containsCornerOf( *this ) == TRUE )
		return TRUE;
		
	// the  case  -------
	//            |     |
	//         --------------
	//         |  |     |   |
	//         --------------
	//            |     |
	//            -------
	
	Line aTop( a.getUpperLeftCorner() , a.getUpperRightCorner() );
	Line aSide( a.getUpperLeftCorner() , a.getLowerLeftCorner() );

	Line thisTop( 	this->getUpperLeftCorner() , 
			this->getUpperRightCorner() );
	Line thisSide( 	this->getUpperLeftCorner() , 
			this->getLowerLeftCorner() );
	
	if ( aTop.intersects( thisSide ) == TRUE )
		return TRUE;
		
	if ( thisTop.intersects( aSide ) == TRUE )
		return TRUE;

	return FALSE;
};