Commit aec8d03f authored by Nico Mack's avatar Nico Mack

Initial commit of file

parent 5e1a8dfe
/**
* Copyright Luxembourg Institute of Science and Technology, 2017. All rights reserved.
*
* This file is part of TULIP.
*
* TULIP 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 3 of the
* License.
*
* TULIP 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.
*
* You should have received a copy of the GNU Lesser General Public License along with TULIP. If
* not, see <http://www.gnu.org/licenses/lgpl-3.0.html>.
*/
package lu.list.itis.dkd.tui.utility;
/**
* @author mack
* @since [major].[minor]
* @version [major].[minor].[micro]
*/
public class AngleUtils {
// ***************************************************************************
// * Constants *
// ***************************************************************************
protected static final double PI_HALF = Math.PI / 2;
protected static final double THREE_PI_HALF = 1.5 * Math.PI;
protected static final double TWO_PI = 2 * Math.PI;
protected static final double THREE_SIXTY = 360d;
protected static final double ONE_EIGHTY = 180d;
// ---------------------------------------------------------------------------
// ***************************************************************************
// * Constructor(s)
// ***************************************************************************
// ---------------------------------------------------------------------------
private AngleUtils() {
}
// ---------------------------------------------------------------------------
// ***************************************************************************
// * Class Body
// ***************************************************************************
// ---------------------------------------------------------------------------
/**
* @param rawAngle
* @return
*/
public static double moduloTwoPi(double rawAngle) {
double angle = rawAngle % TWO_PI;
if (angle < 0)
angle += TWO_PI;
return angle;
}
// ---------------------------------------------------------------------------
/**
* @param rawAngle
* @return
*/
// ---------------------------------------------------------------------------
public static double moduloThreeSixty(double rawAngle) {
double angle = rawAngle % THREE_SIXTY;
if (angle < 0)
angle += THREE_SIXTY;
return angle;
}
// ---------------------------------------------------------------------------
/**
* Brings the specified raw angle in the range between -180 and + 180 degrees. This operation is
* useful for checking angular ranges crossing the 360/0 line.
*
* @param rawAngle
* specifies the raw angle to wrap.
* @return the transformed rawAngle alpha so that the condition -180 <= alpha < 180 is met.
*
*/
// ---------------------------------------------------------------------------
public static double wrap(double rawAngle) {
return ((rawAngle + 540) % THREE_SIXTY) - ONE_EIGHTY;
}
}
/**
* Copyright Luxembourg Institute of Science and Technology, 2017. All rights reserved.
*
* This file is part of TULIP.
*
* TULIP 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 3 of the
* License.
*
* TULIP 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.
*
* You should have received a copy of the GNU Lesser General Public License along with TULIP. If
* not, see <http://www.gnu.org/licenses/lgpl-3.0.html>.
*/
package lu.list.itis.dkd.tui.utility;
import static org.junit.Assert.assertEquals;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
/**
* @author mack
* @since [major].[minor]
* @version [major].[minor].[micro]
*/
public class PolarCoordinateHelperTest {
/**
* @throws java.lang.Exception
*/
@Before
public void setUp() throws Exception {}
/**
* @throws java.lang.Exception
*/
@After
public void tearDown() throws Exception {}
@Test
public void testCarthesianToPolar() {
Point origin = new Point(100, 100, 0, ScreenCoordinates.class);
Point q1 = new Point(150, 50, 0, ScreenCoordinates.class);
Point q2 = new Point(50, 50, 0, ScreenCoordinates.class);
Point q3 = new Point(50, 150, 0, ScreenCoordinates.class);
Point q4 = new Point(150, 150, 0, ScreenCoordinates.class);
Point p1 = PolarCoordinateHelper.carthesianToPolar(origin, q1);
assertEquals(p1.getX(), 50 * Math.sqrt(2), 0.001);
assertEquals(p1.getY(), 50 * Math.sqrt(2), 0.001);
assertEquals(p1.getAngle(), Math.PI / 4, 0.001);
Point p2 = PolarCoordinateHelper.carthesianToPolar(origin, q2);
assertEquals(p2.getX(), 50 * Math.sqrt(2), 0.001);
assertEquals(p2.getY(), 50 * Math.sqrt(2), 0.001);
assertEquals(p2.getAngle(), 3 * Math.PI / 4, 0.001);
Point p3 = PolarCoordinateHelper.carthesianToPolar(origin, q3);
assertEquals(p3.getX(), 50 * Math.sqrt(2), 0.001);
assertEquals(p3.getY(), 50 * Math.sqrt(2), 0.001);
assertEquals(p3.getAngle(), 5 * Math.PI / 4, 0.001);
Point p4 = PolarCoordinateHelper.carthesianToPolar(origin, q4);
assertEquals(p4.getX(), 50 * Math.sqrt(2), 0.001);
assertEquals(p4.getY(), 50 * Math.sqrt(2), 0.001);
assertEquals(p4.getAngle(), 7 * Math.PI / 4, 0.001);
}
}
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment