-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathWorld.java
105 lines (100 loc) · 2.79 KB
/
World.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
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
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package ad.hoc.network;
import java.util.Random;
/**
*
* @author antonis
*/
public class World {
private static final int WORLD_W = 100;
private static final int WORLD_H = 100;
private static Node[] n;
private static int num;
private int max = 75;
public World(int num, int range){
max =range;
int i,x,y;
this.num=num;
Random gen = new Random();
//Initiliaze Nodes
n = new Node[num];
for (i=0;i<num;i++){
//place each node randomly
x= gen.nextInt(WORLD_W)*5;
y= gen.nextInt(WORLD_H)*5;
n[i] = new Node(x,y,i);
}
setNeighborhood();
for(i=0;i<num;i++){
if (Math.sqrt((n[i].getY()-250)*(n[i].getY()-250) + (n[i].getX()-250)*(n[i].getX()-250)) <=100)
n[i].TapAlgorithm(3);
else
n[i].TapAlgorithm(1);
}
}
public World(int num){
World.n = new Node[num];
int i,x,y;
this.num=num;
Random gen = new Random();
//Initiliaze Nodes
for (i=0;i<num;i++){
//place each node randomly
x= gen.nextInt(WORLD_W)*5;
y= gen.nextInt(WORLD_H)*5;
n[i] = new Node(x,y,i);
}
setNeighborhood();
}
/**
* Search if there is a node at x,y
* @param x coordinates
* @param y coordinates
* @return information about this node
*/
public String getNodeInfo(int x, int y){
int i;
x= x - x%5;
y= y - y%5;
for(i=0;i<num;i++){
if(n[i].getX() != x)
continue;
if (n[i].getY() != y)
continue;
return ("Node id: "+ i+"\n" + n[i].getInfo());
}
return "";
}
/**
*
* @param pos id
* @return Node object
*/
public Node getNode(int pos){
return n[pos];
}
private void setNeighborhood(){
int i,j;
int x,y,x2,y2;
double distance;
for(i=0;i<num;i++){
x = n[i].getX();
y = n[i].getY();
for(j=0;j<num;j++){
if(i==j)
continue;
x2 = n[j].getX();
y2 = n[j].getY();
distance = Math.sqrt((y2-y)*(y2-y) + (x2-x)*(x2-x));
//System.out.println(distance + " -- "+ (y2-y)*(y2-y) + (x2-x)*(x2-x) +"-"+ x +"-"+x2 +"-"+ y +"-"+ y2);
if (distance>max) // +++++++
continue;
n[i].addN(j, distance,x2,y2);
}
}
}
}