Java Geometry Algorithm fitCircle(final Point2D P1, final Point2D P2, final Point2D P3)

Here you can find the source of fitCircle(final Point2D P1, final Point2D P2, final Point2D P3)

Description

fit Circle

License

Open Source License

Declaration

public static Ellipse2D fitCircle(final Point2D P1, final Point2D P2, final Point2D P3) 

Method Source Code

//package com.java2s;
/*//from  w w  w.  ja  v a 2  s  .  c  om
 *    GeoTools - The Open Source Java GIS Toolkit
 *    http://geotools.org
 * 
 *    (C) 2001-2008, Open Source Geospatial Foundation (OSGeo)
 *
 *    This library is free software; you can redistribute it and/or
 *    modify it under the terms of the GNU Lesser General Public
 *    License as published by the Free Software Foundation;
 *    version 2.1 of the License.
 *
 *    This 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
 *    Lesser General Public License for more details.
 */

import java.awt.geom.Ellipse2D;

import java.awt.geom.Point2D;

public class Main {

    public static Ellipse2D fitCircle(final Point2D P1, final Point2D P2, final Point2D P3) {
        final Point2D center = circleCentre(P1.getX(), P1.getY(), P2.getX(), P2.getY(), P3.getX(), P3.getY());
        final double radius = center.distance(P2);
        return new Ellipse2D.Double(center.getX() - radius, center.getY() - radius, 2 * radius, 2 * radius);
    }

    public static Point2D circleCentre(double x1, double y1, double x2, double y2, double x3, double y3) {
        x2 -= x1;
        x3 -= x1;
        y2 -= y1;
        y3 -= y1;
        final double sq2 = (x2 * x2 + y2 * y2);
        final double sq3 = (x3 * x3 + y3 * y3);
        final double x = (y2 * sq3 - y3 * sq2) / (y2 * x3 - y3 * x2);
        return new Point2D.Double(x1 + 0.5 * x, y1 + 0.5 * (sq2 - x * x2) / y2);
    }
}

Related

  1. findDistancedPoint(double t, Point2D sp, Point2D c1, Point2D c2, Point2D ep)
  2. findIntersect(Point2D p1, Point2D p2, Point2D p3, Point2D p4)
  3. findIntersection(Point2D.Double p1, Point2D.Double p2, Point2D.Double p3, Point2D.Double p4)
  4. findLineSegmentIntersection(Line2D one, Line2D two, Point2D intersection)
  5. findMiddlePoint(Point2D p1, Point2D p2)
  6. forceMouseMove(Point pos)
  7. generateLine(Point2D.Double point, double length, double angle)
  8. generateLookAtTag(ArrayList geoCoords, ArrayList modsAM)
  9. generatePoint(Shape region)