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.

/**
 ** Java Program to Implement Bresenham Line Algorithm
 **/
 
import java.util.Scanner;
import java.util.ArrayList;
import java.util.List;
import java.awt.Point;
 
/** Class Bresenham **/
public class Bresenham 
{
    /** function findLine() - to find that belong to line connecting the two points **/ 
    public List<Point> findLine(Point[][] grid, int x0, int y0, int x1, int y1) 
    {                    
        List<Point> line = new ArrayList<Point>();
 
        int dx = Math.abs(x1 - x0);
        int dy = Math.abs(y1 - y0);
 
        int sx = x0 < x1 ? 1 : -1; 
        int sy = y0 < y1 ? 1 : -1; 
 
        int err = dx-dy;
        int e2;
 
        while (true) 
        {
            line.add(grid[x0][y0]);
 
            if (x0 == x1 && y0 == y1) 
                break;
 
            e2 = 2 * err;
            if (e2 > -dy) 
            {
                err = err - dy;
                x0 = x0 + sx;
            }
 
            if (e2 < dx) 
            {
                err = err + dx;
                y0 = y0 + sy;
            }
        }                                
        return line;
    }
 
    /** function plot() - to visualize grid **/
    public void plot(Point[][] grid, List<Point> line)
    {
        int rows = grid.length;
        int cols = grid[0].length;
 
        System.out.println("\nPlot : \n");
 
        for (int i = 0; i < rows; i++)
        {
            for (int j = 0; j < cols; j++)
            {
                if (line.contains(grid[i][j]))
                    System.out.print("*");
                else
                    System.out.print("X");
            }
            System.out.println();
        }
    }
 
    /** Function main() **/
    public static void main(String[] args)
    {
        Scanner scan = new Scanner(System.in);
 
        System.out.println("Bresenham Line Algorithm");
 
        System.out.println("\nEnter dimensions of grid");
        int rows = scan.nextInt();
        int cols = scan.nextInt();
 
        Point[][] grid = new Point[rows][cols];
 
        for (int i = 0; i < rows; i++)
            for (int j = 0; j < cols; j++)
                grid[i][j] = new Point(i, j);
 
        System.out.println("\nEnter coordinates of point 1 and point 2");
        int sr = scan.nextInt();
        int sc = scan.nextInt();
        int fr = scan.nextInt();
        int fc = scan.nextInt();
 
        Bresenham b = new Bresenham();
 
        List<Point> line = b.findLine(grid, sr, sc, fr, fc);
 
        b.plot(grid, line);
    }        
}
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*XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
XXXXXXXXX*XXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
XXXXXXXXX*XXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
XXXXXXXXXX*XXXXXXXXXXXXXXXXXXXXXXXXXXXXX
XXXXXXXXXXX*XXXXXXXXXXXXXXXXXXXXXXXXXXXX
XXXXXXXXXXXX*XXXXXXXXXXXXXXXXXXXXXXXXXXX
XXXXXXXXXXXXX*XXXXXXXXXXXXXXXXXXXXXXXXXX
XXXXXXXXXXXXX*XXXXXXXXXXXXXXXXXXXXXXXXXX
XXXXXXXXXXXXXX*XXXXXXXXXXXXXXXXXXXXXXXXX
XXXXXXXXXXXXXXX*XXXXXXXXXXXXXXXXXXXXXXXX
XXXXXXXXXXXXXXXX*XXXXXXXXXXXXXXXXXXXXXXX
XXXXXXXXXXXXXXXXX*XXXXXXXXXXXXXXXXXXXXXX
XXXXXXXXXXXXXXXXX*XXXXXXXXXXXXXXXXXXXXXX
XXXXXXXXXXXXXXXXXX*XXXXXXXXXXXXXXXXXXXXX
XXXXXXXXXXXXXXXXXXX*XXXXXXXXXXXXXXXXXXXX
XXXXXXXXXXXXXXXXXXXX*XXXXXXXXXXXXXXXXXXX
XXXXXXXXXXXXXXXXXXXXX*XXXXXXXXXXXXXXXXXX
XXXXXXXXXXXXXXXXXXXXX*XXXXXXXXXXXXXXXXXX
XXXXXXXXXXXXXXXXXXXXXX*XXXXXXXXXXXXXXXXX
XXXXXXXXXXXXXXXXXXXXXXX*XXXXXXXXXXXXXXXX
XXXXXXXXXXXXXXXXXXXXXXXX*XXXXXXXXXXXXXXX
XXXXXXXXXXXXXXXXXXXXXXXXX*XXXXXXXXXXXXXX
XXXXXXXXXXXXXXXXXXXXXXXXX*XXXXXXXXXXXXXX
XXXXXXXXXXXXXXXXXXXXXXXXXX*XXXXXXXXXXXXX
XXXXXXXXXXXXXXXXXXXXXXXXXXX*XXXXXXXXXXXX
XXXXXXXXXXXXXXXXXXXXXXXXXXXX*XXXXXXXXXXX
XXXXXXXXXXXXXXXXXXXXXXXXXXXXX*XXXXXXXXXX
XXXXXXXXXXXXXXXXXXXXXXXXXXXXX*XXXXXXXXXX
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXX*XXXXXXXXX
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX*XXXXXXXX
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX

Related posts:

Guide to Spring Cloud Kubernetes
Java Program to Implement Meldable Heap
Hướng dẫn Java Design Pattern – Bridge
Java Program to Implement ConcurrentSkipListMap API
HttpAsyncClient Tutorial
How to Set TLS Version in Apache HttpClient
Java Program to Implement Circular Doubly Linked List
Guava CharMatcher
Java Program to Implement Extended Euclid Algorithm
Generic Constructors in Java
OAuth2 for a Spring REST API – Handle the Refresh Token in AngularJS
Encode a String to UTF-8 in Java
Serialize Only Fields that meet a Custom Criteria with Jackson
Java Program to Perform Insertion in a BST
Call Methods at Runtime Using Java Reflection
Java Program to Implement Miller Rabin Primality Test Algorithm
Spring Security – security none, filters none, access permitAll
Java Program to Find a Good Feedback Vertex Set
Java – InputStream to Reader
Java Program to Generate All Subsets of a Given Set in the Lexico Graphic Order
Java Program to Implement Slicker Algorithm that avoids Triangulation to Find Area of a Polygon
Iterable to Stream in Java
Spring Cloud – Securing Services
Java Program to Generate Date Between Given Range
Java Program to Implement the Alexander Bogomolny’s UnOrdered Permutation Algorithm for Elements Fro...
LinkedHashSet trong java
Java Program to Search for an Element in a Binary Search Tree
Java Program to Implement Queue
Pagination and Sorting using Spring Data JPA
Java Program to Implement Dijkstra’s Algorithm using Set
Reactive Flow with MongoDB, Kotlin, and Spring WebFlux
Converting String to Stream of chars