I am working on a class project it's a flight radar simulator.
The situation is that when 2 planes are dangerously close the image of both flights changes, and when they are in a safe distance they change again.
The problem is that most of the time only one of those images is changed to red and I have no idea where the problem could be.
This is the method I am using to check the closeness condition, very simple and only checking for distance between them.
private void checkConflicts(ArrayList<Flight> flightsInArea) {
for (int i = 0; i < flightsInArea.size(); i++) {
for (int j = i + 1; j < flightsInArea.size(); j++) {
Coordinate currentFlight1 = flightsInArea.get(i).getCurrentPosition();
Coordinate currentFlight2 = flightsInArea.get(j).getCurrentPosition();
double cathetusX = Math.pow((currentFlight1.getPositionX() - currentFlight2.getPositionX()), 2);
double cathetusY = Math.pow((currentFlight1.getPositionY() - currentFlight2.getPositionY()), 2);
double distance = Math.sqrt(cathetusX + cathetusY);
if (distance < 100) {
flightsInArea.get(i).establishImage(true);
flightsInArea.get(j).establishImage(true);
} else {
flightsInArea.get(i).establishImage(false);
flightsInArea.get(j).establishImage(false);
}
}
}
}
This is the method use to establish the new image.
public void establishImage(boolean conflict) {
try {
if (conflict) {
image = ImageIO.read(this.getClass().getResource("red_plane.png"));
} else {
image = ImageIO.read(this.getClass().getResource("blue_plane.png"));
}
}
catch (IOException ex) {
Logger.getLogger(Vuelo.class.getName()).log(Level.SEVERE, null, ex);
}
}
Your algorithm is not sound. Consider the following simple example with 3 planes, where planes 1 and 3 are close, but plane 2 is far from both of them. Your loop does this:
- Check 1 & 2. Not close, mark both blue
- Check 1 & 3. Close - mark both red
- Check 2 & 3. Not close, mark both blue
Now, at the end, planes 2 & 3 will be marked blue, and plane 1 will be marked red, even though plane 3 should be marked red. This is because your algorithm is strictly iterative, and doesn't persist red markings. Here's a potential solution:
private void checkConflicts(ArrayList<Flight> flightsInArea) {
HashSet<Integer> redFlights = new HashSet<Integer>();
// Check for red flights
for (int i = 0; i < flightsInArea.size(); i++) {
for (int j = i + 1; j < flightsInArea.size(); j++) {
Coordinate currentFlight1 = flightsInArea.get(i).getCurrentPosition();
Coordinate currentFlight2 = flightsInArea.get(j).getCurrentPosition();
double cathetusX = Math.pow((currentFlight1.getPositionX() - currentFlight2.getPositionX()), 2);
double cathetusY = Math.pow((currentFlight1.getPositionY() - currentFlight2.getPositionY()), 2);
double distance = Math.sqrt(cathetusX + cathetusY);
if (distance < 100) {
redFlights.add(i);
redFlights.add(j);
}
}
}
// Mark flights
for (int i = 0; i < flightsInArea.size(); i++) {
flightsInArea.get(i).establishImage(redFlights.contains(i));
}
}