Returns angle between the line specified by points and y=0 line. - Java java.lang

Java examples for java.lang:Math Geometry Line

Description

Returns angle between the line specified by points and y=0 line.

Demo Code

/*//  w  w w.j a  v  a2 s  .c o  m
 * This file is part of WebLookAndFeel library.
 *
 * WebLookAndFeel library 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.
 *
 * WebLookAndFeel library 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 WebLookAndFeel library.  If not, see <http://www.gnu.org/licenses/>.
 */
import java.awt.Dimension;
import java.awt.Point;
import java.awt.Rectangle;
import java.util.List;

public class Main{
    /**
     * Returns angle between the line specified by points and y=0 line.
     *
     * @param p1
     *            first line point
     * @param p2
     *            second line point
     * @return angle between the line specified by points and y=0 line
     */
    public static double getAngle(final Point p1, final Point p2) {
        return getAngle(p1.x, p1.y, p2.x, p2.y);
    }
    /**
     * Returns angle between the line specified by points and y=0 line.
     *
     * @param x1
     *            first point X coordinate
     * @param y1
     *            first point Y coordinate
     * @param x2
     *            second point X coordinate
     * @param y2
     *            second point Y coordinate
     * @return angle between the line specified by points and y=0 line
     */
    public static double getAngle(final int x1, final int y1, final int x2,
            final int y2) {
        final double angle = Math
                .asin((y2 - y1)
                        / Math.sqrt(MathUtils.sqr(x2 - x1)
                                + MathUtils.sqr(y2 - y1)));
        return x1 > x2 ? -angle - Math.PI : angle;
    }
}

Related Tutorials