import java.util.*; import java.lang.*; public class wirelessNetwork { protected String[] name; // 1-d array to store the names of the vertices protected double[] posx; // 1-d array to store all the x-coordinates protected double[] posy; // 1-d array to store all the y-coordinates protected int numNodes; // number of nodes in the wireless network protected int numEdges; // number of edges in the wireless network protected myGraph graph; // the corresponding graph of the wireless network protected String[] visited; // 1-d array to store the previous vertices in the route protected int last; // index of the last element in the visited array protected int maxDegree; // Maximum degree in the wireless network // Default constructor. public wirelessNetwork( ) { wirelessNetwork network = new wirelessNetwork(10, 500); } // Constructor that takes two parameters public wirelessNetwork(double size, int n) { graph = new myGraph(n); // Construct the corresponding graph posx = new double[n]; posy = new double[n]; name = new String[n]; for (int i = 0; i < n; i++) { double x = size*Math.random(); // randomly generate the x-coordinate double y = size*Math.random(); // randomly generate the y-coordinate posx[i] = x; posy[i] = y; name[i] = "a"+i; // the name of a node is an "a" followed by a number graph.addVertex(name[i]); // add the vertex to the graph } numNodes = n; // Check the distance of each pair of vertices for (int i = 0; i maxCosine)&&(!(isVisited(neighbor, visited)))) { maxCosine = cosine(s, t, neighbor); next = neighbor; } } // Pick this neighbor to do recursive compassRouting // Add this neighbor to visited list so that it won't be visited again visited[last++] = s; return merge(s, compassRouting(next, t)); } } } // Merge an element and an array, this function is called in compassRouting() protected String[] merge(String head, String[] body) { int size = body.length; String[] merged = new String[size+1]; merged[0] = head; for (int i=1; i<=size; i++) { merged[i]=body[i-1]; } return merged; } // Calculate the cosine value of an angle. // Cosine value changes from 1 to -1 when the angle increases from 0 to PI/2 // i.e. larger cosine value indicates smaller angle protected double cosine(String current, String dest, String next) { double a = distance(dest, next); double b = distance(dest, current); double c = distance(current, next); return (b*b + c*c -a*a)/(2*b*c); } // Get the max degree in the wireless network, for experimental purpose protected int getMaxDegree() { maxDegree = 0; for (int i = 0; i maxDegree) maxDegree = degree; } return maxDegree; } }