-
Notifications
You must be signed in to change notification settings - Fork 1
/
Utils.pde
71 lines (57 loc) · 1.77 KB
/
Utils.pde
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
/*
* General utilities
*
* A JavaScript version of this class also exists.
*/
public static class Utils{
private static int id = -1;
public static int nextID(){
return id++;
}
/*
* We use Math.random() instead of Processing's random() to prevent
* having to make this class a singleton and take a Papplet. That code
* would be unnecessarily complex.
*/
public static int getRandomInt(int minVal, int maxVal) {
float scaleFloat = (float) Math.random();
return minVal + (int) (scaleFloat * (maxVal - minVal + 1));
}
/*
*/
public static boolean circleCircleIntersection(PVector circle1Pos, float circle1Radius, PVector circle2Pos, float circle2Radius){
PVector result = circle1Pos;
result.sub(circle2Pos);
float distanceBetween = result.mag();
return distanceBetween < (circle1Radius/2.0 + circle2Radius/2.0);
}
public static float Lerp(float a, float b, float p){
return a * (1 - p) + (b * p);
}
/*
* This is here simply to provide a means for us to call a custom method for the JS version.
*/
public static int charCodeAt(char ch){
return ch;
}
/*
A simple cast in Java is sufficient, but in JavaScript, we'll need to use floor.
*/
public static int floatToInt(float f){
return (int)f;
}
/**
* Mostly used for adding zeros to a number in string format, but general enough to be
* used for other purposes.
*/
public static String prependStringWithString(String baseString, String prefix, int newStrLength){
if(newStrLength <= baseString.length()){
return baseString;
}
int zerosToAdd = newStrLength - baseString.length();
for(int i = 0; i < zerosToAdd; i++){
baseString = prefix + baseString;
}
return baseString;
}
}