Java Utililty Methods Geometry Algorithm

List of utility methods to do Geometry Algorithm

Description

The list of methods to do Geometry Algorithm are organized into topic(s).

Method

Point2DgridAlign(final Point2D point, final double gridX, final double gridY)
Align the given point on the given grid.
return new Point2D.Double(Math.round(point.getX() / gridX) * gridX,
        Math.round(point.getY() / gridY) * gridY);
booleanhitsLine(final Point2D p, final Point2D fromPoint, final Point2D toPoint, final double thickness)
Whether a point is on a line of a given thickness.
if ((fromPoint == null) || (toPoint == null)) {
    return false;
double vx0 = fromPoint.getX();
double vy0 = fromPoint.getY();
double vx1 = toPoint.getX();
double vy1 = toPoint.getY();
double vx2 = p.getX();
...
booleaninsidePoly(Polygon pg, Point p)
Polygon.contains(Point) seems to not consistantly return the right value, so here is a rewrite.
double angle = 0;
Point p1 = null, p2 = null;
for (int i = 0; i < pg.npoints; i++) {
    p1 = new Point(pg.xpoints[i] - p.x, pg.ypoints[i] - p.y);
    p2 = new Point(pg.xpoints[(i + 1) % pg.npoints] - p.x, pg.ypoints[(i + 1) % pg.npoints] - p.y);
    angle += angle2D(p1, p2);
return Math.abs(angle) >= Math.PI;
...
Point2DinterceptLineAndBox(Point2D startPosition, Point2D endPosition, RectangularShape boundingBox)
intercept Line And Box
Point2D intercept = new Point2D.Double();
if (startPosition.getX() == endPosition.getX()) {
    if (startPosition.getY() > endPosition.getY()) {
        intercept.setLocation(startPosition.getX(), boundingBox.getMaxY());
    } else {
        intercept.setLocation(startPosition.getX(), boundingBox.getMinY());
} else {
...
Pointinterpol(Point p1, Point p2, float factor)
makes a curvic interpolation between points
Point p = new Point((int) (p1.x * (1 - factor) + p2.x * factor),
        (int) (p1.y * (1 - factor) + p2.y * factor));
int targety = factor <= 0.5 ? p1.y : p2.y;
float facy = Math.abs(factor - 0.5f) * 2;
p.y = (int) (targety * facy + p.y * (1 - facy));
return p;
Point2DinvVec(final Point2D v)
Generates a vector pointing in the opposite direction.
return mulVec(v, -1.0);
StringlonLatToString(Point2D.Double pt)
Converts signed double longitude and latitude to string.
return String.format("%s, %s", longitudeToString(pt.x), latitudeToString(pt.y));
GeneralPathmakeCircle(double xCenter, double yCenter, double r, int nPoints)
make Circle
if (nPoints < 4)
    throw new RuntimeException("too few points. n=" + nPoints);
GeneralPath gp = new GeneralPath();
for (int i = 0; i < nPoints; i++) {
    double angle = i / (double) nPoints * Math.PI * 2;
    double x = r * Math.cos(angle) + xCenter;
    double y = r * Math.sin(angle) + yCenter;
    if (i == 0)
...
voidmakeCornerTo(GeneralPath gp, Point2D cornerPoint, Point2D nextCornerPoint, float radius)
Draws a corner relative to the current point of the GeneralPath.
Point2D currentPoint = gp.getCurrentPoint();
double distance = currentPoint.distance(cornerPoint);
double fraction = (distance - radius) / distance;
double xDistance = (cornerPoint.getX() - currentPoint.getX()) * fraction;
double yDistance = (cornerPoint.getY() - currentPoint.getY()) * fraction;
lineToRelative(gp, (float) xDistance, (float) yDistance);
Point2D startCurvePoint = gp.getCurrentPoint();
double distanceFromCornerToNextCorner = cornerPoint.distance(nextCornerPoint);
...
ShapemakeLine(Point2D.Double center, Point2D.Double north, Point2D.Double east)
Return a Shape object for the "line" symbol.
Point2D.Double south = new Point2D.Double(center.x - (north.x - center.x), center.y - (north.y - center.y));
Line2D.Double p = new Line2D.Double(north, south);
return p;