-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMathUtils.java
More file actions
137 lines (127 loc) · 3.22 KB
/
MathUtils.java
File metadata and controls
137 lines (127 loc) · 3.22 KB
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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package org.scotsbots.robotbase.utils;
/**
* Has a collection of math functions and constants.
* <br> Originally adapted from Adambots code.
* @author Ben - Modified by Domenic
*/
public class MathUtils
{
// // MATH CONSTANTS
// --------------------------------------------------------
public static final double TWO_PI = Math.PI * 2;
public static final double PI_OVER_FOUR = Math.PI / 4;
public static final double PI_OVER_TWO = Math.PI / 2;
public static final double PI_OVER_180 = Math.PI / 180;
public static final double PI_UNDER_180 = 180 / Math.PI;
// // RANDOM VARIABLES
// ------------------------------------------------------
static long x = System.currentTimeMillis();
static long y = System.currentTimeMillis() + 2065101653;
static long z = System.currentTimeMillis() + 989498661;
static long w = System.currentTimeMillis() + 626378301;
static long v = System.currentTimeMillis() + 914577441;
// // ROUNDING
// --------------------------------------------------------------
/**
* Rounds a double to a certain number of decimal places.
*
* @param n
* The number to round.
* @param digit
* 10 to round to tens, 0.1 to round to tenths, etc.
* @return
*/
public static double roundTo(double n, double digit)
{
return Math.floor(n / digit + 0.5) * digit;
}
/**
* Rounds down a double to a certain number of decimal places.
*
* @param n
* The number to round.
* @param digit
* 10 to round to tens, 0.1 to round to tenths, etc.
* @return
*/
public static double floorTo(double n, double digit)
{
return Math.floor(n / digit) * digit;
}
/**
* Rounds up a double to a certain number of decimal places.
*
* @param n
* The number to round.
* @param digit
* .1 to round to tens, 10 to round to tenths.
* @return
*/
public static double ceilTo(double n, double digit)
{
return Math.ceil(n / digit) * digit;
}
// // SIGN
// ------------------------------------------------------------------
/**
*
* @param n
* @return- changes a double to it's opposite value
*/
public static double sign(double n)
{
return (n > 0.0) ? 1.0 : ((n < 0.0) ? -1.0 : 0.0);
}
/**
*
* @param n
* @return- changes a float to it's opposite value
*/
public static float sign(float n)
{
return (n > 0.0f) ? 1.0f : ((n < 0.0f) ? -1.0f : 0.0f);
}
/**
*
* @param n
* @return- changes an int to it's opposite value
*/
public static int sign(int n)
{
return (n > 0) ? 1 : ((n < 0) ? -1 : 0);
}
public static double exp(double x)
{
double s = 0;
double a = 1;
double[] r = { 1, 1, 2, 6, 24, 120, 720, 5040, 40320, 362880, 3628800 };
for (int i = 0; i < r.length; i++)
{
s += a / r[i];
a *= x;
}
return s;
}
// // RANDOM NUMBER GENERATOR
// -----------------------------------------------
/**
*
* @param high
* @return- a generated random number
*/
public static double rand(int high)
{
long t;
t = (x ^ (x >> 7));
x = y;
y = z;
z = w;
w = v;
v = (v ^ (v << 6)) ^ (t ^ (t << 13));
return (Math.abs((y + y + 1) * v) % (20000 + Math.PI)) / (20000 + Math.PI) * high;
}
}