Here you can find the source of fitCircle(final Point2D P1, final Point2D P2, final Point2D P3)
public static Ellipse2D fitCircle(final Point2D P1, final Point2D P2, final Point2D P3)
//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); } }