Back to index

moin  1.9.0~rc2
Static Public Member Functions | Static Public Attributes | Private Member Functions
CH.ifa.draw.util.Geom Class Reference

Some geometric utilities. More...

List of all members.

Static Public Member Functions

static boolean lineContainsPoint (int x1, int y1, int x2, int y2, int px, int py)
 Tests if a point is on a line.
static int direction (int x1, int y1, int x2, int y2)
 Returns the direction NORTH, SOUTH, WEST, EAST from one point to another one.
static Point south (Rectangle r)
static Point center (Rectangle r)
static Point west (Rectangle r)
static Point east (Rectangle r)
static Point north (Rectangle r)
static int range (int min, int max, int value)
 Constains a value to the given range.
static long length2 (int x1, int y1, int x2, int y2)
 Gets the square distance between two points.
static long length (int x1, int y1, int x2, int y2)
 Gets the distance between to points.
static double pointToAngle (Rectangle r, Point p)
 Gets the angle of a point relative to a rectangle.
static Point angleToPoint (Rectangle r, double angle)
 Gets the point on a rectangle that corresponds to the given angle.
static Point polarToPoint (double angle, double fx, double fy)
 Converts a polar to a point.
static Point ovalAngleToPoint (Rectangle r, double angle)
 Gets the point on an oval that corresponds to the given angle.
static Point intersect (int xa, int ya, int xb, int yb, int xc, int yc, int xd, int yd)
 Standard line intersection algorithm Return the point of intersection if it exists, else null.

Static Public Attributes

static final int NORTH = 1
static final int SOUTH = 2
static final int WEST = 3
static final int EAST = 4

Private Member Functions

 Geom ()

Detailed Description

Some geometric utilities.

Definition at line 14 of file Geom.java.


Constructor & Destructor Documentation

CH.ifa.draw.util.Geom.Geom ( ) [inline, private]

Definition at line 16 of file Geom.java.

{}; // never instantiated

Member Function Documentation

static Point CH.ifa.draw.util.Geom.angleToPoint ( Rectangle  r,
double  angle 
) [inline, static]

Gets the point on a rectangle that corresponds to the given angle.

Definition at line 131 of file Geom.java.

                                                                {
        double si = Math.sin(angle);
        double co = Math.cos(angle);
        double e = 0.0001;

       int x= 0, y= 0;
           if (Math.abs(si) > e) {
                  x= (int) ((1.0 + co/Math.abs(si))/2.0 * r.width);
                  x= range(0, r.width, x);
           } else if (co >= 0.0)
                  x= r.width;
           if (Math.abs(co) > e) {
                  y= (int) ((1.0 + si/Math.abs(co))/2.0 * r.height);
                  y= range(0, r.height, y);
           } else if (si >= 0.0)
                  y= r.height;
           return new Point(r.x + x, r.y + y);
    }

Here is the call graph for this function:

Here is the caller graph for this function:

static Point CH.ifa.draw.util.Geom.center ( Rectangle  r) [inline, static]

Definition at line 77 of file Geom.java.

                                            {
        return new Point(r.x + r.width /2, r.y + r.height/2);
    }

Here is the caller graph for this function:

static int CH.ifa.draw.util.Geom.direction ( int  x1,
int  y1,
int  x2,
int  y2 
) [inline, static]

Returns the direction NORTH, SOUTH, WEST, EAST from one point to another one.

Definition at line 57 of file Geom.java.

                                                                {
        int direction = 0;
        int vx = x2 - x1;
        int vy = y2 - y1;

        if (vy < vx && vx > -vy)
            direction = EAST;
        else if (vy > vx && vy > -vx)
            direction = NORTH;
        else if (vx < vy && vx < -vy)
            direction = WEST;
        else
            direction = SOUTH;
        return direction;
    }

Here is the caller graph for this function:

static Point CH.ifa.draw.util.Geom.east ( Rectangle  r) [inline, static]

Definition at line 85 of file Geom.java.

                                          {
        return new Point(r.x+r.width, r.y + r.height/ 2);
    }

Here is the caller graph for this function:

static Point CH.ifa.draw.util.Geom.intersect ( int  xa,
int  ya,
int  xb,
int  yb,
int  xc,
int  yc,
int  xd,
int  yd 
) [inline, static]

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

Definition at line 173 of file Geom.java.

                                        { // line 2 point 2 y

    // source: http://vision.dai.ed.ac.uk/andrewfg/c-g-a-faq.html
    // eq: for lines AB and CD
    //     (YA-YC)(XD-XC)-(XA-XC)(YD-YC)
    // r = -----------------------------  (eqn 1)
    //     (XB-XA)(YD-YC)-(YB-YA)(XD-XC)
    //
    //     (YA-YC)(XB-XA)-(XA-XC)(YB-YA)
    // s = -----------------------------  (eqn 2)
    //     (XB-XA)(YD-YC)-(YB-YA)(XD-XC)
    //  XI = XA + r(XB-XA)
    //  YI = YA + r(YB-YA)

    double denom = ((xb - xa) * (yd - yc) - (yb - ya) * (xd - xc));

    double rnum = ((ya - yc) * (xd - xc) - (xa - xc) * (yd - yc));

    if (denom == 0.0) { // parallel
      if (rnum == 0.0) { // coincident; pick one end of first line
        if ((xa < xb && (xb < xc || xb < xd)) ||
            (xa > xb && (xb > xc || xb > xd)))
          return new Point(xb, yb);
        else
          return new Point(xa, ya);
      }
      else
        return null;
    }

    double r = rnum / denom;

    double snum = ((ya - yc) * (xb - xa) - (xa - xc) * (yb - ya));
    double s = snum / denom;

    if (0.0 <= r && r <= 1.0 && 0.0 <= s && s <= 1.0) {
      int px = (int)(xa + (xb - xa) * r);
      int py = (int)(ya + (yb - ya) * r);
      return new Point(px, py);
    }
    else
      return null;
  }

