📄 roadfinder.java
字号:
double deltaY = y2 - y1; double deltaX = x2 - x1; double slope = deltaY / deltaX; double newX = x2; double newY = y2; if (newX < 0) { newX = 0; newY = Math.round(slope * (newX - x1) + y1); } else if (newX >= width) { newX = width - 1; newY = Math.round(slope * (newX - x1) + y1); } if (newY < 0) { newY = 0; newX = Math.round(x1 + (newY - y1) / slope); } else if (newY >= height) { newY = height - 1; newX = Math.round(x1 + (newY - y1) / slope); } int intX = (int) newX; int intY = (int) newY; if (intX < 0) { logger.warning("new x is " + intX); intX = 0; } if (intX >= width) { logger.warning("new x is " + intX); intX = width - 1; } if (intY < 0) { logger.warning("new y is " + intY); intY = 0; } if (intY >= height) { logger.warning("new y is " + intY); intY = height - 1; } if (logger.isLoggable(Level.INFO)) { logger.info("from " + x1 + "," + y1 + " to " + x2 + "," + y2 + "w " + width + " h " + height + " interp " + intX + "," + intY); } return new Point(intX, intY); } /** * Makes a road object given the points on the shape that are * within the visible box * * Stores it in a quadTree */ protected void makeRoad(Shape shape, OMGeometry graphic, int num, int[] xPoints, int[] yPoints, int segment) { createRoadFromPoints(num, xPoints, yPoints, segment); } /** * Makes a road object given the points on the shape that are * within the visible box * * @param nPoints in the xpoints and ypoints arrays */ protected RoadObject createRoadFromPoints(int id, int[] xpoints, int[] ypoints, int nPoints) { RoadPoint[] roadPoints = new RoadPoint[nPoints - 2]; Intersection from = findIntersection(xpoints[0], ypoints[0]); int fromBefore = from.getRoadCount(); Intersection to = findIntersection(xpoints[nPoints - 1], ypoints[nPoints - 1]); int toBefore = to.getRoadCount(); if (from == null) { logger.warning("no from intersection for " + xpoints[0] + ", " + ypoints[0]); } if (to == null) { logger.warning("no to intersection for " + xpoints[nPoints - 1] + ", " + ypoints[nPoints - 1]); } String name = "road"; Road road = createRoad(id, name + "-" + id, from, to, defaultRoadClass); if (fromBefore + 1 != from.getRoadCount()) logger.severe("huh? " + from + " had " + fromBefore + " roads before and now " + from.getRoadCount()); if (toBefore + 1 != to.getRoadCount()) logger.severe("huh? " + to + " had " + toBefore + " roads before and now " + to.getRoadCount()); int width = roadsMade % 5; roadsMade++; if (logger.isLoggable(Level.INFO)) { logger.info("road # " + roadsMade + " " + road + " has " + nPoints + " points"); } if (!showLines && drawIntersections) { OMPoint point = new YellowPoint(xpoints[0], ypoints[0], 10); toDraw.add(point); } for (int i = 1; i < nPoints - 1; i++) { roadPoints[i - 1] = new RoadPoint(road, createLatLonPoint(xpoints[i], ypoints[i]), this); if (drawIntersections) { if (showLines) { OMLine line = new YellowLine(xpoints[i - 1], ypoints[i - 1], xpoints[i], ypoints[i], width); toDraw.add(line); toDraw.add(new OMText((xpoints[i - 1] - xpoints[i]) / 2 + xpoints[i - 1], (ypoints[i - 1] - ypoints[i]) / 2 + ypoints[i - 1] - 5, "" + roadsMade, 0)); } else { OMPoint point = new YellowPoint(xpoints[i], ypoints[i], 10); toDraw.add(point); } } } if (drawIntersections) { if (showLines) { OMLine line = new YellowLine(xpoints[nPoints - 2], ypoints[nPoints - 2], xpoints[nPoints - 1], ypoints[nPoints - 1], width); toDraw.add(line); toDraw.add(new OMText((xpoints[nPoints - 2] - xpoints[nPoints - 1]) / 2 + xpoints[nPoints - 2], (ypoints[nPoints - 2] - ypoints[nPoints - 1]) / 2 + ypoints[nPoints - 2] - 5, "" + roadsMade, 0)); line.addArrowHead(true); } else { OMPoint point = new YellowPoint(xpoints[nPoints - 1], ypoints[nPoints - 1], 10); toDraw.add(point); } } if (to == from && nPoints == 2) { deleteRoad(road); return null; } road.setRoadPoints(roadPoints); if (!road.getFirstIntersection().equals(from)) logger.severe("huh? " + road + " first inter " + road.getFirstIntersection() + " not " + from); if (!road.getSecondIntersection().equals(to)) logger.severe("huh? " + road + " second inter " + road.getSecondIntersection() + " not " + to); if (road.getPoints().length < 2) logger.warning("Error : somehow made a road " + road + " with too few points."); else if (logger.isLoggable(Level.INFO)) { // logger.info("made " + road); } return road; } /** a yellow point for displaying intersections */ protected class YellowPoint extends OMPoint { public YellowPoint(int x, int y, int radius) { super(x, y, radius); } public void render(Graphics g) { setGraphicsColor(g, Color.YELLOW); draw(g); } } /** a yellow line for display routes between intersections */ protected class YellowLine extends OMLine { int width; public YellowLine(int x, int y, int x2, int y2, int width) { super(x, y, x2, y2); this.width = width; } public void render(Graphics g) { float[] dash1 = new float[width + 1]; dash1[0] = 10.f; for (int i = 1; i < width; i++) { dash1[i] = 2.0f; } BasicStroke dashed = new BasicStroke(5.0f, BasicStroke.CAP_BUTT, BasicStroke.JOIN_MITER, 10.0f, dash1, 0.0f); ((Graphics2D) g).setStroke(dashed); setGraphicsColor(g, Color.YELLOW); draw(g); } } protected LatLonPoint createLatLonPoint(int x, int y) { return getProjection().inverse(x, y); } protected Intersection findIntersection(LatLonPoint loc, String name) { if (name == null) return findIntersection(loc); Intersection intersection = intersections.get(name); if (intersection != null) { LatLonPoint foundLoc = intersection.getLocation(); float distance = (Math.abs(foundLoc.getLatitude() - loc.getLatitude()) + Math.abs(foundLoc.getLongitude() - loc.getLongitude())); if (distance * Intersection.GRID > 0.1f) { intersection = findIntersection(loc); // Ignore the // name, it's // too far away. System.out.println("Using " + intersection.getName() + " instead of " + name + " distance = " + distance); return intersection; } } else { intersection = new Intersection(loc, name, this); intersections.put(intersection); interQuadTree.put(intersection.getLatitude(), intersection.getLongitude(), intersection); } return intersection; } protected Intersection findIntersection(int x, int y) { LatLonPoint fromLoc = createLatLonPoint(x, y); Intersection from = findIntersection(fromLoc); return from; } protected Intersection findIntersection(LatLonPoint loc) { String name = Intersection.getLatLonPointName(loc); Intersection intersection = intersections.get(name); if (intersection == null) { if (logger.isLoggable(Level.FINE)) logger.fine("making new intersection for " + loc); intersection = new Intersection(loc, name, this); interQuadTree.put(intersection.getLatitude(), intersection.getLongitude(), intersection); intersections.put(intersection); } else { if (logger.isLoggable(Level.FINE)) logger.fine("found existing intersection for " + loc + " with " + intersection.getRoadCount() + " roads coming out of it."); } return intersection; } protected void deleteIntersection(Intersection intersection) { if (intersection.getRoadCount() > 0) throw new IllegalArgumentException("Attempt to delete connected intersection"); intersections.remove(intersection); } /** * called from Intersection Implemented for RoadLayer interface */ public Road createRoad(Intersection from) { return createRoad(-1, null, from, null, null); } protected Road createRoad(int id, String name, Intersection from, Intersection to, RoadClass cl_ss) { if (id < 0) id = findUnusedRoadID(); if (name == null) name = "Road_" + id; if (from == null) from = findIntersection(to.getLocation(), to.getName() + ".drag"); if (to == null) to = findIntersection(from.getLocation(), from.getName() + ".drag"); if (cl_ss == null) cl_ss = defaultRoadClass; Road road = new Road(id, name, from, to, cl_ss, this); road.setModified(true); from.addRoad(road); to.addRoad(road); roads.add(road); return road; } public void deleteRoad(Road road) { Intersection intersection1 = road.getFirstIntersection(); Intersection intersection2 = road.getSecondIntersection(); intersection1.removeRoad(road); intersection2.removeRoad(road); if (intersection1.getRoadCount() == 0) deleteIntersection(intersection1); if (intersection2.getRoadCount() == 0) deleteIntersection(intersection2); if (intersection1.getRoadCount() == 2 && intersection1.getRoad(0).getRoadClass() == intersection1.getRoad(1) .getRoadClass()) joinRoads(intersection1); if (intersection2.getRoadCount() == 2 && intersection2.getRoad(0).getRoadClass() == intersection2.getRoad(1) .getRoadClass()) joinRoads(intersection2); removedRoads.addElement(road); roads.remove(road); } /** * Split a road into two roads at one of its corners. An * intersection is created where the corner was and the segments * before the corner become the segments of the original road. The * segments after the corner become the segments of a new road * between the new intersection and the */ public Intersection splitRoad(Road road, RoadPoint rp) { RoadPoint[] pointsBefore = road.getPointsBefore(rp); RoadPoint[] pointsAfter = road.getPointsAfter(rp); Intersection newIntersection = findIntersection(rp.getLocation(), null); Intersection firstIntersection = road.getFirstIntersection(); Intersection secondIntersection = road.getSecondIntersection(); road.setIntersections(firstIntersection, newIntersection); road.setRoadPoints(pointsBefore); secondIntersection.removeRoad(road); newIntersection.addRoad(road); Road newRoad = createRoad(-1, null,
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -