/*	RectMath.cpp
 *
 *		Rectangle math routines
 */

/**********************************************************************

  YAAF is Copyright 1997-1999 by William Woody.

  Redistribution and use in source and binary forms, with or without 
  modification, are permitted provided that the following conditions 
  are met:

  Redistributions of source code must retain the above copyright notice, 
  this list of conditions and the following disclaimer.

  Neither the name of the copyright holder or other contributors may
  be used to promote products derived from this software without
  specific written prior permission.

  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 
  EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES 
  OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 
  NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS 
  BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN 
  ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN 
  CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 
  SOFTWARE.
  
  Contact:   woody@alumni.caltech.edu
  Web page:  http://www.pandawave.com/yaaf/

**********************************************************************/

#include <XConfig.h>			// must be first to optimize compile on MSVC++
#include <XDrawData.h>

/**@name Support--Rectangle Math Routines
 *
 *		Each platform contains it's own routines for processing rectangles.
 *	While useful, this can be a pain in the neck when it comes to writing
 *	portable software. So here is all of the basic rectangle processing
 *	routines, reproduced for Windows and X Windows, which mimic the
 *	same functionality found on the Macintosh.
 */
 
//@{

/************************************************************************/
/*																		*/
/*	Simple Rectangle Routines											*/
/*																		*/
/************************************************************************/

/*$	SetRect
 *
 *		Set the rectangle based on the four coordinates provided. This
 *	provides a fast and dirty way for initializing a rectangle.
 */

void SetRect(XRect *r, long left, long top, long right, long bottom)
{
	r->left = left;
	r->top = top;
	r->right = right;
	r->bottom = bottom;
}

/*$	OffsetRect
 *
 *		Offset the rectangle by the delta values (x,y). These values
 *	are added to the horizontal and vertical coordinates of the rectangle.
 */

#if !OPT_WINOS
void OffsetRect(XRect *r, long x, long y)
{
	r->left += x;
	r->right += x;
	r->top += y;
	r->bottom += y;
}
#endif

/*$	InsetRect
 *
 *		Inset the rectangle by the delta values provided. If the
 *	values are negative, this expands the rectangle instead. If the
 *	rectangle becomes empty, this sets the rectangle to (0,0,0,0).
 */

void InsetRect(XRect *r, long x, long y)
{
	r->left += x;
	r->right -= x;
	r->top += y;
	r->bottom -= y;
	if (EmptyRect(r)) SetRect(r,0,0,0,0);
}

/************************************************************************/
/*																		*/
/*	Rectangle Manipulation												*/
/*																		*/
/************************************************************************/

/*$	EmptyRect
 *
 *		Return true if the rectangle is empty. A rectangle is empty if
 *	either the left coordinate is equal to the right, or if the top
 *	coordinate is equal to the bottom coordinate.
 */

bool EmptyRect(XRect *r)
{
	return ((r->right <= r->left) || (r->bottom <= r->top));
}

/*$	EqualRect
 *
 *		Return true if rectangles are equal. This works even if the
 *	two rectangles are empty, but have different coordinates representing
 *	the "empty" value.
 */

bool EqualRect(XRect *a, XRect *b)
{
	if (EmptyRect(a) && EmptyRect(b)) return true;
	
	if (a->left != b->left) return false;
	if (a->top != b->top) return false;
	if (a->right != b->right) return false;
	if (a->bottom != b->bottom) return false;
	return true;
}

/************************************************************************/
/*																		*/
/*	Rectangle Math														*/
/*																		*/
/************************************************************************/

/*	lmin
 *
 *		short integer minimum
 */

static long lmin(long x, long y)
{
	return (x < y) ? x : y;
}

/*	lmax
 *
 *		short integer maximum
 */

static long lmax(long x, long y)
{
	return (x > y) ? x : y;
}

/*$	SectRect
 *
 *		Intersect two rectangles. If empty, return false. Like the
 *	Macintosh, #out# may be the same rectangle as #a# or #b#.
 */

bool SectRect(XRect *a, XRect *b, XRect *out)
{
	out->left = lmax(a->left,b->left);
	out->right = lmin(a->right,b->right);
	out->top = lmax(a->top,b->top);
	out->bottom = lmin(a->bottom,b->bottom);
	
	if (EmptyRect(out)) {
		SetRect(out,0,0,0,0);
		return false;
	} else {
		return true;
	}
}

/*$	UnionRect
 *
 *		Find the largest enclosing rectangle. Note that like the
 *	Macintosh, #out# may be the same rectangle as #a# or #b#.
 */

void UnionRect(XRect *a, XRect *b, XRect *out)
{
	if (EmptyRect(a)) {
		*out = *b;
	} else if (EmptyRect(b)) {
		*out = *a;
	} else {
		out->left = lmin(a->left,b->left);
		out->right = lmax(a->right,b->right);
		out->top = lmin(a->top,b->top);
		out->bottom = lmax(a->bottom,b->bottom);
	}
}

/************************************************************************/
/*																		*/
/*	XPoint/Rectangle Stuff												*/
/*																		*/
/************************************************************************/

/*$	PtInRect
 *
 *		Is this point inside this rectangle? Returns 'true' if the
 *	point provided is inside the rectangle, and 'false' if it is not.
 */

bool PtInRect(XPoint pt, XRect *r)
{
	return ((r->left <= pt.h) && (pt.h < r->right) &&
				(r->top <= pt.v) && (pt.v < r->bottom));
}

/*$	Pt2Rect
 *
 *		Create rectangle which contains these two points.
 */

void Pt2Rect(XPoint a, XPoint b, XRect *r)
{
	r->left = lmin(a.h,b.h);
	r->right = lmax(a.h,b.h);
	r->top = lmin(a.v,b.v);
	r->bottom = lmax(a.v,b.v);
}

//@}

