edu.mit.sketch.geom
Class Rectangle

java.lang.Object
  |
  +--edu.mit.sketch.geom.Rectangle
All Implemented Interfaces:
GeometricObject, Paintable, Rotatable, Serializable, Terminal, Translatable
Direct Known Subclasses:
Shade, Spring

public class Rectangle
extends Object
implements GeometricObject, Rotatable, Serializable

This class represents a Rectangle described by its width and height vectors. This class also implements Rotatable. This means it has a getAngle() method. The angle that is returned is the angle between the width vector and the x axis. This angle is always equal to or greater than 0 and strictly less than Math.PI/2.

See Also:
Serialized Form

Field Summary
 Graphics graphics
          Graphics context for this Geometric object.
 long time_stamp
          Time stamp of this object.
 double x
          The x coordinate of the position of width vector.
 double y
          The y coordinate of the position of width vector
 
Fields inherited from interface edu.mit.sketch.geom.GeometricObject
CIRCLE, ELLIPSE, LINE, POINT, POLYGON, RECTANGLE, SQUARE
 
Constructor Summary
Rectangle(double x, double y, double width_x, double width_y, double height)
          The constructor.
Rectangle(double x, double y, Point width_vector, double height)
          The constructor.
Rectangle(int x, int y, int width, int height)
          The constructor.
Rectangle(Point upper_left_point, Dimension dimension)
          The constructor.
Rectangle(Point upper_left_point, Dimension dimension, double angle)
          The constructor.
Rectangle(Rectangle rectangle)
          The constructor.
Rectangle(Rectangle r)
          The constructor.
 
Method Summary
 double area()
          Return area.
 boolean containsGeometricObject(GeometricObject object)
          Returns false if the argument is not completely inside this object.
 boolean containsGeometricObjects(GeometricObject[] objects)
          Returns false if the objects in the input array are completely inside this object.
 double getAngle()
          Returns the angle of the object WRT the x axis in radians.
 Point getCenter()
          Returns the center of this rectangle.
 Polygon getDataPoints()
          This method is used to get the original data points that forms this GeometricObject
 double getHeight()
          Returns the height
 double getMajorAxisAngle()
          Returns the angle between the longer of width or height vectors and the x axis.
 int getMajorAxisDirection()
          Returns the direction of the major axis of this Rectangle Known eksik: Should be tested.
 double getMajorAxisLength()
          Returns the larger of width and height.
 double getMinorAxisAngle()
          Returns the angle between the shorter of width or height vectors and the x axis.
 double getMinorAxisLength()
          Returns the smaller of width and height.
 Polygon getPolygonalBounds()
          This method should return a polygon that corresponds to this object.
 Rectangle getRectangularBounds()
          Supplied for completeness.
 long getTimeStamp()
          Returns the time stamp of the current Terminal
 String getType()
          Implement GeometricObject
 double getWidth()
          Returns the width
 boolean hasOnMajorAxisDirection(GeometricObject object)
          This method should return true if the input object lies in the major axis direction of this Rectangle.
static boolean isRectangle(Polygon p)
          Returns true if the input polygon looks like a Rectangle Can be optimized by eliminating Polygon conversions.
 void paint()
          Draw the object
 void paint(Graphics g)
          Draw the object
 void paint(Graphics g, int thickness)
          Draw the object
 void paintOriginal(Graphics g)
          This method is used to paint the original data points that forms this GeometricObject
 boolean pointIsOn(Point point, int radius)
          Returns true if the point is within +-radius distance from the curve defining the object.
 boolean pointIsOnOriginal(Point p, int radius)
          Returns true if the point is within +-radius distance from the original curve defining the object.
 void rotateAbout(Point p, double theta)
          Rotates the rectangle about the point p theta radians CCW.
 void scaleAboutCenter(double scale)
          scaleAboutCenter
 void setDataPoints(Polygon points)
          This method is used to set the original data points that forms this GeometricObject
 void setGraphicsContext(Graphics g)
          Set graphics context for this Geometric object.
 void setRectangle(Rectangle r)
          setRectangle
 void setTimeStamp(long time_stamp)
          Sets the time stamp of the current Terminal
 void shade(Graphics g, int number_of_shades)
          Shade the object
 void shade(Graphics g, int shades, int thickness, int offset)
          Shade the object
 int spatialRelation(GeometricObject object)
          This method should return the spatial relation of the input parameter with respect to this object.
 int spatialRelation(GeometricObject object, double scale)
          This method should return the spatial relation of the input parameter with respect to this object.
 int spatialRelationCartesian(GeometricObject object)
          This method should return the spatial relation of the input parameter with respect to this object.
 String toString()
          Override toString
 boolean touches(GeometricObject object)
          This method should return true if the input objects touch.
 void translate(double x, double y)
          Add the arguments to the (x, y) position of the object.
 Rectangle union(Rectangle rectangle)
          Return the rectangularBounds of the union of this rectangle and the argument.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Field Detail

x

public double x
The x coordinate of the position of width vector.

y

public double y
The y coordinate of the position of width vector

time_stamp

public long time_stamp
Time stamp of this object.

graphics

public transient Graphics graphics
Graphics context for this Geometric object.
Constructor Detail

Rectangle

public Rectangle(Rectangle rectangle)
The constructor.

Rectangle

public Rectangle(Point upper_left_point,
                 Dimension dimension)
The constructor.

Rectangle

public Rectangle(Point upper_left_point,
                 Dimension dimension,
                 double angle)
The constructor.

Rectangle

public Rectangle(double x,
                 double y,
                 Point width_vector,
                 double height)
The constructor.

Rectangle

public Rectangle(double x,
                 double y,
                 double width_x,
                 double width_y,
                 double height)
The constructor.

Rectangle

public Rectangle(Rectangle r)
The constructor.

Rectangle

public Rectangle(int x,
                 int y,
                 int width,
                 int height)
The constructor.
Method Detail

setRectangle

public void setRectangle(Rectangle r)
setRectangle

scaleAboutCenter

public void scaleAboutCenter(double scale)
scaleAboutCenter

getType

public String getType()
Implement GeometricObject
Specified by:
getType in interface Paintable

toString

public String toString()
Override toString
Overrides:
toString in class Object

paint

public void paint()
Draw the object
Specified by:
paint in interface Paintable

paintOriginal

public void paintOriginal(Graphics g)
This method is used to paint the original data points that forms this GeometricObject
Specified by:
paintOriginal in interface Paintable

paint

public void paint(Graphics g,
                  int thickness)
Draw the object

shade

public void shade(Graphics g,
                  int shades,
                  int thickness,
                  int offset)
Shade the object

paint

public void paint(Graphics g)
Draw the object
Specified by:
paint in interface Paintable

shade

public void shade(Graphics g,
                  int number_of_shades)
Shade the object

pointIsOn

public boolean pointIsOn(Point point,
                         int radius)
Returns true if the point is within +-radius distance from the curve defining the object. Returns false o/w.
Specified by:
pointIsOn in interface Translatable

pointIsOnOriginal

public boolean pointIsOnOriginal(Point p,
                                 int radius)
Returns true if the point is within +-radius distance from the original curve defining the object. Returns false o/w.
Specified by:
pointIsOnOriginal in interface Translatable

setGraphicsContext

public void setGraphicsContext(Graphics g)
Set graphics context for this Geometric object. Must be set at least once before doing any drawing.
Specified by:
setGraphicsContext in interface Paintable

touches

public boolean touches(GeometricObject object)
This method should return true if the input objects touch. It should be optimized making use of the object type information.
Specified by:
touches in interface GeometricObject

getRectangularBounds

public Rectangle getRectangularBounds()
Supplied for completeness.
Specified by:
getRectangularBounds in interface GeometricObject

containsGeometricObject

public boolean containsGeometricObject(GeometricObject object)
Returns false if the argument is not completely inside this object. Return true O/W.
Specified by:
containsGeometricObject in interface GeometricObject

getPolygonalBounds

public Polygon getPolygonalBounds()
This method should return a polygon that corresponds to this object. The polygon is explicity closed and the last point doesn't necessarily have to be the same as the first (zeroth) point. The returned polygon is a liberal approximation to the real shape of the object. Known eksik: This should be refined to return a more conservative result.
Specified by:
getPolygonalBounds in interface GeometricObject

spatialRelation

public int spatialRelation(GeometricObject object,
                           double scale)
This method should return the spatial relation of the input parameter with respect to this object. see the SpatialRelation class for a detailed list of possible spatial relations. Another version of this method should be implemented for handling spatial relations where a rotated coordinate system is to be used. In this method the center square is adjusted using the scale argument. Note that we are using the cartesian coordinates for all the operations, but the returned relationship is for the screen coordinates. In this scheme Y axis is pointing south rather than north as it is in cartesian coordinates. Known eksik: Does not handle cases when directions are 0.
 Example for scale:
 Scale = 1   => 100% 
 Scale = 1.5 => 150% 

  (0,0)
      |--------------------------> X
      |
      |
      |       |   |
      |       |   |
      |    ___|___|___\ horizontal_up
      |       |   |   /
      |       |   |
      |    ___|___|___\ horizontal_down
      |       |   |   /
      |       |   |
      |       |   |
      |       V   V
      |     left right
      |
      |
      |
      |
   Y  V
 

