net.paulhertz.geom
Class GeomUtils

java.lang.Object
  extended by net.paulhertz.geom.GeomUtils

public class GeomUtils
extends Object

Provides static methods for basic geometric transforms. Methods for translating, scaling, and rotating a point, with single precision (using floats). Rotation and scaling center on (0,0) unless you call scaleCoorAroundPoint or rotateCoorAroundPoint. Rotation is counterclockwise in Cartesian coordinate system (positive y-axis points up) and clockwise in screen display system (positive y-axis points down). Methods that accept separate x and y coordinates return a new Point2D. Methods that accept a Point2D modify and return the point (this allows chaining of methods). It's possible to do all this with matrices, of course, and often (but not always) more efficient.


Field Summary
static double TWO_PI
           
 
Method Summary
static double area(double[] coords)
          outputs area of polygon, negative if vertices are in CCW order.
static float area(float[] coords)
          outputs area of polygon, negative if vertices are in CCW order.
static float area(int[] coords)
          outputs area of polygon, negative if vertices are in CCW order.
static double clamp(double v, double l, double h)
          clamps input v to the specified range from l to h
static float clamp(float v, float l, float h)
          clamps input v to the specified range from l to h
static double distSquared(double x0, double y0, double x1, double y1)
          finds squared distance between two points
static float distSquared(float x0, float y0, float x1, float y1)
          finds squared distance between two points
static double getAngle(double dx, double dy)
           
static float getAngle(float dx, float dy)
          Given dx and dy displacements on x axis and y axis, returns the angle of rotation, measured counterclockwise from 0 in Cartesian system, in radians.
static double lerp(double a, double l, double h)
          Interpolates a value within a range.
static float lerp(float a, float l, float h)
          Interpolates a value within a range.
static boolean pointInPoly(int npol, double[] xp, double[] yp, double x, double y)
           
static boolean pointInPoly(int npol, float[] xp, float[] yp, float x, float y)
          decides if a point is inside a polygon
static Point2D.Double rotateCoor(double x, double y, double theta)
           
static Point2D.Float rotateCoor(float x, float y, float theta)
          rotates a point theta radians, returns a new point rotation is counterclockwise for positive theta in Cartesian system, clockwise in screen display coordinate system
static Point2D.Double rotateCoor(Point2D.Double pt, double theta)
           
static Point2D.Float rotateCoor(Point2D.Float pt, float theta)
          rotates a point theta radians, modifies and returns the point rotation is counterclockwise for positive theta in Cartesian system, clockwise in screen display coordinate system
static Point2D.Double rotateCoorAroundPoint(double x, double y, double xctr, double yctr, double theta)
           
static Point2D.Float rotateCoorAroundPoint(float x, float y, float xctr, float yctr, float theta)
          rotates a point theta radians around a point (xctr, yctr), returns a new point rotation is counterclockwise for positive theta in Cartesian system, clockwise in screen display coordinate system
static Point2D.Double rotateCoorAroundPoint(Point2D.Double pt, double xctr, double yctr, double theta)
           
static Point2D.Float rotateCoorAroundPoint(Point2D.Float pt, float xctr, float yctr, float theta)
          rotates a point theta radians around a point (xctr, yctr), modifies and returns the point rotation is counterclockwise for positive theta in Cartesian system, clockwise in screen display coordinate system
static Point2D.Double scaleCoor(double x, double y, double xScale, double yScale)
           
static Point2D.Float scaleCoor(float x, float y, float xScale, float yScale)
          scales a point by xScale and yScale, returns a new point
static Point2D.Double scaleCoor(Point2D.Double pt, double xScale, double yScale)
           
static Point2D.Float scaleCoor(Point2D.Float pt, float xScale, float yScale)
          scales a point by xScale and yScale, modifies and returns the point
static Point2D.Double scaleCoorAroundPoint(double x, double y, double xScale, double yScale, double xctr, double yctr)
           
static Point2D.Float scaleCoorAroundPoint(float x, float y, float xScale, float yScale, float xctr, float yctr)
          scales a point by xScale and yScale arround a point (xctr, yctr), returns a new point
static Point2D.Float scaleCoorAroundPoint(Point2D.Float pt, float xScale, float yScale, float xctr, float yctr)
          scales a point by xScale and yScale around a point (xctr, yctr), modifies and returns the point
static int sgn(double a)
          take binary sign of a, either -1, or 1 if >= 0
static int sgn(float a)
          take binary sign of a, either -1, or 1 if >= 0
static int sgn(int a)
          take binary sign of a, either -1, or 1 if >= 0
static int sgn(long a)
          take binary sign of a, either -1, or 1 if >= 0
static Point2D.Double translateCoor(double x, double y, double xOffset, double yOffset)
           
static Point2D.Float translateCoor(float x, float y, float xOffset, float yOffset)
          translates a point by xOffset and yOffset, returns a new point
static Point2D.Double translateCoor(Point2D.Double pt, double xOffset, double yOffset)
           
static Point2D.Float translateCoor(Point2D.Float pt, float xOffset, float yOffset)
          translates a point by xOffset and yOffset, modifies and returns the point
