get Parallel Line - Java java.lang

Java examples for java.lang:Math Geometry Line

Description

get Parallel Line

Demo Code


//package com.java2s;

import java.awt.geom.Line2D;

import java.awt.geom.Point2D;

public class Main {
    /**/* ww w . ja v  a2 s  . co m*/
     * 
     * @param ptA
     *            Start of line segment
     * @param ptB
     *            End of line segment
     * @param dist
     *            Distance from AB line to the parallel CD line <br>
     *            If >0 angle between AB and AC is +90? <br>
     *            If <0 angle between AB and AC is -+90?
     * @return
     */
    public static Line2D getParallelLine(Point2D ptA, Point2D ptB,
            double dist) {
        if (ptA == null || ptB == null || ptA.equals(ptB)) {
            return null;
        }

        double distAB2 = ptA.distanceSq(ptB);
        double ux = -(ptB.getY() - ptA.getY()) / distAB2;
        double uy = (ptB.getX() - ptA.getX()) / distAB2;

        Point2D ptC = new Point2D.Double(ptA.getX() + dist * ux, ptA.getY()
                + dist * uy);
        Point2D ptD = new Point2D.Double(ptB.getX() + dist * ux, ptB.getY()
                + dist * uy);

        return new Line2D.Double(ptC, ptD);
    }
}

Related Tutorials