
/*
 * Portable Object Compiler (c) 1997.  All Rights Reserved.
 * $Id$
 */

/*
 * This library is free software; you can redistribute it and/or modify it
 * under the terms of the GNU Library General Public License as published 
 * by the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Library General Public License for more details.
 *
 * You should have received a copy of the GNU Library General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 */

#include "config.h"
#include "point.h"
#include "rectangl.h"

@implementation Rectangle

/*****************************************************************************
 *
 * Creation
 *
 ****************************************************************************/


+ new
{
  return [self origin:[Point new] corner:[Point new]];
}

+ origin:p corner:q
{
  return [[super new] origin:p corner:q];
}

+ origin:p extent:q
{
  return [[super new] origin:p extent:q];
}

+ origin:(int)x1:(int)y1 corner:(int)x2:(int)y2
{
  return [[[super new] origin:x1:y1] corner:x2:y2];
}

+ origin:(int)x:(int)y extent:(int)w:(int)h
{
  return [[[super new] origin:x:y] extent:w:h];
}

- copy
{
  return [[super copy] origin:[origin copy] corner:[corner copy]];
}

- deepCopy
{
  return [[super copy] origin:[origin deepCopy] corner:[corner deepCopy]];
}

- free
{
  corner = [corner free];
  origin = [origin free];
  return [super free];
}

/*****************************************************************************
 *
 * Motion
 *
 ****************************************************************************/


- origin:p
{
  origin = p;
  return self;
}

- corner:q
{
  corner = q;
  return self;
}

- origin:p corner:q
{
  [self origin:p];
  [self corner:q];
  return self;
}

- extent:(int)w:(int)h
{
  [self corner:[Point x:[origin x] + w y:[origin y] + h]];
  return self;
}

- extent:q
{
  [self extent:[q x]:[q y]];
  return self;
}

- origin:(int)x1:(int)y1
{
  return [self origin:[Point x:x1 y:y1]];
}

- corner:(int)x1:(int)y1
{
  return [self corner:[Point x:x1 y:y1]];
}

/*****************************************************************************
 *
 * Comparing
 *
 ****************************************************************************/


- (BOOL) contains:aPoint
{
  int xLoc = [aPoint x];
  int yLoc = [aPoint y];

  return ([origin x] <= xLoc && xLoc <= [corner x])
      && ([origin y] <= yLoc && yLoc <= [corner y]);
}

- (unsigned) hash
{
  return [origin hash] ^ [corner hash];
}

- (BOOL) isEqual:aRectangle
{
  /* Stepstone isKindOf: takes id but isa is SHR */
  return [aRectangle isKindOf:(id) isa] &&
      [origin isEqual:[aRectangle origin]] &&
      [corner isEqual:[aRectangle corner]];
}

/*****************************************************************************
 *
 * Interrogation
 *
 ****************************************************************************/


- (int) left
{
  return [origin x];
}

- (int) right
{
  return [corner x];
}

- (int) top
{
  return [origin y];
}

- (int) bottom
{
  return [corner y];
}

- (int) width
{
  return [corner x] - [origin x];
}

- (int) height
{
  return [corner y] - [origin y];
}

- origin
{
  return origin;
}

- corner
{
  return corner;
}

/*****************************************************************************
 *
 * Arithmetic
 *
 ****************************************************************************/


- moveBy:aPoint
{
  [origin moveBy:aPoint];
  [corner moveBy:aPoint];
  return self;
}

/*****************************************************************************
 *
 * Printing
 *
 ****************************************************************************/

- printOn:(IOD)aFile
{
  fprintf(aFile, "(");
  [origin printOn:aFile];
  fprintf(aFile, ",");
  [corner printOn:aFile];
  fprintf(aFile, ")");
  return self;
}

@end
 
