get Collinear Point With Length - Java java.lang

Java examples for java.lang:Math Geometry Line

Description

get Collinear Point With Length

Demo Code


//package com.java2s;

import java.awt.geom.Point2D;

public class Main {
    /**//from  www. j  ava2  s. c om
     * @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