Here you can find the source of determineConnectionPoint(Point wayPoint, Polygon polygon)
Help function:
This function determine the connection point on polygon, Which is the nearest point to the given point.
Parameter | Description |
---|---|
wayPoint | - the given point |
polygon | - the given Polygon |
static Point determineConnectionPoint(Point wayPoint, Polygon polygon)
//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); } }