Here is the caller graph for this function:

static long CH.ifa.draw.util.Geom.length ( int  x1,
int  y1,
int  x2,
int  y2 
) [inline, static]

Gets the distance between to points.

Definition at line 115 of file Geom.java.

                                                              {
        return (long)Math.sqrt(length2(x1, y1, x2, y2));
    }

Here is the call graph for this function:

Here is the caller graph for this function:

static long CH.ifa.draw.util.Geom.length2 ( int  x1,
int  y1,
int  x2,
int  y2 
) [inline, static]

Gets the square distance between two points.

Definition at line 108 of file Geom.java.

                                                               {
        return (x2-x1)*(x2-x1) + (y2-y1)*(y2-y1);
    }

Here is the caller graph for this function:

static boolean CH.ifa.draw.util.Geom.lineContainsPoint ( int  x1,
int  y1,
int  x2,
int  y2,
int  px,
int  py 
) [inline, static]

Tests if a point is on a line.

Definition at line 21 of file Geom.java.

                                            {

        Rectangle r = new Rectangle(new Point(x1, y1));
        r.add(x2, y2);

        r.grow(2,2);
        if (! r.contains(px,py))
            return false;

        double a, b, x, y;

        if (x1 == x2)
            return (Math.abs(px - x1) < 3);

        if (y1 == y2)
            return (Math.abs(py - y1) < 3);

        a = (double)(y1 - y2) / (double)(x1 - x2);
        b = (double)y1 - a * (double)x1;
        x = (py - b) / a;
        y = a * px + b;

        return (Math.min(Math.abs(x - px), Math.abs(y - py)) < 4);
    }

Here is the caller graph for this function:

static Point CH.ifa.draw.util.Geom.north ( Rectangle  r) [inline, static]

Definition at line 89 of file Geom.java.

                                           {
        return new Point(r.x+r.width/2, r.y);
    }

Here is the caller graph for this function:

static Point CH.ifa.draw.util.Geom.ovalAngleToPoint ( Rectangle  r,
double  angle 
) [inline, static]

Gets the point on an oval that corresponds to the given angle.

Definition at line 162 of file Geom.java.

                                                                    {
        Point center = Geom.center(r);
        Point p = Geom.polarToPoint(angle, r.width/2, r.height/2);
        return new Point(center.x + p.x, center.y + p.y);
    }

Here is the call graph for this function:

Here is the caller graph for this function:

static double CH.ifa.draw.util.Geom.pointToAngle ( Rectangle  r,
Point  p 
) [inline, static]

Gets the angle of a point relative to a rectangle.

Definition at line 122 of file Geom.java.

                                                            {
        int px = p.x - (r.x+r.width/2);
        int py = p.y - (r.y+r.height/2);
        return Math.atan2(py*r.width, px*r.height);
    }

Here is the caller graph for this function:

static Point CH.ifa.draw.util.Geom.polarToPoint ( double  angle,
double  fx,
double  fy 
) [inline, static]

Converts a polar to a point.

Definition at line 153 of file Geom.java.

                                                                         {
        double si = Math.sin(angle);
        double co = Math.cos(angle);
           return new Point((int)(fx*co+0.5), (int)(fy*si+0.5));
    }

Here is the caller graph for this function:

static int CH.ifa.draw.util.Geom.range ( int  min,
int  max,
int  value 
) [inline, static]

Constains a value to the given range.

Returns:
the constrained value

Definition at line 97 of file Geom.java.

                                                         {
        if (value < min)
            value = min;
        if (value > max)
            value = max;
        return value;
    }

Here is the caller graph for this function:

static Point CH.ifa.draw.util.Geom.south ( Rectangle  r) [inline, static]

Definition at line 73 of file Geom.java.

                                           {
        return new Point(r.x + r.width /2, r.y + r.height);
    }

Here is the caller graph for this function:

static Point CH.ifa.draw.util.Geom.west ( Rectangle  r) [inline, static]

Definition at line 81 of file Geom.java.

                                          {
        return new Point(r.x, r.y + r.height/ 2);
    }

Here is the caller graph for this function:


Member Data Documentation

final int CH.ifa.draw.util.Geom.EAST = 4 [static]

Definition at line 51 of file Geom.java.

final int CH.ifa.draw.util.Geom.NORTH = 1 [static]

Definition at line 48 of file Geom.java.

final int CH.ifa.draw.util.Geom.SOUTH = 2 [static]

Definition at line 49 of file Geom.java.

final int CH.ifa.draw.util.Geom.WEST = 3 [static]

Definition at line 50 of file Geom.java.


The documentation for this class was generated from the following file: