View Javadoc

1   /*
2    * @(#)Circle.java Jun 13, 2004 Copyright (c) 2002-2005 Delft University of Technology Jaffalaan 5, 2628 BX
3    * Delft, the Netherlands. All rights reserved. This software is proprietary information of Delft University
4    * of Technology The code is published under the Lesser General Public License
5    */
6   package nl.tudelft.simulation.language.d2;
7   
8   import java.awt.geom.Point2D;
9   
10  /***
11   * <p>
12   * (c) copyright 2002-2005 <a href="http://www.simulation.tudelft.nl">Delft University of Technology </a>, the
13   * Netherlands.
14   * <p>
15   * See for project information <a
16   * href="http://www.simulation.tudelft.nl/dsol/language">www.simulation.tudelft.nl/language </a> <br>
17   * License of use: <a href="http://www.gnu.org/copyleft/lesser.html">Lesser General Public License (LGPL)
18   * </a>, no warranty
19   * 
20   * @version $Revision: 1.7 $ $Date: 2005/07/04 12:21:25 $
21   * @author <a href="mailto:a.verbraeck@tbm.tudelft.nl">Alexander Verbraeck </a>
22   */
23  public final class Circle
24  {
25      /***
26       * constructs a new Circle.
27       */
28      private Circle()
29      {
30          super();
31          // unreachable code
32      }
33  
34      /***
35       * Elegant intersection algorithm from http://astronomy.swin.edu.au/~pbourke/geometry/2circle/.
36       * 
37       * @param center0
38       *            the center of the first circle
39       * @param radius0
40       *            the radius of the first circle
41       * @param center1
42       *            the center of the second circle
43       * @param radius1
44       *            the radius of the second circle
45       * @return the intersection
46       */
47      public static Point2D[] intersection(final Point2D center0, final double radius0, final Point2D center1,
48              final double radius1)
49      {
50          double distance = center0.distance(center1);
51          double x0 = center0.getX();
52          double x1 = center1.getX();
53          double y0 = center0.getY();
54          double y1 = center1.getY();
55          Point2D[] result;
56          if ((distance > radius0 + radius1) || (distance < Math.abs(radius0 - radius1)))
57          {
58              return new Point2D.Double[0];
59          }
60          double a = (radius0 * radius0 - radius1 * radius1 + distance * distance) / (2 * distance);
61          double h = Math.sqrt(radius0 * radius0 - a * a);
62          double x2 = x0 + ((a / distance) * (x1 - x0));
63          double y2 = y0 + ((a / distance) * (y1 - y0));
64          if (distance == radius0 + radius1)
65          {
66              result = new Point2D.Double[1];
67              result[0] = new Point2D.Double(x2, y2);
68          }
69          else
70          {
71              result = new Point2D.Double[2];
72              result[0] = new Point2D.Double(x2 + h * (y1 - y0) / distance, y2 - h * (x1 - x0) / distance);
73              result[1] = new Point2D.Double(x2 - h * (y1 - y0) / distance, y2 + h * (x1 - x0) / distance);
74          }
75          return result;
76      }
77  }