Skip to content

Commit e8d3beb

Browse files
committed
Improvement for BresenhamLine #6437
1 parent 24f4090 commit e8d3beb

File tree

6 files changed

+123
-126
lines changed

6 files changed

+123
-126
lines changed

src/main/java/com/thealgorithms/geometry/BresenhamLine.java

Lines changed: 0 additions & 69 deletions
This file was deleted.
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
package com.thealgorithms.geometry;
2+
3+
import java.awt.Point;
4+
import java.util.ArrayList;
5+
import java.util.List;
6+
7+
public class BresenhamLineStrategy implements LineDrawingStrategy {
8+
9+
@Override
10+
public List<Point> findLine(int x0, int y0, int x1, int y1) {
11+
List<Point> line = new ArrayList<>();
12+
13+
int dx = Math.abs(x1 - x0);
14+
int dy = Math.abs(y1 - y0);
15+
int sx = (x0 < x1) ? 1 : -1;
16+
int sy = (y0 < y1) ? 1 : -1;
17+
int err = dx - dy;
18+
19+
while (true) {
20+
line.add(new Point(x0, y0));
21+
if (x0 == x1 && y0 == y1) break;
22+
23+
int e2 = err * 2;
24+
25+
if (e2 > -dy) {
26+
err -= dy;
27+
x0 += sx;
28+
}
29+
if (e2 < dx) {
30+
err += dx;
31+
y0 += sy;
32+
}
33+
}
34+
35+
return line;
36+
}
37+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
package com.thealgorithms.geometry;
2+
3+
import java.awt.Point;
4+
import java.util.List;
5+
6+
public class LineDrawer {
7+
private LineDrawingStrategy strategy;
8+
9+
public LineDrawer(LineDrawingStrategy strategy) {
10+
this.strategy = strategy;
11+
}
12+
13+
public void setStrategy(LineDrawingStrategy strategy) {
14+
this.strategy = strategy;
15+
}
16+
17+
public List<Point> drawLine(int x0, int y0, int x1, int y1) {
18+
return strategy.findLine(x0, y0, x1, y1);
19+
}
20+
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
package com.thealgorithms.geometry;
2+
3+
import java.awt.Point;
4+
import java.util.List;
5+
6+
public interface LineDrawingStrategy {
7+
List<Point> findLine(int x0, int y0, int x1, int y1);
8+
}

src/test/java/com/thealgorithms/geometry/BresenhamLineTest.java

Lines changed: 0 additions & 57 deletions
This file was deleted.
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
package com.thealgorithms.geometry;
2+
3+
import java.awt.Point;
4+
import java.util.Collection;
5+
import java.util.List;
6+
import java.util.stream.Stream;
7+
import org.junit.jupiter.api.Assertions;
8+
import org.junit.jupiter.params.ParameterizedTest;
9+
import org.junit.jupiter.params.provider.Arguments;
10+
import org.junit.jupiter.params.provider.MethodSource;
11+
12+
13+
/**
14+
* Unit tests for the LineDrawer using BresenhamLineStrategy.
15+
*/
16+
class LineDrawerTest {
17+
18+
/**
19+
* Provides test data for drawing lines using the Bresenham algorithm.
20+
*/
21+
static Stream<Arguments> linePointsProvider() {
22+
return Stream.of(Arguments.of(0, 0, 5, 5, List.of(new Point(0, 0), new Point(1, 1), new Point(2, 2), new Point(3, 3), new Point(4, 4), new Point(5, 5))), Arguments.of(0, 0, 5, 0, List.of(new Point(0, 0), new Point(1, 0), new Point(2, 0), new Point(3, 0), new Point(4, 0), new Point(5, 0))),
23+
Arguments.of(0, 0, 0, 5, List.of(new Point(0, 0), new Point(0, 1), new Point(0, 2), new Point(0, 3), new Point(0, 4), new Point(0, 5))), Arguments.of(-2, -2, -5, -5, List.of(new Point(-2, -2), new Point(-3, -3), new Point(-4, -4), new Point(-5, -5))),
24+
Arguments.of(-1, -1, 2, 2, List.of(new Point(-1, -1), new Point(0, 0), new Point(1, 1), new Point(2, 2))), Arguments.of(2, -1, -1, -4, List.of(new Point(2, -1), new Point(1, -2), new Point(0, -3), new Point(-1, -4))));
25+
}
26+
27+
/**
28+
* Tests line drawing using the BresenhamLineStrategy through LineDrawer.
29+
*/
30+
@ParameterizedTest
31+
@MethodSource("linePointsProvider")
32+
void testLineDrawingWithBresenhamStrategy(int x0, int y0, int x1, int y1, Collection<Point> expected) {
33+
LineDrawingStrategy strategy = new BresenhamLineStrategy();
34+
LineDrawer drawer = new LineDrawer(strategy);
35+
36+
List<Point> actual = drawer.drawLine(x0, y0, x1, y1);
37+
38+
Assertions.assertEquals(expected.size(), actual.size(), "Point count mismatch.");
39+
Assertions.assertTrue(expected.containsAll(actual) && actual.containsAll(expected), "Generated points do not match expected points.");
40+
}
41+
42+
/**
43+
* Demonstrates dynamic strategy switching.
44+
*/
45+
@ParameterizedTest
46+
@MethodSource("linePointsProvider")
47+
void testSetStrategyDynamicSwitch(int x0, int y0, int x1, int y1, Collection<Point> expected) {
48+
LineDrawer drawer = new LineDrawer(new BresenhamLineStrategy());
49+
50+
// Simulate switching strategy at runtime (e.g., in future: drawer.setStrategy(new DdaLineStrategy()))
51+
drawer.setStrategy(new BresenhamLineStrategy()); // No-op here but shows extensibility
52+
53+
List<Point> actual = drawer.drawLine(x0, y0, x1, y1);
54+
55+
Assertions.assertEquals(expected.size(), actual.size(), "Point count mismatch after strategy switch.");
56+
Assertions.assertTrue(expected.containsAll(actual) && actual.containsAll(expected), "Generated points do not match expected points after strategy switch.");
57+
}
58+
}

0 commit comments

Comments
 (0)