Skip to content

Improvement for BresenhamLine #6437 #6448

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
69 changes: 0 additions & 69 deletions src/main/java/com/thealgorithms/geometry/BresenhamLine.java

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package com.thealgorithms.geometry;

import java.awt.Point;
import java.util.ArrayList;
import java.util.List;

public class BresenhamLineStrategy implements LineDrawingStrategy {

@Override
public List<Point> findLine(int x0, int y0, int x1, int y1) {
List<Point> line = new ArrayList<>();

int dx = Math.abs(x1 - x0);
int dy = Math.abs(y1 - y0);
int sx = (x0 < x1) ? 1 : -1;
int sy = (y0 < y1) ? 1 : -1;
int err = dx - dy;

while (true) {
line.add(new Point(x0, y0));
if (x0 == x1 && y0 == y1) {
break;
}
int e2 = err * 2;

if (e2 > -dy) {
err -= dy;
x0 += sx;
}
if (e2 < dx) {
err += dx;
y0 += sy;
}
}

return line;
}
}
20 changes: 20 additions & 0 deletions src/main/java/com/thealgorithms/geometry/LineDrawer.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package com.thealgorithms.geometry;

import java.awt.Point;
import java.util.List;

public class LineDrawer {
private LineDrawingStrategy strategy;

public LineDrawer(LineDrawingStrategy strategy) {
this.strategy = strategy;
}

public void setStrategy(LineDrawingStrategy strategy) {
this.strategy = strategy;
}

public List<Point> drawLine(int x0, int y0, int x1, int y1) {
return strategy.findLine(x0, y0, x1, y1);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package com.thealgorithms.geometry;

import java.awt.Point;
import java.util.List;

public interface LineDrawingStrategy {
List<Point> findLine(int x0, int y0, int x1, int y1);
}
57 changes: 0 additions & 57 deletions src/test/java/com/thealgorithms/geometry/BresenhamLineTest.java

This file was deleted.

57 changes: 57 additions & 0 deletions src/test/java/com/thealgorithms/geometry/LineDrawerTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
package com.thealgorithms.geometry;

import java.awt.Point;
import java.util.Collection;
import java.util.List;
import java.util.stream.Stream;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.Arguments;
import org.junit.jupiter.params.provider.MethodSource;

/**
* Unit tests for the LineDrawer using BresenhamLineStrategy.
*/
class LineDrawerTest {

/**
* Provides test data for drawing lines using the Bresenham algorithm.
*/
static Stream<Arguments> linePointsProvider() {
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))),
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))),
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))));
}

/**
* Tests line drawing using the BresenhamLineStrategy through LineDrawer.
*/
@ParameterizedTest
@MethodSource("linePointsProvider")
void testLineDrawingWithBresenhamStrategy(int x0, int y0, int x1, int y1, Collection<Point> expected) {
LineDrawingStrategy strategy = new BresenhamLineStrategy();
LineDrawer drawer = new LineDrawer(strategy);

List<Point> actual = drawer.drawLine(x0, y0, x1, y1);

Assertions.assertEquals(expected.size(), actual.size(), "Points count mismatch.");
Assertions.assertTrue(expected.containsAll(actual) && actual.containsAll(expected), "Generated points do not match expected points.");
}

/**
* Demonstrates dynamic strategy switching.
*/
@ParameterizedTest
@MethodSource("linePointsProvider")
void testSetStrategyDynamicSwitch(int x0, int y0, int x1, int y1, Collection<Point> expected) {
LineDrawer drawer = new LineDrawer(new BresenhamLineStrategy());

// Simulate switching strategy at runtime (e.g., in future: drawer.setStrategy(new DdaLineStrategy()))
drawer.setStrategy(new BresenhamLineStrategy()); // No-op here but shows extensibility

List<Point> actual = drawer.drawLine(x0, y0, x1, y1);

Assertions.assertEquals(expected.size(), actual.size(), "Point count mismatch after strategy switch.");
Assertions.assertTrue(expected.containsAll(actual) && actual.containsAll(expected), "Generated points do not match expected points after strategy switch.");
}
}