Java Geometry Algorithm projectionFactor(Point pt, Point start, Point stop)

Here you can find the source of projectionFactor(Point pt, Point start, Point stop)

Description

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

License

Open Source License

Parameter

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

Return

the project factor for the projection from the given point onto the line segment

Declaration

private static double projectionFactor(Point pt, Point start, Point stop) 

Method Source Code

//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|| &circ; 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 &lt; 0 : P is on the backward extension of AB</li>
     * <li>r &gt; 1 : P is on the forward extension of AB</li>
     * <li>0 &lt; r &lt; 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;
    }
}

Related

  1. preciseFrontBumperOffset( Point2D.Double sourceLocation, Point2D.Double botLocation)
  2. preprocess(Point2D pa, Point2D pb, Point2D pc)
  3. prodEscalar(Point2D p1, Point2D p2)
  4. project(Point2D sourceLocation, double angle, double length)
  5. project(Point2D.Double sourceLocation, double angle, double length)
  6. projectPoint(float x, float y, Line2D ray, float distance)
  7. projectPointOntoLine(Point2D pt, Line2D ln)
  8. quantisePoint(Point2D.Double dpos, Point gpos)
  9. relTo(final Point2D from, final Point2D to, final Point2D rel)