forked from Anuken/Arc
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAngles.java
More file actions
157 lines (127 loc) · 5.01 KB
/
Angles.java
File metadata and controls
157 lines (127 loc) · 5.01 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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
package arc.math;
import arc.func.*;
import arc.math.geom.*;
public class Angles{
private static final Rand rand = new Rand();
private static final Vec2 rv = new Vec2();
public static float forwardDistance(float angle1, float angle2){
return Math.abs(angle1 - angle2);
}
public static float backwardDistance(float angle1, float angle2){
return 360 - Math.abs(angle1 - angle2);
}
public static boolean within(float a, float b, float margin){
return angleDist(a, b) <= margin;
}
public static float angleDist(float a, float b){
a = Mathf.mod(a, 360f);
b = Mathf.mod(b, 360f);
return Math.min((a - b) < 0 ? a - b + 360 : a - b, (b - a) < 0 ? b - a + 360 : b - a);
}
public static boolean near(float a, float b, float range){
return angleDist(a, b) < range;
}
public static float clampRange(float angle, float dest, float range){
float dst = angleDist(angle, dest);
return dst <= range ? angle : moveToward(angle, dest, dst - range);
}
public static float moveToward(float angle, float to, float speed){
if(Math.abs(angleDist(angle, to)) < speed) return to;
angle = Mathf.mod(angle, 360f);
to = Mathf.mod(to, 360f);
if(angle > to == backwardDistance(angle, to) > forwardDistance(angle, to)){
angle -= speed;
}else{
angle += speed;
}
return angle;
}
public static float angle(float x, float y){
return angle(0, 0, x, y);
}
public static float angle(float x, float y, float x2, float y2){
float ang = Mathf.atan2(x2 - x, y2 - y) * Mathf.radDeg;
if(ang < 0) ang += 360f;
return ang;
}
public static float angleRad(float x, float y, float x2, float y2){
return Mathf.atan2(x2 - x, y2 - y);
}
public static float trnsx(float angle, float len){
return len * Mathf.cosDeg(angle);
}
public static float trnsy(float angle, float len){
return len * Mathf.sinDeg(angle);
}
public static float trnsx(float angle, float x, float y){
return rv.set(x, y).rotate(angle).x;
}
public static float trnsy(float angle, float x, float y){
return rv.set(x, y).rotate(angle).y;
}
public static void circleVectors(int points, float length, Floatc2 pos){
for(int i = 0; i < points; i++){
float f = i * 360f / points;
pos.get(trnsx(f, length), trnsy(f, length));
}
}
public static void circleVectors(int points, float length, float offset, Floatc2 pos){
for(int i = 0; i < points; i++){
float f = i * 360f / points + offset;
pos.get(trnsx(f, length), trnsy(f, length));
}
}
public static void randVectors(long seed, int amount, float length, Floatc2 cons){
rand.setSeed(seed);
for(int i = 0; i < amount; i++){
rv.trns(rand.random(360f), length);
cons.get(rv.x, rv.y);
}
}
public static void randLenVectors(long seed, int amount, float length, Floatc2 cons){
rand.setSeed(seed);
for(int i = 0; i < amount; i++){
rv.trns(rand.random(360f), rand.random(length));
cons.get(rv.x, rv.y);
}
}
public static void randLenVectors(long seed, int amount, float minLength, float length, Floatc2 cons){
rand.setSeed(seed);
for(int i = 0; i < amount; i++){
rv.trns(rand.random(360f), minLength + rand.random(length));
cons.get(rv.x, rv.y);
}
}
public static void randLenVectors(long seed, int amount, float length, float angle, float range, Floatc2 cons){
rand.setSeed(seed);
for(int i = 0; i < amount; i++){
rv.trns(angle + rand.range(range), rand.random(length));
cons.get(rv.x, rv.y);
}
}
public static void randLenVectors(long seed, int amount, float length, float angle, float range, float spread, Floatc2 cons){
rand.setSeed(seed);
for(int i = 0; i < amount; i++){
rv.trns(angle + rand.range(range), rand.random(length));
cons.get(rv.x + rand.range(spread), rv.y + rand.range(spread));
}
}
public static void randLenVectors(long seed, float fin, int amount, float length, ParticleConsumer cons){
rand.setSeed(seed);
for(int i = 0; i < amount; i++){
float l = rand.nextFloat();
rv.trns(rand.random(360f), length * l * fin);
cons.accept(rv.x, rv.y, fin * l, (1f - fin) * l);
}
}
public static void randLenVectors(long seed, float fin, int amount, float length, float angle, float range, ParticleConsumer cons){
rand.setSeed(seed);
for(int i = 0; i < amount; i++){
rv.trns(angle + rand.range(range), rand.random(length * fin));
cons.accept(rv.x, rv.y, fin * (rand.nextFloat()), 0f);
}
}
public interface ParticleConsumer{
void accept(float x, float y, float fin, float fout);
}
}