org.jhotdraw.geom
Class Geom

java.lang.Object
  extended by org.jhotdraw.geom.Geom

public class Geom
extends java.lang.Object

Some geometric utilities.

Version:
$Id: Geom.java 717 2010-11-21 12:30:57Z rawcoder $

Field Summary
static int OUT_BOTTOM
          The bitmask that indicates that a point lies below the rectangle.
static int OUT_LEFT
          The bitmask that indicates that a point lies to the left of the rectangle.
static int OUT_RIGHT
          The bitmask that indicates that a point lies to the right of the rectangle.
static int OUT_TOP
          The bitmask that indicates that a point lies above the rectangle.
 
Constructor Summary
private Geom()
           
 
Method Summary
static double angle(double x1, double y1, double x2, double y2)
          Gets the angle of the specified line.
static java.awt.geom.Point2D.Double angleToPoint(java.awt.geom.Rectangle2D.Double r, double angle)
          Gets the point on a rectangle that corresponds to the given angle.
static java.awt.Point angleToPoint(java.awt.Rectangle r, double angle)
          Gets the point on a rectangle that corresponds to the given angle.
static java.awt.geom.Point2D.Double cap(java.awt.geom.Point2D.Double p1, java.awt.geom.Point2D.Double p2, double radius)
          Caps the line defined by p1 and p2 by the number of units specified by radius.
static java.awt.Point center(java.awt.Rectangle r)
           
static java.awt.geom.Point2D.Double center(java.awt.geom.Rectangle2D.Double r)
           
static java.awt.geom.Point2D.Double chop(java.awt.Shape shape, java.awt.geom.Point2D.Double p)
          Returns a point on the edge of the shape which crosses the line from the center of the shape to the specified point.
static boolean contains(java.awt.geom.Rectangle2D.Double r1, java.awt.geom.Rectangle2D.Double r2)
          Returns true, if rectangle 1 contains rectangle 2.
static boolean contains(java.awt.geom.Rectangle2D r1, java.awt.geom.Rectangle2D r2)
          Returns true, if rectangle 1 contains rectangle 2.
static int direction(double x1, double y1, double x2, double y2)
          Returns the direction OUT_TOP, OUT_BOTTOM, OUT_LEFT, OUT_RIGHT from one point to another one.
static int direction(int x1, int y1, int x2, int y2)
          Returns the direction OUT_TOP, OUT_BOTTOM, OUT_LEFT, OUT_RIGHT from one point to another one.
static double distanceFromLine(int xa, int ya, int xb, int yb, int xc, int yc)
          compute distance of point from line segment, or Double.MAX_VALUE if perpendicular projection is outside segment; or If pts on line are same, return distance from point
static java.awt.Point east(java.awt.Rectangle r)
           
static java.awt.geom.Point2D.Double east(java.awt.geom.Rectangle2D.Double r)
           
static void grow(java.awt.geom.Rectangle2D.Double r, double h, double v)
          Resizes the Rectangle2D.Double both horizontally and vertically.
static java.awt.geom.Point2D.Double intersect(double xa, double ya, double xb, double yb, double xc, double yc, double xd, double yd)
          
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

OUT_TOP

public static final int OUT_TOP
The bitmask that indicates that a point lies above the rectangle.

See Also:
Constant Field Values

OUT_BOTTOM

public static final int OUT_BOTTOM
The bitmask that indicates that a point lies below the rectangle.

See Also:
Constant Field Values

OUT_LEFT

public static final int OUT_LEFT
The bitmask that indicates that a point lies to the left of the rectangle.

See Also:
Constant Field Values

OUT_RIGHT

public static final int OUT_RIGHT
The bitmask that indicates that a point lies to the right of the rectangle.

See Also:
Constant Field Values
Constructor Detail

Geom

private Geom()
Method Detail

lineContainsPoint