static double[] xCoords(double[] arr)
           
static float[] xCoords(float[] arr)
          extracts array of x-coordinates from an array of points in x,y order
static double[] yCoords(double[] arr)
           
static float[] yCoords(float[] arr)
          extracts array of y-coordinates from an array of points in x,y order
static int zsgn(double a)
          take sign of a, either -1, 0, or 1
static int zsgn(float a)
          take sign of a, either -1, 0, or 1
static int zsgn(int a)
          take sign of a, either -1, 0, or 1
static int zsgn(long a)
          take sign of a, either -1, 0, or 1
 
Methods inherited from class java.lang.Object
equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

TWO_PI

public static final double TWO_PI
See Also:
Constant Field Values
Method Detail

zsgn

public static int zsgn(double a)
take sign of a, either -1, 0, or 1


zsgn

public static int zsgn(float a)
take sign of a, either -1, 0, or 1


zsgn

public static int zsgn(int a)
take sign of a, either -1, 0, or 1


zsgn

public static int zsgn(long a)
take sign of a, either -1, 0, or 1


sgn

public static int sgn(double a)
take binary sign of a, either -1, or 1 if >= 0


sgn

public static int sgn(float a)
take binary sign of a, either -1, or 1 if >= 0


sgn

public static int sgn(int a)
take binary sign of a, either -1, or 1 if >= 0


sgn

public static int sgn(long a)
take binary sign of a, either -1, or 1 if >= 0


lerp

public static double lerp(double a,
                          double l,
                          double h)
Interpolates a value within a range. linear interpolation from l (when a=0) to h (when a=1) (equal to (a * h) + (1 - a) * l)


lerp

public static float lerp(float a,
                         float l,
                         float h)
Interpolates a value within a range. linear interpolation from l (when a=0) to h (when a=1) (equal to (a * h) + (1 - a) * l)


clamp

public static double clamp(double v,
                           double l,
                           double h)
clamps input v to the specified range from l to h


clamp

public static float clamp(float v,
                          float l,
                          float h)
clamps input v to the specified range from l to h


scaleCoor

public static Point2D.Float scaleCoor(float x,
                                      float y,
                                      float xScale,
                                      float yScale)
scales a point by xScale and yScale, returns a new point

Parameters:
x - x coordinate of point
y - y coordinate of point
xScale - scaling on x-axis
yScale - scaling on y-axis
Returns:
a new scaled point

scaleCoor

public static Point2D.Double scaleCoor(double x,
                                       double y,
                                       double xScale,
                                       double yScale)

scaleCoor

public static Point2D.Float scaleCoor(Point2D.Float pt,
                                      float xScale,
                                      float yScale)
scales a point by xScale and yScale, modifies and returns the point

Parameters:
pt - the point to scale
xScale - scaling on x-axis
yScale - scaling on y-axis
Returns:
the scaled point

scaleCoor

public static Point2D.Double scaleCoor(Point2D.Double pt,
                                       double xScale,
                                       double yScale)

scaleCoorAroundPoint

public static Point2D.Float scaleCoorAroundPoint(float x,
                                                 float y,
                                                 float xScale,
                                                 float yScale,
                                                 float xctr,
                                                 float yctr)
scales a point by xScale and yScale arround a point (xctr, yctr), returns a new point

Parameters:
x - x coordinate of point
y - y coordinate of point
xScale - scaling on x-axis
yScale - scaling on y-axis
xctr - x coordinate of center of transformation
yctr - y coordinate of center of transformation
Returns:
a new scaled point

scaleCoorAroundPoint

public static Point2D.Double scaleCoorAroundPoint(double x,
                                                  double y,
                                                  double xScale,
                                                  double yScale,
                                                  double xctr,
                                                  double yctr)

scaleCoorAroundPoint

public static Point2D.Float scaleCoorAroundPoint(Point2D.Float pt,
                                                 float xScale,
                                                 float yScale,
                                                 float xctr,
                                                 float yctr)
scales a point by xScale and yScale around a point (xctr, yctr), modifies and returns the point

Parameters:
pt - the point to scale
xScale - scaling on x-axis
yScale - scaling on y-axis
xctr - x coordinate of center of transformation
yctr - y coordinate of center of transformation
Returns:
the scaled point

translateCoor

public static Point2D.Float translateCoor(float x,
                                          float y,
                                          float xOffset,
                                          float yOffset)
translates a point by xOffset and yOffset, returns a new point

Parameters:
x - x coordinate of point
y - y coordinate of point
xOffset - distance to translate on x-xis
yOffset - distance to translate on y-axis
Returns:
a new translated point

translateCoor

public static Point2D.Double translateCoor(double x,
                                           double y,
                                           double xOffset,
                                           double yOffset)

translateCoor

public static Point2D.Float translateCoor(Point2D.Float pt,
                                          float xOffset,
                                          float yOffset)
translates a point by xOffset and yOffset, modifies and returns the point

Parameters:
pt - the point to translate
xOffset - distance to translate on x-xis
yOffset - distance to translate on y-axis
Returns:
the translated point

translateCoor

public static Point2D.Double translateCoor(Point2D.Double pt,
                                           double xOffset,
                                           double yOffset)

