# Java Program to Implement Bresenham Line Algorithm

«
»
This is a Java Program to Implement Bresenham Line Algorithm. The Bresenham line algorithm is an algorithm which determines which order to form a close approximation to a straight line between two given points. It is commonly used to draw lines on a computer screen, as it uses only integer addition, subtraction and bit shifting, all of which are very cheap operations in standard computer architectures. It is one of the earliest algorithms developed in the field of computer graphics. A minor extension to the original algorithm also deals with drawing circles.

Here is the source code of the Java Program to Implement Bresenham Line Algorithm. The Java program is successfully compiled and run on a Windows system. The program output is also shown below.

1. `/**`
2. ` ** Java Program to Implement Bresenham Line Algorithm`
3. ` **/`
4. ` `
5. `import java.util.Scanner;`
6. `import java.util.ArrayList;`
7. `import java.util.List;`
8. `import java.awt.Point;`
9. ` `
10. `/** Class Bresenham **/`
11. `public class Bresenham `
12. `{`
13. `    /** function findLine() - to find that belong to line connecting the two points **/ `
14. `    public List<Point> findLine(Point[][] grid, int x0, int y0, int x1, int y1) `
15. `    {                    `
16. `        List<Point> line = new ArrayList<Point>();`
17. ` `
18. `        int dx = Math.abs(x1 - x0);`
19. `        int dy = Math.abs(y1 - y0);`
20. ` `
21. `        int sx = x0 < x1 ? 1 : -1; `
22. `        int sy = y0 < y1 ? 1 : -1; `
23. ` `
24. `        int err = dx-dy;`
25. `        int e2;`
26. ` `
27. `        while (true) `
28. `        {`
29. `            line.add(grid[x0][y0]);`
30. ` `
31. `            if (x0 == x1 && y0 == y1) `
32. `                break;`
33. ` `
34. `            e2 = 2 * err;`
35. `            if (e2 > -dy) `
36. `            {`
37. `                err = err - dy;`
38. `                x0 = x0 + sx;`
39. `            }`
40. ` `
41. `            if (e2 < dx) `
42. `            {`
43. `                err = err + dx;`
44. `                y0 = y0 + sy;`
45. `            }`
46. `        }                                `
47. `        return line;`
48. `    }`
49. ` `
50. `    /** function plot() - to visualize grid **/`
51. `    public void plot(Point[][] grid, List<Point> line)`
52. `    {`
53. `        int rows = grid.length;`
54. `        int cols = grid[0].length;`
55. ` `
56. `        System.out.println("\nPlot : \n");`
57. ` `
58. `        for (int i = 0; i < rows; i++)`
59. `        {`
60. `            for (int j = 0; j < cols; j++)`
61. `            {`
62. `                if (line.contains(grid[i][j]))`
63. `                    System.out.print("*");`
64. `                else`
65. `                    System.out.print("X");`
66. `            }`
67. `            System.out.println();`
68. `        }`
69. `    }`
70. ` `
71. `    /** Function main() **/`
72. `    public static void main(String[] args)`
73. `    {`
74. `        Scanner scan = new Scanner(System.in);`
75. ` `
76. `        System.out.println("Bresenham Line Algorithm");`
77. ` `
78. `        System.out.println("\nEnter dimensions of grid");`
79. `        int rows = scan.nextInt();`
80. `        int cols = scan.nextInt();`
81. ` `
82. `        Point[][] grid = new Point[rows][cols];`
83. ` `
84. `        for (int i = 0; i < rows; i++)`
85. `            for (int j = 0; j < cols; j++)`
86. `                grid[i][j] = new Point(i, j);`
87. ` `
88. `        System.out.println("\nEnter coordinates of point 1 and point 2");`
89. `        int sr = scan.nextInt();`
90. `        int sc = scan.nextInt();`
91. `        int fr = scan.nextInt();`
92. `        int fc = scan.nextInt();`
93. ` `
94. `        Bresenham b = new Bresenham();`
95. ` `
96. `        List<Point> line = b.findLine(grid, sr, sc, fr, fc);`
97. ` `
98. `        b.plot(grid, line);`
99. `    }        `
100. `}`

```
Bresenham Line Algorithm

Enter dimensions of grid
40 40

Enter coordinates of point 1 and point 2
2 3
37 31

Plot :

XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
XXX*XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
XXXX*XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
XXXXX*XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
XXXXX*XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
XXXXXX*XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
XXXXXXX*XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
XXXXXXXX*XXXXX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