From f79da38f7fcbb395accccac8b68ea67ca1126092 Mon Sep 17 00:00:00 2001 From: creme332 <65414576+creme332@users.noreply.github.com> Date: Sun, 7 Jul 2024 17:41:40 +0400 Subject: [PATCH] handle case where start and end points are same --- .../com/github/creme332/algorithms/LineCalculator.java | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/src/main/java/com/github/creme332/algorithms/LineCalculator.java b/src/main/java/com/github/creme332/algorithms/LineCalculator.java index 51345cb9..13ae0844 100644 --- a/src/main/java/com/github/creme332/algorithms/LineCalculator.java +++ b/src/main/java/com/github/creme332/algorithms/LineCalculator.java @@ -23,6 +23,9 @@ private LineCalculator() { * x-coordinates and the second element is the array of y-coordinates. */ public static int[][] dda(int x0, int y0, int x1, int y1) { + if (x0 == x1 && y0 == y1) + return new int[][] { { x0 }, { y0 } }; + final int dx = x1 - x0; final int dy = y1 - y0; final int steps = Math.max(Math.abs(dx), Math.abs(dy)); @@ -59,6 +62,9 @@ public static int[][] dda(int x0, int y0, int x1, int y1) { * x-coordinates and the second element is the array of y-coordinates. */ public static int[][] bresenham(int x0, int y0, int x1, int y1) { + if (x0 == x1 && y0 == y1) + return new int[][] { { x0 }, { y0 } }; + int dx = Math.abs(x1 - x0); int dy = Math.abs(y1 - y0); @@ -102,6 +108,9 @@ public static int[][] bresenham(int x0, int y0, int x1, int y1) { * @return */ public static int[][] bresenham2(int x0, int y0, int x1, int y1) { + if (x0 == x1 && y0 == y1) + return new int[][] { { x0 }, { y0 } }; + List xpoints = new ArrayList<>(); List ypoints = new ArrayList<>();