Ejemplo n.º 1
0
  /** Returns a (random) coordinate that is between two adjacent MapNodes */
  @Override
  public Coord getInitialLocation(boolean translate) {
    List<MapNode> nodes = map.getNodes();
    MapNode n, n2;
    Coord n2Location, nLocation, placement;
    double dx, dy;
    double rnd = rng.nextDouble();

    // choose a random node (from OK types if such are defined)
    do {
      n = nodes.get(rng.nextInt(nodes.size()));
    } while (okMapNodeTypes != null && !n.isType(okMapNodeTypes));

    // choose a random neighbor of the selected node
    n2 = n.getNeighbors().get(rng.nextInt(n.getNeighbors().size()));

    nLocation = n.getLocation();
    n2Location = n2.getLocation();

    placement = n.getLocation().clone();

    dx = rnd * (n2Location.getX() - nLocation.getX());
    dy = rnd * (n2Location.getY() - nLocation.getY());
    if (translate == false) {
      dx = 0;
      dy = 0;
    }
    placement.translate(dx, dy); // move coord from n towards n2

    this.lastMapNode = n;
    return placement;
  }
Ejemplo n.º 2
0
  /**
   * Selects and returns a random node that is OK from a list of nodes. Whether node is OK, is
   * determined by the okMapNodeTypes list. If okMapNodeTypes are defined, the given list
   * <strong>must</strong> contain at least one OK node to prevent infinite looping.
   *
   * @param nodes The list of nodes to choose from.
   * @return A random node from the list (that is OK if ok list is defined)
   */
  protected MapNode selectRandomOkNode(List<MapNode> nodes) {
    MapNode n;
    do {
      n = nodes.get(rng.nextInt(nodes.size()));
    } while (okMapNodeTypes != null && !n.isType(okMapNodeTypes));

    return n;
  }