List of usage examples for java.awt Point distance
public double distance(Point2D pt)
From source file:exploration.rendezvous.MultiPointRendezvousStrategy.java
public static Point getBetterCommLocation(Point point1, Point point2, RealAgent ag) { double curSignal = PropModel1.signalStrength(ag.getCommRange(), ag.getOccupancyGrid(), point1, point2); double origSignal = curSignal; Point curPoint = new Point(point1.x, point1.y); boolean foundNewPoint = true; while (foundNewPoint && (point1.distance(curPoint) < SimConstants.DEFAULT_SPEED)) { foundNewPoint = false;//from w ww . j a v a 2 s .c om int oldX = curPoint.x; int oldY = curPoint.y; for (int x = oldX - SimConstants.DEFAULT_SPEED; x <= oldX + SimConstants.DEFAULT_SPEED; x++) { for (int y = oldY - SimConstants.DEFAULT_SPEED; y <= oldY + SimConstants.DEFAULT_SPEED; y++) { Point testPoint = new Point(x, y); if (ag.getOccupancyGrid().directLinePossible(point1, testPoint, true, false)) { double newSignal = PropModel1.signalStrength(ag.getCommRange(), ag.getOccupancyGrid(), testPoint, point2); if (newSignal > curSignal) { curPoint = testPoint; curSignal = newSignal; foundNewPoint = true; } } } } } //if (!curPoint.equals(point1)) { if (SimConstants.DEBUG_OUTPUT) { System.out.println(ag + " getBetterCommLocation(" + point1 + ", " + point2 + "): " + "origSignal: " + origSignal + ", newSignal: " + curSignal + ", newPoint: " + curPoint); } return curPoint; //} else { // return RandomWalk.takeStep(agent); //} }
From source file:it.unibo.alchemist.boundary.monitors.Generic2DDisplay.java
/** * Actually draws the environment on the view. * // w ww. ja v a2 s. co m * @param g * {@link Graphics2D} object responsible for drawing */ protected final void drawEnvOnView(final Graphics2D g) { if (wormhole == null || !isVisible() || !isEnabled()) { return; } accessData(); if (hooked.isPresent()) { final Position hcoor = positions.get(hooked.get()); final Point hp = wormhole.getViewPoint(hcoor); if (hp.distance(getCenter()) > FREEDOM_RADIUS) { wormhole.setViewPosition(hp); } } /* * Compute nodes in sight and their screen position */ final Map<Node<T>, Point> onView = positions.entrySet().parallelStream() .map(pair -> new Pair<>(pair.getKey(), wormhole.getViewPoint(pair.getValue()))) .filter(p -> wormhole.isInsideView(p.getSecond())) .collect(Collectors.toMap(Pair::getKey, Pair::getValue)); g.setColor(Color.BLACK); if (obstacles != null) { /* * TODO: only draw obstacles if on view */ obstacles.parallelStream().map(this::convertObstacle).forEachOrdered(g::fill); } if (paintLinks) { g.setColor(Color.GRAY); onView.keySet().parallelStream().map(neighbors::get) .flatMap(neigh -> neigh.getNeighbors().parallelStream() .map(node -> node.compareTo(neigh.getCenter()) > 0 ? new Pair<>(neigh.getCenter(), node) : new Pair<>(node, neigh.getCenter()))) .distinct().map( pair -> mapPair(pair, node -> Optional.ofNullable(onView.get(node)) .orElse(wormhole.getViewPoint(positions.get(node))))) .forEachOrdered(line -> { final Point p1 = line.getFirst(); final Point p2 = line.getSecond(); g.drawLine(p1.x, p1.y, p2.x, p2.y); }); } releaseData(); if (isDraggingMouse && status == ViewStatus.MOVING && originPoint.isPresent() && endingPoint.isPresent()) { for (final Node<T> n : selectedNodes) { if (onView.containsKey(n)) { onView.put(n, new Point(onView.get(n).x + (endingPoint.get().x - originPoint.get().x), onView.get(n).y + (endingPoint.get().y - originPoint.get().y))); } } } g.setColor(Color.GREEN); if (effectStack != null) { effectStack.forEach(effect -> { onView.entrySet().forEach(entry -> { final Point p = entry.getValue(); effect.apply(g, entry.getKey(), p.x, p.y); }); }); } if (isCloserNodeMarked()) { final Optional<Map.Entry<Node<T>, Point>> closest = onView.entrySet().parallelStream() .min((pair1, pair2) -> { final Point p1 = pair1.getValue(); final Point p2 = pair2.getValue(); final double d1 = Math.hypot(p1.x - mousex, p1.y - mousey); final double d2 = Math.hypot(p2.x - mousex, p2.y - mousey); return Double.compare(d1, d2); }); if (closest.isPresent()) { nearest = closest.get().getKey(); final int nearestx = closest.get().getValue().x; final int nearesty = closest.get().getValue().y; drawFriedEgg(g, nearestx, nearesty, Color.RED, Color.YELLOW); } } else { nearest = null; } if (isDraggingMouse && status == ViewStatus.SELECTING && originPoint.isPresent() && endingPoint.isPresent()) { g.setColor(Color.BLACK); final int x = originPoint.get().x < endingPoint.get().x ? originPoint.get().x : endingPoint.get().x; final int y = originPoint.get().y < endingPoint.get().y ? originPoint.get().y : endingPoint.get().y; final int width = Math.abs(endingPoint.get().x - originPoint.get().x); final int height = Math.abs(endingPoint.get().y - originPoint.get().y); g.drawRect(x, y, width, height); selectedNodes = onView.entrySet().parallelStream() .filter(nodes -> isInsideRectangle(nodes.getValue(), x, y, width, height)) .map(onScreen -> onScreen.getKey()).collect(Collectors.toSet()); } selectedNodes.parallelStream().map(e -> Optional.ofNullable(onView.get(e))).filter(Optional::isPresent) .map(Optional::get).forEachOrdered(p -> drawFriedEgg(g, p.x, p.y, Color.BLUE, Color.CYAN)); }
From source file:exploration.rendezvous.MultiPointRendezvousStrategy.java
private void calculateRendezvousFrontier(int timeElapsed) { RendezvousAgentData rvd = agent.getRendezvousAgentData(); // Only calculate rv every several time steps at most if (rvd.getTimeSinceLastRVCalc() < SimConstants.RV_REPLAN_INTERVAL) { return;//w ww.j a va2 s. co m } else { rvd.setTimeSinceLastRVCalc(0); } Point explorerPoint = getExplorerRVPoint(); TeammateAgent relay = agent.getParentTeammate(); //Do same as sampling method, except we already have explorer point //need to find nearest point to base's comms range System.out.print(SimConstants.INDENT + "Generating random points ... "); generatedPoints = SampleEnvironmentPoints(agent, settings.SamplePointDensity); NearRVPoint explorerRVPoint = new NearRVPoint(explorerPoint.x, explorerPoint.y); generatedPoints.add(explorerRVPoint); System.out.print(SimConstants.INDENT + "Finding commlinks ... "); connectionsToBase = FindCommLinks(generatedPoints, agent); if (SimConstants.DEBUG_OUTPUT) { System.out.println(agent + " connectionsToBase count is " + connectionsToBase.size()); } int pathsCalculated = 0; int meetingTime = calculateRVTimings(explorerPoint, explorerPoint, agent.getTeammate(SimConstants.BASE_STATION_TEAMMATE_ID).getLocation(), timeElapsed); PriorityQueue<Frontier> frontiers = agent.getFrontiers(); int maxFrontierExploreTime = 0; Point currentRelayBasePoint = agent.getTeammate(SimConstants.BASE_STATION_TEAMMATE_ID).getLocation(); if ((rvd.getParentRendezvous() != null) && (rvd.getParentRendezvous().parentsRVLocation != null) && (rvd.getParentRendezvous().parentsRVLocation.getChildLocation() != null)) { currentRelayBasePoint = rvd.getParentRendezvous().parentsRVLocation.getChildLocation(); } if (SimConstants.DEBUG_OUTPUT) { System.out.println("Current relay base point is " + currentRelayBasePoint); } Frontier bestFrontier = null; CommLink bestLink = null; for (Frontier f : frontiers) { if (!f.equals(agent.getFrontier())) { //potential frontier for the relay to explore //can relay even get to frontier in time, if the meeting point was at frontier centre? double timeToFrontier = 0; timeToFrontier += agent.calculatePath(relay.getLocation(), currentRelayBasePoint, false, false) .getLength(); timeToFrontier += agent.calculatePath(currentRelayBasePoint, f.getCentre(), false, false) .getLength(); double hereToFrontier = timeToFrontier; double delta = explorerPoint.distance(f.getCentre()) - agent.getCommRange(); if (delta < 0) { delta = 0; } timeToFrontier += delta; timeToFrontier = timeToFrontier / agent.getSpeed(); timeToFrontier += agent.getTimeElapsed(); if (timeToFrontier > meetingTime) { if (SimConstants.DEBUG_OUTPUT) { System.out.println("Skipping frontier test1 at " + f.getCentre() + "; timeToFrontier is " + timeToFrontier + ", meetingTime is " + meetingTime); } continue; } //cannot possibly reach frontier in time. if (meetingTime - timeToFrontier < maxFrontierExploreTime) { if (SimConstants.DEBUG_OUTPUT) { System.out.println("Skipping frontier test1 at " + f.getCentre() + "; timeToFrontier is " + timeToFrontier + ", meetingTime is " + meetingTime + ", maxFrontierExploreTime is " + maxFrontierExploreTime); } continue; } //we already have a frontier we can explore for longer double minDistToBase = Double.MAX_VALUE; for (CommLink link : explorerRVPoint.commLinks) { NearRVPoint connectedPoint = link.getRemotePoint(); Path frontierToMeeting = agent.calculatePath(f.getCentre(), connectedPoint.getLocation(), false, false); double totalTime = (hereToFrontier + frontierToMeeting.getLength()) / agent.getSpeed(); totalTime += agent.getTimeElapsed(); if (!frontierToMeeting.found) { if (SimConstants.DEBUG_OUTPUT) { System.out.println( "Skipping frontier test2 at " + f.getCentre() + "; path not found! (between " + f.getCentre() + " and " + connectedPoint.getLocation()); } continue; } if (totalTime > meetingTime) { if (SimConstants.DEBUG_OUTPUT) { System.out.println("Skipping frontier test2 at " + f.getCentre() + "; timeToFrontier is " + timeToFrontier + ", meetingTime is " + meetingTime + ", totalTime is " + totalTime); } continue; } //cannot make it to meeting point in time if (meetingTime - totalTime < maxFrontierExploreTime) { if (SimConstants.DEBUG_OUTPUT) { System.out.println("Skipping frontier test2 at " + f.getCentre() + "; timeToFrontier is " + timeToFrontier + ", meetingTime is " + meetingTime + ", maxFrontierExploreTime is " + maxFrontierExploreTime); } continue; } //we already have a better point maxFrontierExploreTime = (int) (meetingTime - totalTime); bestFrontier = f; bestLink = link; /*pathsCalculated = findNearestPointInBaseCommRange(connectedPoint, connectionsToBase, agent); if (connectedPoint.distanceToParent < minDistToBase) { minDistToBase = connectedPoint.distanceToParent; explorerRVPoint.commLinkClosestToBase = link; }*/ } } } NearRVPoint parentPoint; NearRVPoint childPoint = explorerRVPoint; if (bestFrontier != null && bestLink != null) { pathsCalculated = findNearestPointInBaseCommRange(bestLink.getRemotePoint(), connectionsToBase, agent); explorerRVPoint.commLinkClosestToBase = bestLink; parentPoint = childPoint.commLinkClosestToBase.getRemotePoint(); if (SimConstants.DEBUG_OUTPUT) { System.out.println("Ended up selecting frontier " + bestFrontier.getCentre() + ", parentPoint is " + parentPoint.toString() + ", basePoint is " + parentPoint.parentPoint.toString()); } } else { parentPoint = explorerRVPoint; pathsCalculated = findNearestPointInBaseCommRange(parentPoint, connectionsToBase, agent); CommLink selfLink = new CommLink(parentPoint, parentPoint); explorerRVPoint.commLinkClosestToBase = selfLink; } //At this point, for explorerRVPoint, we know: // 1. Connected point explorerRVPoint' that is nearest to comm range of Base // 2. Distance from explorerRVPoint' to comm range of Base // 3. Nearest point from explorerRVPoint' that is within comm range of Base if (explorerRVPoint.commLinkClosestToBase == null || explorerRVPoint.commLinkClosestToBase.getRemotePoint() == null) { //something went wrong, set RV to backup and return Rendezvous meetingLocation = new Rendezvous(explorerPoint); meetingLocation.setParentLocation(explorerPoint); Point baseLocation = agent.getTeammate(agent.getParentTeammate().getParent()).getLocation(); Rendezvous parentsMeetingLocation = new Rendezvous(baseLocation); if (SimConstants.DEBUG_OUTPUT) { System.out.println(" base location: " + baseLocation); } parentsMeetingLocation.setParentLocation(baseLocation); meetingLocation.parentsRVLocation = parentsMeetingLocation; rvd.setParentRendezvous(meetingLocation); Rendezvous backupRV = new Rendezvous(explorerPoint); rvd.setParentBackupRendezvous(backupRV); calculateRVTimings(timeElapsed); return; } //End method. Now just set the found points as RV. Rendezvous meetingLocation = new Rendezvous(childPoint); meetingLocation.setParentLocation(parentPoint); Rendezvous parentsMeetingLocation = new Rendezvous(parentPoint.parentPoint); Point baseLocation = agent.getTeammate(agent.getParentTeammate().getParent()).getLocation(); if (SimConstants.DEBUG_OUTPUT) { System.out.println(" base location: " + baseLocation); } parentsMeetingLocation .setParentLocation(agent.getTeammate(agent.getParentTeammate().getParent()).getLocation()); meetingLocation.parentsRVLocation = parentsMeetingLocation; rvd.setParentRendezvous(meetingLocation); Rendezvous backupRV = new Rendezvous(childPoint); rvd.setParentBackupRendezvous(backupRV); calculateRVTimings(timeElapsed); displayData.setGeneratedPoints(generatedPoints); System.out.print(SimConstants.INDENT + "Choosing advanced RV complete, chose " + rvd.getParentRendezvous().getChildLocation().x + "," + rvd.getParentRendezvous().getChildLocation().y + ". "); }