Find a point at a given perpendicular distance from a line - Java java.lang

Java examples for java.lang:Math Geometry Distance

Description

Find a point at a given perpendicular distance from a line

Demo Code


//package com.java2s;

import java.awt.geom.Point2D;

public class Main {
    /**/* w  w  w.ja  v a 2s .  co  m*/
     * Find a point at a given perpendicular distance from a line
     * 
     * @param ptA
     *            Start of line segment
     * @param ptB
     *            End of line segment
     * @param ptP
     *            Point of AB line
     * @param distPC
     *            Distance from line to return Point ptC <br>
     *            If >0 angle between AB and PC is +90? <br>
     *            If <0 angle between AB and PC is -+90?
     * @return ptC point
     */
    public static Point2D getPerpendicularPointFromLine(Point2D ptA,
            Point2D ptB, Point2D ptP, double distPC) {
        if (ptA == null || ptB == null || ptA.equals(ptB) || ptP == null) {
            return null;
        }

        double distAB = ptA.distance(ptB);
        double ux = -(ptB.getY() - ptA.getY()) / distAB;
        double uy = (ptB.getX() - ptA.getX()) / distAB;

        return new Point2D.Double(ptP.getX() + distPC * ux, ptP.getY()
                + distPC * uy);
    }

    public static Point2D getPerpendicularPointFromLine(Point2D ptA,
            Point2D ptB, double distAP, double distPC) {
        return getPerpendicularPointFromLine(ptA, ptB,
                getCollinearPointWithLength(ptA, ptB, distAP), distPC);
    }

    /**
     * @param ptA
     *            the first point of line segment
     * @param ptB
     *            the last point of line segment
     * @param newLength
     *            represents length from ptA to the return point ptC along AB line segment.<br>
     *            If >AB , ptC point will be located on extension of AB<br>
     *            If <0 , ptC point will be located on extension of BA <br>
     *            If >0 && < AB , ptC point will be interior of AB<br>
     * @return New point ptC coordinates or null if any argument is invalid
     */
    public static Point2D.Double getCollinearPointWithLength(Point2D ptA,
            Point2D ptB, double newLength) {
        if (ptA != null && ptB != null) {
            return getCollinearPointWithRatio(ptA, ptB,
                    newLength / ptA.distance(ptB));
        }
        return null;
    }

    /**
     * @param ptA
     *            first point of line segment
     * @param ptB
     *            last point of line segment
     * @param k
     *            represents ratio between AB and AC, ptC being the returned point along AB line segment.<br>
     *            If >1 , ptC point will be located on extension of AB<br>
     *            If <0 , ptC point will be located on extension of BA <br>
     *            If >0 && <AB , ptC point will be interior of AB<br>
     * @return New point ptC coordinates or null if any argument is invalid
     */
    public static Point2D.Double getCollinearPointWithRatio(Point2D ptA,
            Point2D ptB, double k) {
        if (ptA != null && ptB != null) {
            return new Point2D.Double(
                    ptB.getX() * k + ptA.getX() * (1 - k), ptB.getY() * k
                            + ptA.getY() * (1 - k));
        }
        return null;
    }
}

Related Tutorials