Creates and returns a line that is perpendicular to the specified line. - Java 2D Graphics

Java examples for 2D Graphics:Line

Description

Creates and returns a line that is perpendicular to the specified line.

Demo Code

/* --------------------------------------------------------===
 * Orson Charts : a 3D chart library for the Java(tm) platform
 * --------------------------------------------------------===
 * /*from  w w w . ja  va 2 s.c om*/
 * (C)opyright 2013-2016, by Object Refinery Limited.  All rights reserved.
 * 
 * http://www.object-refinery.com/orsoncharts/index.html
 * 
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 * 
 * [Oracle and Java are registered trademarks of Oracle and/or its affiliates. 
 * Other names may be trademarks of their respective owners.]
 * 
 * If you do not wish to be bound by the terms of the GPL, an alternative
 * commercial license can be purchased.  For details, please see visit the
 * Orson Charts home page:
 * 
 * http://www.object-refinery.com/orsoncharts/index.html
 * 
 */
//package com.java2s;

import java.awt.geom.Line2D;
import java.awt.geom.Point2D;

public class Main {
    /**
     * Creates and returns a line that is perpendicular to the specified line.
     *
     * @param line  the reference line ({@code null} not permitted).
     * @param b  the base point, expressed as a percentage along the length of 
     *     the reference line.
     * @param size  the size or length of the perpendicular line.
     * @param opposingPoint  an opposing point, to define which side of the 
     *     reference line the perpendicular line will extend ({@code null} 
     *     not permitted).
     *
     * @return The perpendicular line.
     */
    public static Line2D createPerpendicularLine(Line2D line, double b,
            double size, Point2D opposingPoint) {
        double dx = line.getX2() - line.getX1();
        double dy = line.getY2() - line.getY1();
        double length = Math.sqrt(dx * dx + dy * dy);
        double pdx = dy / length;
        double pdy = -dx / length;
        int ccw = line.relativeCCW(opposingPoint);
        Point2D pt1 = new Point2D.Double(line.getX1() + b * dx,
                line.getY1() + b * dy);
        Point2D pt2 = new Point2D.Double(pt1.getX() - ccw * size * pdx,
                pt1.getY() - ccw * size * pdy);
        return new Line2D.Double(pt1, pt2);
    }

    /**
     * Creates and returns a line that is perpendicular to the specified 
     * line.
     * 
     * @param line  the reference line ({@code null} not permitted).
     * @param pt1  a point on the reference line ({@code null} not 
     *     permitted).
     * @param size  the length of the new line.
     * @param opposingPoint  an opposing point, to define which side of the 
     *     reference line the perpendicular line will extend ({@code null} 
     *     not permitted).
     * 
     * @return The perpendicular line. 
     */
    public static Line2D createPerpendicularLine(Line2D line, Point2D pt1,
            double size, Point2D opposingPoint) {
        double dx = line.getX2() - line.getX1();
        double dy = line.getY2() - line.getY1();
        double length = Math.sqrt(dx * dx + dy * dy);
        double pdx = dy / length;
        double pdy = -dx / length;
        int ccw = line.relativeCCW(opposingPoint);
        Point2D pt2 = new Point2D.Double(pt1.getX() - ccw * size * pdx,
                pt1.getY() - ccw * size * pdy);
        return new Line2D.Double(pt1, pt2);
    }
}

Related Tutorials