rotateCoor

public static Point2D.Float rotateCoor(float x,
                                       float y,
                                       float theta)
rotates a point theta radians, returns a new point rotation is counterclockwise for positive theta in Cartesian system, clockwise in screen display coordinate system

Parameters:
x - x coordinate of point
y - y coordinate of point
theta - angle to rotate, in radians
Returns:
a new point

rotateCoor

public static Point2D.Double rotateCoor(double x,
                                        double y,
                                        double theta)

rotateCoor

public static Point2D.Float rotateCoor(Point2D.Float pt,
                                       float theta)
rotates a point theta radians, modifies and returns the point rotation is counterclockwise for positive theta in Cartesian system, clockwise in screen display coordinate system

Parameters:
pt - the point to rotate
theta - angle to rotate, in radians
Returns:
the rotated point

rotateCoor

public static Point2D.Double rotateCoor(Point2D.Double pt,
                                        double theta)

rotateCoorAroundPoint

public static Point2D.Float rotateCoorAroundPoint(float x,
                                                  float y,
                                                  float xctr,
                                                  float yctr,
                                                  float theta)
rotates a point theta radians around a point (xctr, yctr), returns a new point rotation is counterclockwise for positive theta in Cartesian system, clockwise in screen display coordinate system

Parameters:
x - x coordinate of point
y - y coordinate of point
xctr - x coordinate of center of rotation
yctr - y coordinate of center of rotation
theta - angle to rotate, in radians
Returns:
a new rotated point

rotateCoorAroundPoint

public static Point2D.Double rotateCoorAroundPoint(double x,
                                                   double y,
                                                   double xctr,
                                                   double yctr,
                                                   double theta)

rotateCoorAroundPoint

public static Point2D.Float rotateCoorAroundPoint(Point2D.Float pt,
                                                  float xctr,
                                                  float yctr,
                                                  float theta)
rotates a point theta radians around a point (xctr, yctr), modifies and returns the point rotation is counterclockwise for positive theta in Cartesian system, clockwise in screen display coordinate system

Parameters:
pt - the point to rotate
xctr - x coordinate of center of rotation
yctr - y coordinate of center of rotation
theta - angle to rotate, in radians
Returns:
the rotated point

rotateCoorAroundPoint

public static Point2D.Double rotateCoorAroundPoint(Point2D.Double pt,
                                                   double xctr,
                                                   double yctr,
                                                   double theta)

getAngle

public static float getAngle(float dx,
                             float dy)
Given dx and dy displacements on x axis and y axis, returns the angle of rotation, measured counterclockwise from 0 in Cartesian system, in radians. angle will be measured closckwise in screen display system

Parameters:
dx - x coordinate of point
dy - y coordinate of point
Returns:
angle in radians

getAngle

public static double getAngle(double dx,
                              double dy)

pointInPoly

public static boolean pointInPoly(int npol,
                                  float[] xp,
                                  float[] yp,
                                  float x,
                                  float y)
decides if a point is inside a polygon

Parameters:
npol - number of points in polygon
xp - array of x-coordinates
yp - array of y-coordinates
x - x-coordinate of point
y - y-coordinate of point
Returns:
true if point is in polygon, false otherwise

pointInPoly

public static boolean pointInPoly(int npol,
                                  double[] xp,
                                  double[] yp,
                                  double x,
                                  double y)

xCoords

public static float[] xCoords(float[] arr)
extracts array of x-coordinates from an array of points in x,y order

Parameters:
arr - array of points in x,y order
Returns:
array of x-coordinates (even index values)

xCoords

public static double[] xCoords(double[] arr)

yCoords

public static float[] yCoords(float[] arr)
extracts array of y-coordinates from an array of points in x,y order

Parameters:
arr - array of points in x,y order
Returns:
array of y-coordinates (odd index values)

yCoords

public static double[] yCoords(double[] arr)

distSquared

public static float distSquared(float x0,
                                float y0,
                                float x1,
                                float y1)
finds squared distance between two points

Parameters:
x0 - x-coordinate of first point
y0 - y-coordinate of first point
x1 - x-coordinate of second point
y1 - y-coordinate of second point
Returns:
squared distance between points

distSquared

public static double distSquared(double x0,
                                 double y0,
                                 double x1,
                                 double y1)
finds squared distance between two points

Parameters:
x0 - x-coordinate of first point
y0 - y-coordinate of first point
x1 - x-coordinate of second point
y1 - y-coordinate of second point
Returns:
squared distance between points

area

public static float area(int[] coords)
outputs area of polygon, negative if vertices are in CCW order. if last coordinate pair is a copy of first coordinate pair, area is unaffected

Since:
October 21, 2011

area

public static float area(float[] coords)
outputs area of polygon, negative if vertices are in CCW order. if last coordinate pair is a copy of first coordinate pair, area is unaffected

Since:
October 21, 2011

area

public static double area(double[] coords)
outputs area of polygon, negative if vertices are in CCW order. if last coordinate pair is a copy of first coordinate pair, area is unaffected

Since:
October 21, 2011


Processing library IgnoCodeLib by Paul Hertz. (C) 2013