spatialRelation

public int spatialRelation(GeometricObject object)
This method should return the spatial relation of the input parameter with respect to this object. see the SpatialRelation class for a detailed list of possible spatial relations. This method should hadles spatial relations where a rotated Rectangle is used
  (0,0)
      |--------------------------> X
      |
      |
      |       |   |
      |       |   |
      |    ___|___|___\ horizontal_up
      |       |   |   /
      |       |   |
      |    ___|___|___\ horizontal_down
      |       |   |   /
      |       |   |
      |       |   |
      |       V   V
      |     left right
      |
      |
      |
      |
   Y  V
 
Specified by:
spatialRelation in interface GeometricObject

hasOnMajorAxisDirection

public boolean hasOnMajorAxisDirection(GeometricObject object)
This method should return true if the input object lies in the major axis direction of this Rectangle. Else return false. This uses the screen coordinate system.
  (0,0)
      |------------------------> X
      |
      |
      |      /\         /\
      |       |          |
      |       |          |
      |    ___|__________|___\
      |       |          |   /
      |  *****|          |***** 
      |    ___|__________|___\
      |       |          |   /
      |       |          |
      |       |          |
      |
      |
      |
      |
   Y  V

 

spatialRelationCartesian

public int spatialRelationCartesian(GeometricObject object)
This method should return the spatial relation of the input parameter with respect to this object. see the SpatialRelation class for a detailed list of possible spatial relations. This returns the relationship in the cartesian coordinate system.
   Y  /\
      |
      |
      |      /\  /\
      |       |   |
      |       |   |
      |    ___|___|___\ 
      |       |   |   /
      |       |   |
      |    ___|___|___\ 
      |       |   |   /
      |       |   |
      |       |   |
      |
      |
      |
      |
      |--------------------------> X
  (0,0)
 

getWidth

public double getWidth()
Returns the width

getHeight

public double getHeight()
Returns the height

getAngle

public double getAngle()
Returns the angle of the object WRT the x axis in radians. This should be made more general by adding an angle field to the class.
Specified by:
getAngle in interface Rotatable

getMajorAxisLength

public double getMajorAxisLength()
Returns the larger of width and height.

getMinorAxisLength

public double getMinorAxisLength()
Returns the smaller of width and height.

rotateAbout

public void rotateAbout(Point p,
                        double theta)
Rotates the rectangle about the point p theta radians CCW.

getCenter

public Point getCenter()
Returns the center of this rectangle.

isRectangle

public static boolean isRectangle(Polygon p)
Returns true if the input polygon looks like a Rectangle Can be optimized by eliminating Polygon conversions.

setTimeStamp

public void setTimeStamp(long time_stamp)
Sets the time stamp of the current Terminal
Specified by:
setTimeStamp in interface Terminal

getTimeStamp

public long getTimeStamp()
Returns the time stamp of the current Terminal
Specified by:
getTimeStamp in interface Terminal

getMinorAxisAngle

public double getMinorAxisAngle()
Returns the angle between the shorter of width or height vectors and the x axis.

getMajorAxisAngle

public double getMajorAxisAngle()
Returns the angle between the longer of width or height vectors and the x axis.

getMajorAxisDirection

public int getMajorAxisDirection()
Returns the direction of the major axis of this Rectangle Known eksik: Should be tested.

setDataPoints

public void setDataPoints(Polygon points)
This method is used to set the original data points that forms this GeometricObject
Specified by:
setDataPoints in interface GeometricObject

getDataPoints

public Polygon getDataPoints()
This method is used to get the original data points that forms this GeometricObject
Specified by:
getDataPoints in interface GeometricObject

area

public double area()
Return area.

union

public Rectangle union(Rectangle rectangle)
Return the rectangularBounds of the union of this rectangle and the argument.

translate

public void translate(double x,
                      double y)
Add the arguments to the (x, y) position of the object.
Specified by:
translate in interface Translatable

containsGeometricObjects

public boolean containsGeometricObjects(GeometricObject[] objects)
Returns false if the objects in the input array are completely inside this object. Return true O/W.
Specified by:
containsGeometricObjects in interface GeometricObject