1

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.

Radar

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);
    }
}
Cœur
  • 37,241
  • 25
  • 195
  • 267
sandiego
  • 151
  • 1
  • 4
  • 10
  • 3
    Won't solve your problem, but you should NOT be reading the image from desk every time you do your check. The images should be read into memory when you start your program. – camickr Oct 13 '13 at 18:45
  • Thanks, I'll follow your advice. – sandiego Oct 13 '13 at 19:14
  • ["Aircraft use a slightly modified version of polar coordinates for navigation,"](http://en.wikipedia.org/wiki/Polar_coordinate_system#Position_and_navigation) seen [here](http://stackoverflow.com/a/12607586/230513). – trashgod Oct 13 '13 at 20:27

1 Answers1

2

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));
  }
}
torquestomp
  • 3,304
  • 19
  • 26