View Javadoc

1   /*
2    * @(#)Angle.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   * The Angle class presents a number of mathematical utility functions on the angle.
12   * <p>
13   * (c) copyright 2002-2005 <a href="http://www.simulation.tudelft.nl">Delft University of Technology </a>, the
14   * Netherlands.
15   * <p>
16   * See for project information <a
17   * href="http://www.simulation.tudelft.nl/dsol/language">www.simulation.tudelft.nl/language </a> <br>
18   * License of use: <a href="http://www.gnu.org/copyleft/lesser.html">Lesser General Public License (LGPL)
19   * </a>, no warranty
20   * 
21   * @version $Revision: 1.7 $ $Date: 2005/07/04 12:21:25 $
22   * @author <a href="mailto:a.verbraeck@tbm.tudelft.nl">Alexander Verbraeck </a>
23   */
24  public final class Angle
25  {
26      /***
27       * constructs a new Angle.
28       */
29      private Angle()
30      {
31          super();
32          // unreachable code
33      }
34  
35      /***
36       * Normalize an angle between 0 and 2*pi.
37       * 
38       * @param angle
39       *            the angle to normalize
40       * @return normalized angle
41       */
42      public static double normalize2Pi(final double angle)
43      {
44          double result = angle + 2.0d * Math.PI;
45          double times = Math.floor(result / (2.0d * Math.PI));
46          result -= times * 2.0d * Math.PI;
47          return result;
48      }
49  
50      /***
51       * Normalize an angle between -pi and +pi.
52       * 
53       * @param angle
54       *            the angle to normalize
55       * @return normalized angle
56       */
57      public static double normalizePi(final double angle)
58      {
59          double result = angle + 2.0d * Math.PI;
60          double times = Math.floor((result + Math.PI) / (2.0d * Math.PI));
61          result -= times * 2.0d * Math.PI;
62          return result;
63      }
64  
65      /***
66       * Return the 2-pi normalized angle when making an arc from p0 to p1.
67       * 
68       * @param p0
69       *            first point
70       * @param p1
71       *            second point
72       * @return the normalized angle
73       */
74      public static double angle(final Point2D p0, final Point2D p1)
75      {
76          return normalize2Pi(Math.atan2(p1.getY() - p0.getY(), p1.getX() - p0.getX()));
77      }
78  }