Here you can find the source of projectionFactor(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.
Parameter | Description |
---|---|
pt | the point to get the closest point on the given segment |
start | the start point of the segment |
stop | the stop point of the segment |
private static double projectionFactor(Point pt, Point start, Point stop)
//package com.java2s; import java.awt.Point; public class Main { /**//from w w w . j av a2 s . c o m * <p> * Compute the Projection Factor for the projection of the given point pt onto the line segment defined by given * start and end points. * </p> * <p> * The Projection Factor is computed as the following formula: * * <pre> * project factor = (AC dot AB) / (||AB|| ˆ 2) * </pre> * * <b>C</b> is the given point <b>pt</b>, while <b>A</b> is the given start point and <b>B</b> is the given end * point. * <p> * <p> * The project factor has the following meaning: * <ul> * <li>r = 0 : P = A</li> * <li>r = 1 : P = B</li> * <li>r < 0 : P is on the backward extension of AB</li> * <li>r > 1 : P is on the forward extension of AB</li> * <li>0 < r < 1 : P is interior to AB</li> * </ul> * Note, <b>P</b> is the projected point of the given point <b>pt</b> on the segment (defined by <b>A</b> and * <b>B</b>) * </p> * * @param pt the point to get the closest point on the given segment * @param start the start point of the segment * @param stop the stop point of the segment * @return the project factor for the projection from the given point onto the line segment */ private static double projectionFactor(Point pt, Point start, Point stop) { 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; // calculate the value of (||AB|| ^ 2) double len2 = dx * dx + dy * dy; // calculate the value of (AC dot AB) / (||AB|| ^ 2) return ((pt.x - start.x) * dx + (pt.y - start.y) * dy) / len2; } }