Skip to content

Instantly share code, notes, and snippets.

@0x414c
Created November 16, 2015 12:02
Show Gist options
  • Save 0x414c/3bbd1122a50e4be229ce to your computer and use it in GitHub Desktop.
Save 0x414c/3bbd1122a50e4be229ce to your computer and use it in GitHub Desktop.
public class Bresenham implements Drawer<BufferedImage2D> {
@Override
public void line (BufferedImage2D target, int x1, int y1, int x2, int y2, int color) {
int x, y;
int dx, dy;
int incx, incy;
int balance;
if (x2 >= x1) {
dx = x2 - x1;
incx = 1;
} else {
dx = x1 - x2;
incx = -1;
}
if (y2 >= y1) {
dy = y2 - y1;
incy = 1;
} else {
dy = y1 - y2;
incy = -1;
}
x = x1;
y = y1;
if (dx >= dy) {
dy <<= 1;
balance = dy - dx;
dx <<= 1;
while (x != x2) {
target.set (x, y, color);
if (balance >= 0) {
y += incy;
balance -= dx;
}
balance += dy;
x += incx;
}
target.set (x, y, color);
} else {
dx <<= 1;
balance = dx - dy;
dy <<= 1;
while (y != y2) {
target.set (x, y, color);
if (balance >= 0) {
x += incx;
balance -= dy;
}
balance += dx;
y += incy;
}
target.set (x, y, color);
}
return;
}
}
public class DDA implements Drawer<BufferedImage2D> {
@Override
public void line (BufferedImage2D target, int x0, int y0, int x1, int y1, int color) {
int length, i;
double x, y;
double xincrement;
double yincrement;
length = Math.abs (x1 - x0);
if (Math.abs (y1 - y0) > length)
length = Math.abs (y1 - y0);
xincrement = (double) (x1 - x0) / (double) length;
yincrement = (double) (y1 - y0) / (double) length;
x = x1 + 0.5;
y = y1 + 0.5;
for (i = 1; i <= length; ++i) {
try {
target.set ((int) x, (int) y, color);
} catch (Exception e) {
System.out.println (String.format ("%f %f", x, y));
return;
}
x = x + xincrement;
y = y + yincrement;
}
return;
}
}
public class EFLA implements Drawer<BufferedImage2D> {
public static final int ROUNDING = 0x8000;
public static final int FIXEDPOINTNUMBER = 16;
/**
* Extremely Fast Line Algorithm Var. E (Addition Fixed Point PreCalc)
* Copyright 2001-2, By Po-Han Lin
*
* Freely usable in non-commercial applications as long as credits
* to Po-Han Lin and link to http://www.edepot.com is provided in
* source code and can been seen in compiled executable.
* Commercial applications please inquire about licensing the algorithms.
*
* Latest version at http://www.edepot.com/phl.html
* This version is for standard displays (up to 65536x65536)
* @param target
* @param x0
* @param y0
* @param x1
* @param y1
* @param color
*/
@Override
public void line (BufferedImage2D target, int x0, int y0, int x1, int y1, int color) {
boolean isDyGreaterThanDx = false;
int dy = y1 - y0;
int dx = x1 - x0;
if (Math.abs (dy) > Math.abs (dx)) {
int tmp = dy;
dy = dx;
dx = tmp;
isDyGreaterThanDx = true;
}
int inc;
if (dx == 0) {
inc = 0;
} else {
inc = (dy << FIXEDPOINTNUMBER) / dx;
}
if (isDyGreaterThanDx) {
if (dx > 0) {
dx += y0;
for (int x = ROUNDING + (x0 << FIXEDPOINTNUMBER); y0 <= dx; ++y0) {
target.set (x >> FIXEDPOINTNUMBER, y0, color);
x += inc;
}
return;
}
dx += y0;
for (int x = ROUNDING + (x0 << FIXEDPOINTNUMBER); y0 >= dx; --y0) {
target.set (x >> FIXEDPOINTNUMBER, y0, color);
x -= inc;
}
return;
}
if (dx > 0) {
dx += x0;
for (int y = ROUNDING + (y0 << FIXEDPOINTNUMBER); x0 <= dx; ++x0) {
target.set (x0, y >> FIXEDPOINTNUMBER, color);
y += inc;
}
return;
}
dx += x0;
for (int y = ROUNDING + (y0 << FIXEDPOINTNUMBER); x0 >= dx; --x0) {
target.set (x0, y >> FIXEDPOINTNUMBER, color);
y -= inc;
}
return;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment