Java Geometry Algorithm positiveAngleBetween3Points(Point2D.Double p1, Point2D.Double p2, Point2D.Double p3)

Here you can find the source of positiveAngleBetween3Points(Point2D.Double p1, Point2D.Double p2, Point2D.Double p3)

Description

Computes angle between three points.

License

Apache License

Parameter

Parameter Description
p1 first point
p2 second point
p3 third point

Return

the rotation angle, between 0 and 2pi; or NaN if any of the points coincide

Declaration

public static double positiveAngleBetween3Points(Point2D.Double p1, Point2D.Double p2, Point2D.Double p3) 

Method Source Code


//package com.java2s;
/*/*from   w  ww. j av a 2 s.  co m*/
 * #%L
 * BlaiseMath
 * --
 * Copyright (C) 2009 - 2015 Elisha Peterson
 * --
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *      http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 * #L%
 */

import java.awt.geom.Point2D;
import static java.lang.Math.*;

public class Main {
    /**
     * Computes angle between three points. In particular, this is the angle required to rotate
     * the vector [p2->p1] to the point where it is parallel to the vector [p2->p3].
     * Allows for points at infinity of the form (infinity, angle)
     * @param p1 first point
     * @param p2 second point
     * @param p3 third point
     * @return the rotation angle, between 0 and 2pi; or NaN if any of the points coincide
     */
    public static double positiveAngleBetween3Points(Point2D.Double p1, Point2D.Double p2, Point2D.Double p3) {
        if (isInfinite(p2)) {
            return 0;
        }
        double angleDiff = (isInfinite(p3) ? p3.y : positiveAngle(p3.x - p2.x, p3.y - p2.y))
                - (isInfinite(p1) ? p1.y : positiveAngle(p1.x - p2.x, p1.y - p2.y));
        if (angleDiff < 0) {
            angleDiff += 2 * Math.PI;
        }
        return angleDiff;
    }

    /**
     * Determines whether provided point is infinite.
     * @param point point to check
     * @return <code>true</code> if this represents an infinite point.
     */
    public static boolean isInfinite(Point2D.Double point) {
        return Double.isInfinite(point.x);
    }

    /**
     * Computes angle formed by vector with the x-axis.
     * @param x the x coord
     * @param y the y coord
     * @return an angle with range between 0 and 2pi
     */
    public static double positiveAngle(double x, double y) {
        double result = atan2(y, x);
        return result < 0 ? result + 2 * PI : result;
    }

    /**
     * Computes angle made by given vector with the x-axis
     * @param vec a vector
     * @return an angle with range between 0 and 2pi
     */
    public static double positiveAngle(Point2D.Double vec) {
        if (Double.isInfinite(vec.x))
            return vec.y < 0 ? vec.y + 2 * PI : vec.y;
        return positiveAngle(vec.x, vec.y);

    }
}

Related

  1. perpendicularScalarProjection(Point2D.Double projVec, Point2D.Double dir)
  2. plotBezier(GeneralPath path, @Nonnull Point2D p0, @Nonnull Point2D p1, @Nonnull Point2D p2, @Nonnull Point2D p3, int depth, double displacement)
  3. plus(Point point1, Point point2)
  4. polarPointAtInfinity(double angle)
  5. polarToPoint(double angle, double fx, double fy)
  6. preciseFrontBumperOffset( Point2D.Double sourceLocation, Point2D.Double botLocation)
  7. preprocess(Point2D pa, Point2D pb, Point2D pc)
  8. prodEscalar(Point2D p1, Point2D p2)
  9. project(Point2D sourceLocation, double angle, double length)