public void addLinkNext(int index, Coord second) { Link link = links.get(index); if (index == links.size() - 1 || !link.getToNode().equals(links.get(index + 1).getFromNode())) { Point2D toPoint = new Point2D(link.getToNode().getCoord().getX(), link.getToNode().getCoord().getY()); Point2D secondPoint = new Point2D(second.getX(), second.getY()); Vector2D dirSegment = new Vector2D(toPoint, secondPoint); Link bestLink = null; double smallestAngle = Double.POSITIVE_INFINITY; for (Link linkN : network.getLinks().values()) if (link.getToNode().equals(linkN.getFromNode())) { Point2D toPoint2 = new Point2D(linkN.getToNode().getCoord().getX(), linkN.getToNode().getCoord().getY()); Vector2D linkNSegment = new Vector2D(toPoint, toPoint2); double angle = dirSegment.getAngleTo(linkNSegment); if (angle < smallestAngle) { smallestAngle = angle; bestLink = linkN; } } links.add(index + 1, bestLink); } }
private List<Link> getBestLinksMode(Network network, Coord coord, Shape shape) { List<Double> nearestDistances = new ArrayList<Double>(); List<Link> nearestLinks = new ArrayList<Link>(); for (double minDistance = this.minDistance; nearestLinks.size() < numCandidates; minDistance += MIN_DISTANCE_DELTA) for (Link link : network.getLinks().values()) if (link.getAllowedModes().contains(mode)) { Point2D fromPoint = new Point2D( link.getFromNode().getCoord().getX(), link.getFromNode().getCoord().getY()); Point2D toPoint = new Point2D(link.getToNode().getCoord().getX(), link.getToNode().getCoord().getY()); Vector2D linkVector = new Vector2D(fromPoint, toPoint); Line2D linkSegment = new Line2D(fromPoint, toPoint); if (!withInsideStops || linkSegment.isNearestInside(new Point2D(coord.getX(), coord.getY()))) if (!withAngleShape || shape == null || linkVector.getAngleTo(shape.getVector(coord)) < Math.PI / 16) { double distance = ((LinkImpl) link).calcDistance(coord); if (distance < minDistance) { int i = 0; for (; i < nearestDistances.size() && distance < nearestDistances.get(i); i++) ; if (i > 0 || nearestLinks.size() < numCandidates) { nearestDistances.add(i, distance); nearestLinks.add(i, link); if (nearestLinks.size() > numCandidates) { nearestDistances.remove(0); nearestLinks.remove(0); } } } } } return nearestLinks; }