コード例 #1
0
  /**
   * Calculates an approximation of the tiles that are making up the building. Sets these tiles
   * values to the corresponding collision values.
   *
   * @param collsionMatrix The collision matrix that is modified
   */
  public void calculateCollision(int[][] collisionMatrix) {
    Rectangle rect = p.getBounds();
    int maxX, maxY, minX, minY;
    maxX = (int) rect.getMaxX();
    maxY = (int) rect.getMaxY();
    minX = (int) rect.getMinX();
    minY = (int) rect.getMinY();

    // If building is a building used for education - find the center of it and add an EDUCATION
    // node
    if (tag.equals(OSM_Reader.EDUCATION)) {
      createEducationCenter(collisionMatrix, (maxX - minX) / 2 + minX, (maxY - minY) / 2 + minY);
    }

    double distance;
    double[] minDistances = new double[targetsInside.size()];
    Arrays.fill(minDistances, Double.MAX_VALUE);
    LinkedList<int[]> closestNodes = new LinkedList<int[]>();
    // Initiate vector with edge points (closest to respective target point)
    for (int i = 0; i < targetsInside.size(); i++) {
      int[] temp = new int[2];
      closestNodes.add(temp);
    }

    // Go through each line of the building
    for (int y = minY; y <= maxY; y++) {
      // Are we inside of map bounds (y)
      if (Math.round(OSM_Reader.scaleCollision * y) >= 0
          && Math.round(OSM_Reader.scaleCollision * y) < collisionMatrix.length) {
        for (int x = minX; x <= maxX; x++) {
          // Are we inside of map bounds (x)
          if (Math.round(OSM_Reader.scaleCollision * x) >= 0
              && Math.round(OSM_Reader.scaleCollision * x) < collisionMatrix.length) {
            // Is this point inside of the building? (and has no previous cost value)
            if (p.contains(x, y)
                && collisionMatrix[Math.round(OSM_Reader.scaleCollision * x)][
                        Math.round(OSM_Reader.scaleCollision * y)]
                    == 0) {
              collisionMatrix[Math.round(OSM_Reader.scaleCollision * x)][
                      Math.round(OSM_Reader.scaleCollision * y)] =
                  cost;

              // On the edge of the building
              if (!p.contains(x - 1, y) || !p.contains(x + 1, y)) {
                Node node;
                // Go through all targets that are inside of the building
                for (int i = 0; i < targetsInside.size(); i++) {
                  node = targetsInside.get(i);

                  // Calculate the Euclidean distance from target to edge
                  distance =
                      Math.sqrt(Math.pow(node.getXPos() - x, 2) + Math.pow(node.getYPos() - y, 2));

                  // Check if target is closer to this edge point
                  if (minDistances[i] > distance) {
                    minDistances[i] = distance;
                    closestNodes.get(i)[0] = x;
                    closestNodes.get(i)[1] = y;
                  }
                }
              }
            }
          }
        }
      }
    }
    createEntranceWays(collisionMatrix, closestNodes);
  }