-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCell.java
More file actions
53 lines (46 loc) · 1.54 KB
/
Cell.java
File metadata and controls
53 lines (46 loc) · 1.54 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
import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
import java.awt.Color;
import java.util.List;
/**
* Area of the map that indicates distance from radio towers.
*/
public class Cell extends Actor
{
public Cell() {
super();
}
/**
* Set the color of the cell based on the environment.
*/
public void act()
{
List<Tower> towers = getWorld().getObjects(Tower.class);
double dist = 0;
for( Tower t: towers) {
dist = dist + findDistance(t);
}
// TODO Set color based on signal strength, not just the distance
// Set color based on 'dist'
GreenfootImage pic = new GreenfootImage(16, 16);
int red = (int)scale(dist/towers.size());
pic.setColor(new Color(red, 0,0));
pic.fill();
setImage(pic);
}
/** Find the hypotenuse of the right triangle with the given side lengths. */
private double findDistance(double a, double b) {
return Math.sqrt((a*a) + (b*b));
}
/** Find distance from this object to the given Actor. */
private double findDistance(Actor other) {
double a, b;
a = other.getX() - this.getX();
b = other.getY() - this.getY();
return findDistance(a,b);
}
/** Scale from 0-46 range to 255-0 for color calculation. */
private double scale(double value) {
// TODO use world size to scale
return 255- (value / 46 * 255);
}
}