Skip to content
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

Add method for drawing line #7

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
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
40 changes: 40 additions & 0 deletions src/main/java/io/raffi/drawille/Canvas.java
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,46 @@ protected void checkRange ( int x, int y ) {
}
}

//ttaken from https://rosettacode.org/wiki/Bitmap/Bresenham%27s_line_algorithm#Java
public void drawLine(Boolean value, int fromX, int fromY, int toX, int toY) {
int d = 0;

int dx = Math.abs(fromX - toX);
int dy = Math.abs(fromY - toY);

int dx2 = 2 * dx; // slope scaling factors to
int dy2 = 2 * dy; // avoid floating point

int ix = fromX < toX ? 1 : -1; // increment direction
int iy = fromY < toY ? 1 : -1;

int x = fromX;
int y = fromY;

if (dx >= dy) {
while (x != toX) {
change(x, y, value);

x += ix;
d += dy2;
if (d > dx) {
y += iy;
d -= dx2;
}
}
} else {
while (y != toY) {
change(x, y, value);

y += iy;
d += dx2;
if (d > dy) {
x += ix;
d -= dy2;
}
}
}
}
/**
* This method returns the screen width in the true pixel definition. The user supplied width is
* multiplied by 2 because a braille dot matrix has 2 columns.
Expand Down