Java Geometry Algorithm arcThroughThreePoints(float x1, float y1, float x2, float y2, float x3, float y3, boolean clockwise)

Here you can find the source of arcThroughThreePoints(float x1, float y1, float x2, float y2, float x3, float y3, boolean clockwise)

Description

arc Through Three Points

License

Apache License

Declaration

public static Arc2D.Float arcThroughThreePoints(float x1, float y1, float x2, float y2, float x3, float y3,
            boolean clockwise) 

Method Source Code

//package com.java2s;
/*/*from w w w. j a  v a  2  s  .c  om*/
 * Copyright 2005 Tom Gibara
 * 
 * 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.
 * 
 */

import java.awt.geom.Arc2D;

public class Main {
    private static final float fltTwoPi = (float) (Math.PI * 2.0);
    private static final float fltRadToDeg = (float) (180 / Math.PI);

    public static Arc2D.Float arcThroughThreePoints(float x1, float y1, float x2, float y2, float x3, float y3,
            boolean clockwise) {
        float d = 2 * (x1 - x3) * (y3 - y2) + 2 * (x2 - x3) * (y1 - y3);
        float m1 = x1 * x1 - x3 * x3 + y1 * y1 - y3 * y3;
        float m2 = x3 * x3 - x2 * x2 + y3 * y3 - y2 * y2;
        float nx = m1 * (y3 - y2) + m2 * (y3 - y1);
        float ny = m1 * (x2 - x3) + m2 * (x1 - x3);
        float cx = nx / d;
        float cy = ny / d;
        float r = (float) Math.hypot(x1 - cx, y1 - cy);
        float a1 = (float) Math.atan2(-(y1 - cy), x1 - cx);
        float a2 = (float) Math.atan2(-(y3 - cy), x3 - cx);
        float extent = a2 - a1;
        if (!clockwise && extent < 0f) {
            extent = fltTwoPi + extent;
        } else if (clockwise && extent > 0f) {
            extent = extent - fltTwoPi;
        }
        return new Arc2D.Float(cx - r, cy - r, r * 2, r * 2, a1 * fltRadToDeg, extent * fltRadToDeg, Arc2D.OPEN);
    }
}

Related

  1. absoluteBearing(Point2D source, Point2D target)
  2. absoluteBearing(Point2D.Double sourceLocation, Point2D.Double target)
  3. applyCoG(Point2D.Double[] points, Point2D.Double cog)
  4. applyDynamism(final Point[] spline, final int msForMove, final int msPerMove)
  5. applyMidPointApprox(CubicCurve2D cubic, QuadCurve2D quad)
  6. area2(Point2D a, Point2D b, Point2D c)
  7. AreAlign(Point2D.Double pivot, Point2D.Double a, Point2D.Double b)
  8. areDifferentAnchorPoints(Point2D p1, Point2D p2)
  9. areLocationsClose(Point2D point1, Point2D point2)