Return-Path: Delivered-To: apmail-commons-commits-archive@locus.apache.org Received: (qmail 49417 invoked from network); 8 Feb 2008 12:36:57 -0000 Received: from hermes.apache.org (HELO mail.apache.org) (140.211.11.2) by minotaur.apache.org with SMTP; 8 Feb 2008 12:36:57 -0000 Received: (qmail 22180 invoked by uid 500); 8 Feb 2008 12:36:48 -0000 Delivered-To: apmail-commons-commits-archive@commons.apache.org Received: (qmail 22099 invoked by uid 500); 8 Feb 2008 12:36:48 -0000 Mailing-List: contact commits-help@commons.apache.org; run by ezmlm Precedence: bulk List-Help: List-Unsubscribe: List-Post: List-Id: Reply-To: dev@commons.apache.org Delivered-To: mailing list commits@commons.apache.org Received: (qmail 22090 invoked by uid 99); 8 Feb 2008 12:36:48 -0000 Received: from athena.apache.org (HELO athena.apache.org) (140.211.11.136) by apache.org (qpsmtpd/0.29) with ESMTP; Fri, 08 Feb 2008 04:36:48 -0800 X-ASF-Spam-Status: No, hits=-2000.0 required=10.0 tests=ALL_TRUSTED X-Spam-Check-By: apache.org Received: from [140.211.11.3] (HELO eris.apache.org) (140.211.11.3) by apache.org (qpsmtpd/0.29) with ESMTP; Fri, 08 Feb 2008 12:36:27 +0000 Received: by eris.apache.org (Postfix, from userid 65534) id C402B1A9832; Fri, 8 Feb 2008 04:36:34 -0800 (PST) Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit Subject: svn commit: r619861 - in /commons/proper/math/trunk/src: java/org/apache/commons/math/util/MathUtils.java site/xdoc/changes.xml test/org/apache/commons/math/util/MathUtilsTest.java Date: Fri, 08 Feb 2008 12:36:34 -0000 To: commits@commons.apache.org From: luc@apache.org X-Mailer: svnmailer-1.0.8 Message-Id: <20080208123634.C402B1A9832@eris.apache.org> X-Virus-Checked: Checked by ClamAV on apache.org Author: luc Date: Fri Feb 8 04:36:33 2008 New Revision: 619861 URL: http://svn.apache.org/viewvc?rev=619861&view=rev Log: added an angle normalization method to MathUtils Modified: commons/proper/math/trunk/src/java/org/apache/commons/math/util/MathUtils.java commons/proper/math/trunk/src/site/xdoc/changes.xml commons/proper/math/trunk/src/test/org/apache/commons/math/util/MathUtilsTest.java Modified: commons/proper/math/trunk/src/java/org/apache/commons/math/util/MathUtils.java URL: http://svn.apache.org/viewvc/commons/proper/math/trunk/src/java/org/apache/commons/math/util/MathUtils.java?rev=619861&r1=619860&r2=619861&view=diff ============================================================================== --- commons/proper/math/trunk/src/java/org/apache/commons/math/util/MathUtils.java (original) +++ commons/proper/math/trunk/src/java/org/apache/commons/math/util/MathUtils.java Fri Feb 8 04:36:33 2008 @@ -43,6 +43,9 @@ /** 0.0 cast as a short. */ private static final short ZS = (short)0; + /** 2 π. */ + private static final double TWO_PI = 2 * Math.PI; + /** * Private Constructor */ @@ -706,6 +709,28 @@ } /** + * Normalize an angle in a 2&pi wide interval around a center value. + *

This method has three main uses:

+ *
    + *
  • normalize an angle between 0 and 2π:
    + * a = MathUtils.normalizeAngle(a, Math.PI);
  • + *
  • normalize an angle between -π and +π
    + * a = MathUtils.normalizeAngle(a, 0.0);
  • + *
  • compute the angle between two defining angular positions:
    + * angle = MathUtils.normalizeAngle(end, start) - start;
  • + *
+ *

Note that due to numerical accuracy and since π cannot be represented + * exactly, the result interval is closed, it cannot be half-closed + * as would be more satisfactory in a purely mathematical view.

+ * @param a angle to normalize + * @param center center of the desired 2π interval for the result + * @return a-2kπ with integer k and center-π <= a-2kπ <= center+π + */ + public static double normalizeAngle(double a, double center) { + return a - TWO_PI * Math.floor((a + Math.PI - center) / TWO_PI); + } + + /** * Round the given value to the specified number of decimal places. The * value is rounded using the {@link BigDecimal#ROUND_HALF_UP} method. * @@ -1007,4 +1032,5 @@ } return ret; } + } Modified: commons/proper/math/trunk/src/site/xdoc/changes.xml URL: http://svn.apache.org/viewvc/commons/proper/math/trunk/src/site/xdoc/changes.xml?rev=619861&r1=619860&r2=619861&view=diff ============================================================================== --- commons/proper/math/trunk/src/site/xdoc/changes.xml (original) +++ commons/proper/math/trunk/src/site/xdoc/changes.xml Fri Feb 8 04:36:33 2008 @@ -146,6 +146,10 @@ Added a equals and hash methods in MathUtils to check for double arrays + + Added an angle normalization method in MathUtils to force angles in some + user-defined interval +