Last active
April 21, 2018 01:21
-
-
Save Scarsz/d029d2bdca11906677ee86a48e6a528c to your computer and use it in GitHub Desktop.
Join a list of points with routes of a maximum length
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import javafx.geometry.Point3D; | |
import javax.imageio.ImageIO; | |
import java.awt.*; | |
import java.awt.image.BufferedImage; | |
import java.io.File; | |
import java.io.IOException; | |
import java.util.HashSet; | |
import java.util.Random; | |
import java.util.Set; | |
/** | |
* Note: Point3D's are used though this is a 2D graph.<br> | |
* This is because this gist was originally made with the intent to route points in 3D. | |
* The resulting picture is 2D just to show the concept. | |
*/ | |
public class GenerateUndirectedGraphWithRoutesMaxLength { | |
public static final File OUTPUT = new File("output.png"); | |
public static final Random RANDOM = new Random(); | |
public static void main(String[] args) { | |
int pointsToGenerate = 25; | |
int maxDistanceBetweenPoints = 100; | |
int connectionWidth = 3; | |
int pointSize = 20; | |
int pictureSize = 1000; | |
int borderSize = 100; | |
Set<Point3D> points = new HashSet<>(); | |
for (int i = 0; i < pointsToGenerate; i++) points.add(new Point3D(RANDOM.nextInt(pictureSize - (borderSize * 2)) + borderSize, RANDOM.nextInt(pictureSize - (borderSize * 2)) + borderSize, 0)); | |
Set<Route> routes = Route.computeAllRoutes(points, maxDistanceBetweenPoints); | |
try { | |
BufferedImage image = new BufferedImage(pictureSize, pictureSize, BufferedImage.TYPE_INT_ARGB); | |
Graphics2D graphics = image.createGraphics(); | |
graphics.setPaint(Color.white); | |
graphics.fillRect(0, 0, pictureSize, pictureSize); | |
// radii | |
graphics.setPaint(new Color(255, 21, 24, 150)); | |
for (Point3D point : points) { | |
graphics.fillOval( | |
(int) point.getX() - maxDistanceBetweenPoints, | |
(int) point.getY() - maxDistanceBetweenPoints, | |
maxDistanceBetweenPoints * 2, | |
maxDistanceBetweenPoints * 2 | |
); | |
} | |
// points | |
graphics.setPaint(Color.black); | |
for (Point3D point : points) { | |
graphics.fillOval( | |
(int) point.getX() - (pointSize / 2), | |
(int) point.getY() - (pointSize / 2), | |
pointSize, | |
pointSize | |
); | |
} | |
// connections | |
graphics.setPaint(Color.green); | |
graphics.setStroke(new BasicStroke(connectionWidth)); | |
for (Route route : routes) { | |
graphics.drawLine( | |
(int) route.getPoint1().getX(), | |
(int) route.getPoint1().getY(), | |
(int) route.getPoint2().getX(), | |
(int) route.getPoint2().getY() | |
); | |
} | |
ImageIO.write(image, "PNG", OUTPUT); | |
} catch (IOException ie) { | |
ie.printStackTrace(); | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import javafx.geometry.Point3D; | |
import java.util.HashSet; | |
import java.util.Set; | |
public class Route { | |
public static Set<Route> computeAllRoutes(Set<Point3D> points, int maxDistance) { | |
Set<Route> routes = new HashSet<>(); | |
for (Point3D point1 : points) { | |
for (Point3D point2 : points) { | |
if (point1.equals(point2)) continue; | |
boolean routeExists = routes.stream() | |
.anyMatch(route -> | |
(point1.equals(route.getPoint1()) || point1.equals(route.getPoint2())) && | |
(point2.equals(route.getPoint1()) || point2.equals(route.getPoint2())) | |
); | |
if (!routeExists) routes.add(new Route(point1, point2)); | |
} | |
} | |
// invalidate the route if the points are too far apart | |
routes.removeIf(route -> route.getDistance() > maxDistance); | |
return routes; | |
} | |
private final Point3D[] points; | |
public Route(Point3D point1, Point3D point2) { | |
this.points = new Point3D[] { point1, point2 }; | |
} | |
public double getDistance() { | |
return getPoint1().distance(getPoint2()); | |
} | |
public Point3D getPoint1() { | |
return points[0]; | |
} | |
public Point3D getPoint2() { | |
return points[1]; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment