Finds the list of points forming a line between two endpoints using DDA. @param x0 the x-coordinate of the starting point @param y0 the y-coordinate of the starting point @param x1 the x-coordinate of the ending point @param y1 the y-coordinate of the ending point @return an unmodifiable {@code Lis
(int x0, int y0, int x1, int y1)
| 30 | * @return an unmodifiable {@code List<Point>} containing all points on the line |
| 31 | */ |
| 32 | public static List<Point> findLine(int x0, int y0, int x1, int y1) { |
| 33 | int dx = x1 - x0; |
| 34 | int dy = y1 - y0; |
| 35 | |
| 36 | int steps = Math.max(Math.abs(dx), Math.abs(dy)); // number of steps |
| 37 | |
| 38 | double xIncrement = dx / (double) steps; |
| 39 | double yIncrement = dy / (double) steps; |
| 40 | |
| 41 | double x = x0; |
| 42 | double y = y0; |
| 43 | |
| 44 | List<Point> line = new ArrayList<>(steps + 1); |
| 45 | |
| 46 | for (int i = 0; i <= steps; i++) { |
| 47 | line.add(new Point((int) Math.round(x), (int) Math.round(y))); |
| 48 | x += xIncrement; |
| 49 | y += yIncrement; |
| 50 | } |
| 51 | |
| 52 | return line; |
| 53 | } |
| 54 | } |