-
Notifications
You must be signed in to change notification settings - Fork 0
/
MathUtils.java
69 lines (59 loc) · 1.95 KB
/
MathUtils.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
import core.Vector;
/**
* This class only provides a collection of utilities required
* for easy vector handling in bots. An instantiation is not
* required because all methods are static.
*
* @author Oskar Kirmis <[email protected]>
*/
final public class MathUtils {
public static final double DEFAULT_PARALLEL_THRESHOLD = Math.PI / 4;
/**
* Get the angle from a given vector (polar form).
*
* @param v The vector to get the angle from
* @return Angle of the vector
*/
public static double angleFromVector( Vector v )
{
assert v != null : "Null pointer given";
// Special situation handling: +/- 90
if( v.x == 0 )
return v.y > 0 ? Math.PI / 2.0 : -Math.PI / 2.0;
// Normal handling
return Math.atan( v.y / v.x );
}
/**
* Get the angle the projection of an object with the given object radius
* produces on a given distance (circle radius).
*
* @param circleRadius Distance of the object
* @param objectRadius The radius of the object to project
* @return Angle a projection of this object on a circle takes
*/
public static double angleOnCircle( double circleRadius, double objectRadius )
{
// Special situation handling: +/- 90
if( circleRadius == 0 )
return Math.PI / 2.0;
// Normal handling
return Math.atan( 2.0 * objectRadius / circleRadius );
}
/**
* Find out if two vectors are parallel. This is decided by comparing
* the angles of the vectors (smaller PI/4)
*
* @param v0 First vector for comparison
* @param v1 Second vector to compare to the first one
* @return true, if the difference of the angles is smaller MathUtils.DEFAULT_PARALLEL_THRESHOLD
*/
public static boolean isVectorParallel( Vector v0, Vector v1 )
{
assert v0 != null && v1 != null : "Null pointer given";
// Get difference of the vectors angles
double d = Math.abs(
MathUtils.angleFromVector( v0 ) - MathUtils.angleFromVector( v1 )
);
return d < MathUtils.DEFAULT_PARALLEL_THRESHOLD;
}
}