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

Point2Dproject(Point2D sourceLocation, double angle, double length)
project
return new Point2D.Double(sourceLocation.getX() + Math.sin(angle) * length,
        sourceLocation.getY() + Math.cos(angle) * length);
Point2D.Doubleproject(Point2D.Double sourceLocation, double angle, double length)
project
return project(sourceLocation, Math.sin(angle), Math.cos(angle), length);
doubleprojectionFactor(Point pt, Point start, Point stop)

Compute the Projection Factor for the projection of the given point pt onto the line segment defined by given start and end points.

if (pt.equals(start)) {
    return 0.0;
if (pt.equals(stop)) {
    return 1.0;
double dx = stop.x - start.x;
double dy = stop.y - start.y;
...
Point2DprojectPoint(float x, float y, Line2D ray, float distance)
Move a point a given distance along a line parallel to the ray implied by the the given line.
final Point2D.Float p = new Point2D.Float();
final double rotation = computeVerticalRotation(ray);
final java.awt.geom.AffineTransform tx = new java.awt.geom.AffineTransform();
tx.setToTranslation(x, y);
tx.rotate(rotation);
tx.translate(0, distance);
tx.transform(p, p);
return p;
...
Point2DprojectPointOntoLine(Point2D pt, Line2D ln)
Calculates the one point on a line which is nearest to a given point, such that a line between the given and the returned point will be orthogonal to the line.
double dx = ln.getX2() - ln.getX1();
double dy = ln.getY2() - ln.getY1();
double lineLenSq = dx * dx + dy * dy;
double d1;
if (lineLenSq == 0.0) {
    return (new Point2D.Double(ln.getX1(), ln.getY1()));
} else {
    d1 = ((pt.getX() - ln.getX1()) * dx + (pt.getY() - ln.getY1()) * dy) / lineLenSq;
...
voidquantisePoint(Point2D.Double dpos, Point gpos)
Maps a floating point graphics position to an integer graphics position, that is a 2-dimensional grid cell index.
gpos.x = ifloor(dpos.x);
gpos.y = ifloor(dpos.y);
intrelTo(final Point2D from, final Point2D to, final Point2D rel)
Calculates the relative orientation of two vectors.
return (int) Math.signum((to.getX() - from.getX()) * (from.getY() - rel.getY())
        - (rel.getX() - from.getX()) * (from.getY() - to.getY()));
voidresample(Vector points, int n, Vector newPoints)
resample
if (points.isEmpty())
    return;
Vector<Point2D> dstPts = new Vector<Point2D>(n);
double segLength = pathLength(points) / (n - 1);
double currentSegLength = 0;
Vector<Point2D> srcPts = new Vector<Point2D>(points);
dstPts.add((Point2D) srcPts.get(0).clone());
for (int i = 1; i < srcPts.size(); i++) {
...
GeneralPathsetPathAnchor(Shape s, Point2D pt)
set Path Anchor
Point2D a = getPathAnchor(s);
float dx = (float) (pt.getX() - a.getX());
float dy = (float) (pt.getY() - a.getY());
GeneralPath gp = new GeneralPath();
float[] coords = new float[6];
for (PathIterator p = s.getPathIterator(null); !p.isDone(); p.next()) {
    switch (p.currentSegment(coords)) {
    case PathIterator.SEG_MOVETO:
...
GeneralPathsetPathControlPoint(Shape s, int i, Point2D pt)
set Path Control Point
GeneralPath gp = new GeneralPath();
float[] coords = new float[6];
for (PathIterator p = s.getPathIterator(null); !p.isDone(); p.next()) {
    switch (p.currentSegment(coords)) {
    case PathIterator.SEG_MOVETO:
        if (i == 0) {
            coords[0] = (float) pt.getX();
            coords[1] = (float) pt.getY();
...