Java Geometry Algorithm determineConnectionPoint(Point wayPoint, Polygon polygon)

Here you can find the source of determineConnectionPoint(Point wayPoint, Polygon polygon)

Description

Help function:

This function determine the connection point on polygon, Which is the nearest point to the given point.

License

Open Source License

Parameter

Parameter Description
wayPoint - the given point
polygon - the given Polygon

Return

the connection point on the polygon

Declaration

static Point determineConnectionPoint(Point wayPoint, Polygon polygon) 

Method Source Code

//package com.java2s;
import java.awt.Point;
import java.awt.Polygon;

public class Main {
    /**//  w  ww .  jav a  2  s .c o m
     * <p>
     * <strong>Help function:</strong>
     * </p><p>
     * This function determine the connection point on polygon,
     * Which is the nearest point to the given point.
     * </p>
     *
     * @return the connection point on the polygon
     *
     * @param wayPoint - the given point
     * @param polygon - the given Polygon
     */
    static Point determineConnectionPoint(Point wayPoint, Polygon polygon) {
        // the shortest distance and the connection point.
        double shortestDistance = Double.MAX_VALUE;
        Point connectionPoint = new Point();

        // for every segment of the polygon, calculates the shortest distance,
        // and then updates the shortestDistance and connectionPoint if necessary.
        for (int i = 0; i < polygon.npoints; i++) {
            // the port of the segment.
            Point start = new Point(polygon.xpoints[i], polygon.ypoints[i]);
            Point end = new Point(
                    polygon.xpoints[(i + 1) % polygon.npoints],
                    polygon.ypoints[(i + 1) % polygon.npoints]);

            // Calculates shortest distance and updates the shortestDistance and connectionPoint if necessary
            double distance = Double.MAX_VALUE;
            if (dot(start, end, wayPoint) >= 0) {
                distance = wayPoint.distance(end);
                if (Double.compare(distance, shortestDistance) < 0) {
                    shortestDistance = distance;
                    connectionPoint = end;
                }
            } else if (dot(end, start, wayPoint) >= 0) {
                distance = wayPoint.distance(start);
                if (Double.compare(distance, shortestDistance) < 0) {
                    shortestDistance = distance;
                    connectionPoint = start;
                }
            } else {
                distance = Math.abs(cross(start, end, wayPoint))
                        / start.distance(end);
                if (Double.compare(distance, shortestDistance) < 0) {
                    shortestDistance = distance;
                    connectionPoint = getNearestPoint(start, end, wayPoint);
                }
            }
        }

        return connectionPoint;
    }

    /**
     * <p>
     * <strong>Help function:</strong>
     * </p><p>
     * This function calculates the dot product of vector ab and bc.
     * </p>
     *
     * @return the dot product of ab and bc
     *
     * @param a - one of the given point
     * @param b - one of the given point
     * @param c - one of the given point
     */
    static int dot(Point a, Point b, Point c) {
        return (b.x - a.x) * (c.x - b.x) + (b.y - a.y) * (c.y - b.y);
    }

    /**
     * <p>
     * <strong>Help function:</strong>
     * </p><p>
     * This function calculates the cross product of vector ab and bc.
     * </p>
     *
     * @return the cross product of ab and bc
     *
     * @param a - one of the given point
     * @param b - one of the given point
     * @param c - one of the given point
     */
    static int cross(Point a, Point b, Point c) {
        return (b.x - a.x) * (c.y - a.y) - (b.y - a.y) * (c.x - a.x);
    }

    /**
     * <p>
     * <strong>Help function:</strong>
     * </p><p>
     * This function finds the nearest point in segment ab to c.
     * </p>
     *
     * @return the nearest point in segment ab to c
     *
     * @param a - one of the given point
     * @param b - one of the given point
     * @param c - one of the given point
     */
    static Point getNearestPoint(Point a, Point b, Point c) {
        int x, y;
        int m, n;

        // if a and b is the same point, return a.
        if ((a.x == b.x) && (a.y == b.y)) {
            return a;
        }

        // calculates the nearest point in segment ab to c.
        m = b.x - a.x;
        n = b.y - a.y;
        x = (m * m * c.x + n * n * a.x + m * n * c.y - m * n * a.y)
                / (m * m + n * n);
        y = (m * m * a.y + n * n * c.y + m * n * c.x - m * n * a.x)
                / (m * m + n * n);

        return new Point(x, y);
    }
}

Related

  1. coordinateSplit(Point2D.Double[] aPoints, double[] aX, double[] aY)
  2. cornersToWorldFile(Point2D[] esq, Dimension size)
  3. curvedLineHit(Point point, Point startPoint, Point endPoint, Point controlPoint1, Point controlPoint2, float padding)
  4. derivativeOfCubicBezier(Point2D p0, Point2D p1, Point2D p2, Point2D p3, double t)
  5. deriveLocation(Point origin, double radius)
  6. directVincenty(Point2D.Double point, double brng, double distance)
  7. dot(@Nonnull Point2D pA, @Nonnull Point2D pB)
  8. dot(final Point2D a, final Point2D b)
  9. dot(Point a, Point b, Point c)