public static boolean lineContainsPoint(int x1,
                                        int y1,
                                        int x2,
                                        int y2,
                                        int px,
                                        int py)
Tests if a point is on a line.


lineContainsPoint

public static boolean lineContainsPoint(int x1,
                                        int y1,
                                        int x2,
                                        int y2,
                                        int px,
                                        int py,
                                        double tolerance)
Tests if a point is on a line.

changed Werner Randelshofer 2003-11-26


lineContainsPoint

public static boolean lineContainsPoint(double x1,
                                        double y1,
                                        double x2,
                                        double y2,
                                        double px,
                                        double py,
                                        double tolerance)
Tests if a point is on a line.

changed Werner Randelshofer 2003-11-26


direction

public static int direction(int x1,
                            int y1,
                            int x2,
                            int y2)
Returns the direction OUT_TOP, OUT_BOTTOM, OUT_LEFT, OUT_RIGHT from one point to another one.


direction

public static int direction(double x1,
                            double y1,
                            double x2,
                            double y2)
Returns the direction OUT_TOP, OUT_BOTTOM, OUT_LEFT, OUT_RIGHT from one point to another one.


outcode

public static int outcode(java.awt.Rectangle r1,
                          java.awt.Rectangle r2)
This method computes a binary OR of the appropriate mask values indicating, for each side of Rectangle r1, whether or not the Rectangle r2 is on the same side of the edge as the rest of this Rectangle.

Returns:
the logical OR of all appropriate out codes OUT_RIGHT, OUT_LEFT, OUT_BOTTOM, OUT_TOP.

outcode

public static int outcode(java.awt.geom.Rectangle2D.Double r1,
                          java.awt.geom.Rectangle2D.Double r2)
This method computes a binary OR of the appropriate mask values indicating, for each side of Rectangle r1, whether or not the Rectangle r2 is on the same side of the edge as the rest of this Rectangle.

Returns:
the logical OR of all appropriate out codes OUT_RIGHT, OUT_LEFT, OUT_BOTTOM, OUT_TOP.

south

public static java.awt.Point south(java.awt.Rectangle r)

south

public static java.awt.geom.Point2D.Double south(java.awt.geom.Rectangle2D.Double r)

center

public static java.awt.Point center(java.awt.Rectangle r)

center

public static java.awt.geom.Point2D.Double center(java.awt.geom.Rectangle2D.Double r)

chop

public static java.awt.geom.Point2D.Double chop(java.awt.Shape shape,
                                                java.awt.geom.Point2D.Double p)
Returns a point on the edge of the shape which crosses the line from the center of the shape to the specified point. If no edge crosses of the shape crosses the line, the nearest control point of the shape is returned.


west

public static java.awt.Point west(java.awt.Rectangle r)

west

public static java.awt.geom.Point2D.Double west(java.awt.geom.Rectangle2D.Double r)

east

public static java.awt.Point east(java.awt.Rectangle r)

east

public static java.awt.geom.Point2D.Double east(java.awt.geom.Rectangle2D.Double r)

north

public static java.awt.Point north(java.awt.Rectangle r)

north

public static java.awt.geom.Point2D.Double north(java.awt.geom.Rectangle2D.Double r)

range

public static int range(int min,
                        int max,
                        int value)
Constains a value to the given range.

Returns:
the constrained value

range

public static double range(double min,
                           double max,
                           double value)
Constains a value to the given range.

Returns:
the constrained value

length2

public static long length2(int x1,
                           int y1,
                           int x2,
                           int y2)
Gets the square distance between two points.


length

public static long length(int x1,
                          int y1,
                          int x2,
                          int y2)
Gets the distance between to points


length2

public static double length2(double x1,
                             double y1,
                             double x2,
                             double y2)
Gets the square distance between two points.


length

public static double length(double x1,
                            double y1,
                            double x2,
                            double y2)
Gets the distance between to points


length

public static double length(java.awt.geom.Point2D.Double p1,
                            java.awt.geom.Point2D.Double p2)
