Skip to content

Instantly share code, notes, and snippets.

@suvasish114
Last active February 9, 2022 11:45
Show Gist options
  • Save suvasish114/62cbefc896d8bd6158867c3bf02ec0b3 to your computer and use it in GitHub Desktop.
Save suvasish114/62cbefc896d8bd6158867c3bf02ec0b3 to your computer and use it in GitHub Desktop.
Brecenhan's Line Drawing Algorithm using Java
import java.util.List;
import java.util.ArrayList;
import javax.swing.JFrame;
import java.awt.Graphics;
import java.awt.Color;
import java.util.Arrays;
// driving code
class BrecenhansLineDrawingAlgorithm{
public static void main(String[] args) {
Table table = new Table(2, 2, 80, 60);
table.drawTable();
new DrawPixel(table.getList());
}
}
// create table class
class Table {
private int x1, x2, y1, y2;
private List<List<Integer>> list = new ArrayList<List<Integer>>();
Table(int x1, int y1, int x2, int y2){
this.x1 = x1;
this.x2 = x2;
this.y1 = y1;
this.y2 = y2;
}
public void drawTable(){
System.out.println("X old\tY old\tX inc\tY inc\tX new\tY new\tround(X new)\tround(Y new)");
System.out.println("-----------------------------------------------------------------------------");
int dx = Math.abs(x2 - x1);
int dy = Math.abs(y2 - y1);
float x=x1, y=y1;
float p = (2*dy) - dx;
list.add(Arrays.asList(Math.round(x),Math.round(y)));
for(int i=0; i<dx; i++){
System.out.print(String.format("%.2f", x)+"\t"+String.format("%.2f", y)+"\t"+1+"\t"+1+"\t");
x += 1;
if(p >= 0){
y += 1;
p += (2*dy) - (2*dx);
}
else{
p += 2*dy;
}
list.add(Arrays.asList(Math.round(x),Math.round(y)));
System.out.println(String.format("%.2f", x)+"\t"+String.format("%.2f", y)+"\t"+Math.round(x)+"\t\t"+Math.round(y));
}
}
public List<List<Integer>> getList(){
return this.list;
}
}
// draw pixel class
class DrawPixel extends JFrame{
private List<List<Integer>> list = new ArrayList<List<Integer>>();
public DrawPixel(List<List<Integer>> list){
super("Brecenhan's Algo");
setSize(200, 200);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setVisible(true);
this.list = list;
}
@Override
public void paint(Graphics g){
g.setColor(new Color(255, 0, 0));
for(List<Integer> a: list){
g.drawRect(0+a.get(0), 200-a.get(1), 1, 1);
}
}
}
@suvasish114
Copy link
Author

suvasish114 commented Feb 9, 2022

Output

Screenshot 2022-02-09 at 5 14 41 PM

X old Y old X inc Y inc X new Y new round(X new) round(Y new)
2.00 2.00 1 1 3.00 3.00 3 3
3.00 3.00 1 1 4.00 3.00 4 3
4.00 3.00 1 1 5.00 4.00 5 4
5.00 4.00 1 1 6.00 5.00 6 5
6.00 5.00 1 1 7.00 6.00 7 6
7.00 6.00 1 1 8.00 6.00 8 6
8.00 6.00 1 1 9.00 7.00 9 7
9.00 7.00 1 1 10.00 8.00 10 8
10.00 8.00 1 1 11.00 9.00 11 9
11.00 9.00 1 1 12.00 9.00 12 9
12.00 9.00 1 1 13.00 10.00 13 10
13.00 10.00 1 1 14.00 11.00 14 11
14.00 11.00 1 1 15.00 12.00 15 12
15.00 12.00 1 1 16.00 12.00 16 12
16.00 12.00 1 1 17.00 13.00 17 13
17.00 13.00 1 1 18.00 14.00 18 14
18.00 14.00 1 1 19.00 15.00 19 15
19.00 15.00 1 1 20.00 15.00 20 15
20.00 15.00 1 1 21.00 16.00 21 16
21.00 16.00 1 1 22.00 17.00 22 17
22.00 17.00 1 1 23.00 18.00 23 18
23.00 18.00 1 1 24.00 18.00 24 18
24.00 18.00 1 1 25.00 19.00 25 19
25.00 19.00 1 1 26.00 20.00 26 20
26.00 20.00 1 1 27.00 21.00 27 21
27.00 21.00 1 1 28.00 21.00 28 21
28.00 21.00 1 1 29.00 22.00 29 22
29.00 22.00 1 1 30.00 23.00 30 23
30.00 23.00 1 1 31.00 24.00 31 24
31.00 24.00 1 1 32.00 24.00 32 24
32.00 24.00 1 1 33.00 25.00 33 25
33.00 25.00 1 1 34.00 26.00 34 26
34.00 26.00 1 1 35.00 27.00 35 27
35.00 27.00 1 1 36.00 27.00 36 27
36.00 27.00 1 1 37.00 28.00 37 28
37.00 28.00 1 1 38.00 29.00 38 29
38.00 29.00 1 1 39.00 30.00 39 30
39.00 30.00 1 1 40.00 30.00 40 30
40.00 30.00 1 1 41.00 31.00 41 31
41.00 31.00 1 1 42.00 32.00 42 32
42.00 32.00 1 1 43.00 32.00 43 32
43.00 32.00 1 1 44.00 33.00 44 33
44.00 33.00 1 1 45.00 34.00 45 34
45.00 34.00 1 1 46.00 35.00 46 35
46.00 35.00 1 1 47.00 35.00 47 35
47.00 35.00 1 1 48.00 36.00 48 36
48.00 36.00 1 1 49.00 37.00 49 37
49.00 37.00 1 1 50.00 38.00 50 38
50.00 38.00 1 1 51.00 38.00 51 38
51.00 38.00 1 1 52.00 39.00 52 39
52.00 39.00 1 1 53.00 40.00 53 40
53.00 40.00 1 1 54.00 41.00 54 41
54.00 41.00 1 1 55.00 41.00 55 41
55.00 41.00 1 1 56.00 42.00 56 42
56.00 42.00 1 1 57.00 43.00 57 43
57.00 43.00 1 1 58.00 44.00 58 44
58.00 44.00 1 1 59.00 44.00 59 44
59.00 44.00 1 1 60.00 45.00 60 45
60.00 45.00 1 1 61.00 46.00 61 46
61.00 46.00 1 1 62.00 47.00 62 47
62.00 47.00 1 1 63.00 47.00 63 47
63.00 47.00 1 1 64.00 48.00 64 48
64.00 48.00 1 1 65.00 49.00 65 49
65.00 49.00 1 1 66.00 50.00 66 50
66.00 50.00 1 1 67.00 50.00 67 50
67.00 50.00 1 1 68.00 51.00 68 51
68.00 51.00 1 1 69.00 52.00 69 52
69.00 52.00 1 1 70.00 53.00 70 53
70.00 53.00 1 1 71.00 53.00 71 53
71.00 53.00 1 1 72.00 54.00 72 54
72.00 54.00 1 1 73.00 55.00 73 55
73.00 55.00 1 1 74.00 56.00 74 56
74.00 56.00 1 1 75.00 56.00 75 56
75.00 56.00 1 1 76.00 57.00 76 57
76.00 57.00 1 1 77.00 58.00 77 58
77.00 58.00 1 1 78.00 59.00 78 59
78.00 59.00 1 1 79.00 59.00 79 59
79.00 59.00 1 1 80.00 60.00 80 60

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment