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:

Java Program to Implement Meldable Heap
Map Interface trong java
SOAP Web service: Upload và Download file sử dụng MTOM trong JAX-WS
Java Program to Implement Find all Cross Edges in a Graph
Limiting Query Results with JPA and Spring Data JPA
Spring Security Logout
LIKE Queries in Spring JPA Repositories
Apache Camel with Spring Boot
Java Program to Implement LinkedTransferQueue API
A Guide to Java HashMap
Spring Data JPA and Null Parameters
Guava Collections Cookbook
Enum trong java
Java Program to Check whether Graph is Biconnected
Java Program to Implement Ford–Fulkerson Algorithm
Spring MVC + Thymeleaf 3.0: New Features
Java Program to Find Number of Spanning Trees in a Complete Bipartite Graph
Java Program to Implement Graph Structured Stack
Giới thiệu về Stream API trong Java 8
Java Program to Find MST (Minimum Spanning Tree) using Prim’s Algorithm
Java Program to Implement RoleList API
Java Program to Find the Median of two Sorted Arrays using Binary Search Approach
Java Program to Perform the Sorting Using Counting Sort
Java Program to Apply Above-Below-on Test to Find the Position of a Point with respect to a Line
Iterable to Stream in Java
Java Program to Apply DFS to Perform the Topological Sorting of a Directed Acyclic Graph
Examine the internal DNS cache
Hướng dẫn Java Design Pattern – Builder
Java program to Implement Tree Set
Giới thiệu Google Guice – Injection, Scope
Java Program to Generate a Graph for a Given Fixed Degree Sequence
Exploring the Spring 5 WebFlux URL Matching