public void findTHS() { int doubleRange = this.range * 2; for (int i = 0; i < numNodes; i++) { Node active = nodes.get(i); for (int j = 0; j < numNodes; j++) { Node toCheck = nodes.get(j); if (i != j) { // System.out.println("active x: " + active.getXLanePosition() + " active y: " + // active.getyLanePosition()); // System.out.println("check y : " + toCheck.getXLanePosition() + " check y: " + // toCheck.getyLanePosition()); int activeX = active.getXLanePosition(); int activeY = active.getyLanePosition(); int checkX = toCheck.getXLanePosition(); int checkY = toCheck.getyLanePosition(); double distance = Math.sqrt(Math.pow((activeX - checkX), 2) + Math.pow((activeY - checkY), 2)); if (distance <= doubleRange) { active.addToTHS(toCheck.getID()); } } } } }
/** Arranges cars based on the number of lanes */ private void arrangeCars() { // Number of cars placed so far int numCar = 0; // Position in lane where cars placed int lanePosition = 0; // Place cars until reach simulation parameters while (numCar < this.numNodes) { for (int i = 0; i < this.numLanes; i++) { double rand = r.nextDouble(); if (rand < this.density) { Node toAdd = new Node( numCar, this.numSlots, this.PoS, this.range, i * this.width, lanePosition, this.maxReservationDuration); nodes.add(toAdd); numCar++; } } lanePosition++; } field = new Node[numLanes][lanePosition]; for (int i = 0; i < this.numNodes; i++) { Node active = nodes.get(i); field[active.getXLanePosition() / this.width][active.getyLanePosition()] = active; } }
public void sendJamInRange(int senderID, JamMessage m) { Node active = nodes.get(senderID); int activeLane = active.getLane(); // int activeLanePosition = active.getLanePosition(); // System.out.println(field.length+ " x " + field[0].length); // Left most lane with which communication may occur int leftLimit = ((activeLane * this.width) - this.range) / this.width; if (leftLimit < 0) leftLimit = 0; // Right most lane with which communication may occur int rightLimit = ((activeLane * this.width) + this.range) / this.width; if (rightLimit > field.length) rightLimit = field.length - 1; int upperLimit = active.getyLanePosition() + this.range; if (upperLimit >= field[0].length) upperLimit = field[0].length - 1; int lowerLimit = active.getyLanePosition() - this.range; if (lowerLimit < 0) lowerLimit = 0; int activeX = active.getXLanePosition(); int activeY = active.getyLanePosition(); for (int j = leftLimit; j <= rightLimit; j++) { for (int k = lowerLimit; k <= upperLimit; k++) { Node toCheck = field[j][k]; if (toCheck != null && toCheck.getID() != active.getID()) { int toCheckX = toCheck.getXLanePosition() * this.width; int toCheckY = toCheck.getyLanePosition(); double distance = Math.sqrt(Math.pow(toCheckX - activeX, 2) + Math.pow(toCheckY - activeY, 2)); // System.out.println(distance); if (distance < this.range) { toCheck.recJam(m); } } } } }