-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAgent.java
73 lines (63 loc) · 2.51 KB
/
Agent.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
70
71
72
73
import java.awt.*;
import java.util.Random;
public class Agent {
public float x, y;
public float angle;
private Random r = new Random();
public Agent(int x, int y, int angle){
this.x = x;
this.y = y;
this.angle = angle;
}
private void placePheromone(Picture p){
try {
int color = Window.AGENT_STRENGTH;
p.setColorAt((int) x, (int) y, new Color(color, color, color));
/*p.setColorAt((int) x, (int) y+1, new Color(color, color, color));
p.setColorAt((int) x+1, (int) y, new Color(color, color, color));
p.setColorAt((int) x+1, (int) y+1, new Color(color, color, color));*/
}catch (IndexOutOfBoundsException ie){}
}
private void move(float speed, Picture p){
float xN = (float) Math.sin(2 * Math.PI * (angle / 360));
float yN = (float) Math.cos(2 * Math.PI * (angle / 360));
float newX = x + xN * speed;
float newY = y + yN * speed;
if (newX < 0 || newX >= Window.width || newY < 0 || newY >= Window.height){
newX = (float) Math.min(Window.width - 0.01, Math.max(0, newX));
newY = (float) Math.min(Window.height - 0.01, Math.max(0, newY));
angle = new Random().nextInt(360);
}
x = newX;
y = newY;
if( Window.FOLLOW) {
int fl = getSensoryData(45, speed, p);
int f = getSensoryData(0, speed, p);
int fr = getSensoryData(-45, speed, p);
if (f > fl && f > fr) {
return;
} else if (f < fl && f < fr) {
angle += (r.nextDouble() - 0.5) * Window.TURN_SPEED;
} else if (fl < fr) {
angle -= r.nextDouble() - Window.TURN_SPEED;
} else if (fr < fl) {
angle += r.nextDouble() - Window.TURN_SPEED;
}
}
}
private int getSensoryData(int turn, float speed, Picture p){
float xN = (float) Math.sin(2 * Math.PI * ((angle-turn) / 360));
float yN = (float) Math.cos(2 * Math.PI * ((angle-turn) / 360));
float newX = x + xN * speed * Window.RANGE;
float newY = y + yN * speed * Window.RANGE;
try {
return p.getColorAt((int) newX, (int) newY).getRed();
}catch (IndexOutOfBoundsException ie){
return 0;
}
}
public void update(Picture p, float speed){
move(speed, p);
placePheromone(p);
}
}