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