Gets the distance between to points


cap

public static java.awt.geom.Point2D.Double cap(java.awt.geom.Point2D.Double p1,
                                               java.awt.geom.Point2D.Double p2,
                                               double radius)
Caps the line defined by p1 and p2 by the number of units specified by radius.

Returns:
A new end point for the line.

pointToAngle

public static double pointToAngle(java.awt.Rectangle r,
                                  java.awt.Point p)
Gets the angle of a point relative to a rectangle.


pointToAngle

public static double pointToAngle(java.awt.geom.Rectangle2D.Double r,
                                  java.awt.geom.Point2D.Double p)
Gets the angle of a point relative to a rectangle.


angle

public static double angle(double x1,
                           double y1,
                           double x2,
                           double y2)
Gets the angle of the specified line.


angleToPoint

public static java.awt.Point angleToPoint(java.awt.Rectangle r,
                                          double angle)
Gets the point on a rectangle that corresponds to the given angle.


angleToPoint

public static java.awt.geom.Point2D.Double angleToPoint(java.awt.geom.Rectangle2D.Double r,
                                                        double angle)
Gets the point on a rectangle that corresponds to the given angle.


polarToPoint

public static java.awt.Point polarToPoint(double angle,
                                          double fx,
                                          double fy)
Converts a polar to a point


polarToPoint2D

public static java.awt.geom.Point2D.Double polarToPoint2D(double angle,
                                                          double fx,
                                                          double fy)
Converts a polar to a point


ovalAngleToPoint

public static java.awt.Point ovalAngleToPoint(java.awt.Rectangle r,
                                              double angle)
Gets the point on an oval that corresponds to the given angle.


ovalAngleToPoint

public static java.awt.geom.Point2D.Double ovalAngleToPoint(java.awt.geom.Rectangle2D.Double r,
                                                            double angle)
Gets the point on an oval that corresponds to the given angle.


intersect

Standard line intersection algorithm Return the point of intersection if it exists, else null.


intersect

Standard line intersection algorithm Return the point of intersection if it exists, else null


intersect


distanceFromLine

public static double distanceFromLine(int xa,
                                      int ya,
                                      int xb,
                                      int yb,
                                      int xc,
                                      int yc)
compute distance of point from line segment, or Double.MAX_VALUE if perpendicular projection is outside segment; or If pts on line are same, return distance from point


grow

public static void grow(java.awt.geom.Rectangle2D.Double r,
                        double h,
                        double v)
Resizes the Rectangle2D.Double both horizontally and vertically.

This method modifies the Rectangle2D.Double so that it is h units larger on both the left and right side, and v units larger at both the top and bottom.

The new Rectangle2D.Double has (x - h, y - v) as its top-left corner, a width of width + 2h, and a height of height + 2v.

If negative values are supplied for h and v, the size of the Rectangle2D.Double decreases accordingly. The grow method does not check whether the resulting values of width and height are non-negative.

Parameters:
h - the horizontal expansion
v - the vertical expansion

contains

public static boolean contains(java.awt.geom.Rectangle2D.Double r1,
                               java.awt.geom.Rectangle2D.Double r2)
Returns true, if rectangle 1 contains rectangle 2.

This method is similar to Rectangle2D.contains, but also returns true, when rectangle1 contains rectangle2 and either or both of them are empty.

Parameters:
r1 - Rectangle 1.
r2 - Rectangle 2.
Returns:
true if r1 contains r2.

contains

public static boolean contains(java.awt.geom.Rectangle2D r1,
                               java.awt.geom.Rectangle2D r2)
Returns true, if rectangle 1 contains rectangle 2.

This method is similar to Rectangle2D.contains, but also returns true, when rectangle1 contains rectangle2 and either or both of them are empty.

Parameters:
r1 - Rectangle 1.
r2 - Rectangle 2.
Returns:
true if r1 contains r2.