Last active
September 7, 2019 02:33
-
-
Save lanesurface/5384357c563dfe88a7577e75f673ac6c to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// A simple example of the DDA line-drawing algorithm using characters. | |
import static java.lang.Math.abs; | |
public class Main { | |
static class Frame { | |
private int w, h; | |
private char[][] frm; | |
Frame( | |
int w, | |
int h) | |
{ | |
this.w = w; | |
this.h = h; | |
frm = new char[h][w]; | |
for (int y = 0; y < h; y++) | |
java.util.Arrays.fill( | |
frm[y], | |
' '); | |
} | |
public void set( | |
char ch, | |
int x, | |
int y) | |
{ | |
frm[x][y] = ch; | |
} | |
public void print() { | |
for (int y = 0; y < h; y++) { | |
for (int x = 0; x < w; x++) { | |
System.out.print(frm[y][x]); | |
} | |
System.out.println(); | |
} | |
} | |
} | |
public static void main(String[] args) { | |
Frame f; | |
f = new Frame( | |
25, | |
25); | |
line( | |
f, | |
0, | |
0, | |
20, | |
20); | |
f.print(); | |
} | |
static void line( | |
Frame frame, | |
int sx, | |
int sy, | |
int ex, | |
int ey) | |
{ | |
int steps, | |
x, | |
y, | |
dx, | |
dy, | |
xi, | |
yi; | |
dx = ex - sx; | |
dy = ey - sy; | |
steps = Math.max( | |
abs(dx), | |
abs(dy)); | |
xi = (int)(dx / (steps+0.d)); | |
yi = (int)(dy / (steps+0.d)); | |
x = y = 0; | |
for (int i = 0; i < steps; i++) { | |
x += xi; | |
y += yi; | |
frame.set( | |
'*', | |
x, | |
y); | |
} | |
} | |
} | |
// And here is the output: | |
// * | |
// * | |
// * | |
// * | |
// * | |
// * | |
// * | |
// * | |
// * | |
// * |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment