Pacific-Design.com

    
Home Index

1. Computer Vision

2. Bresenham

Computer Vision / Bresenham /

Bresenham Algorithm

/*------------------------------------------------------------------------------
 * Drawing Line using Bresenham Algorithm
 * Advanced Computer Graphics
 * Written by: Kevin T. Duraj
 */

public class Graphics1 {

    public static void main(String[] args) {
        bresenham_image();
    }
    
    /*
     * Bresenham Image
     */
    private static void bresenham_image() {
        
        int image_width = 250;
        int image_height = 250;

        System.out.println("Creating Bresenham Image ...");
        Bresenham bres = new Bresenham(image_width, image_height, 0, 0, 0);

        //--- Create a star pattern of lines ---//
        for (int i = 0; i < 360; i += 30) {
            int x = (int) (200 * Math.cos(i * Math.PI / 180.0));
            int y = (int) (200 * Math.sin(i * Math.PI / 180.0));

            int ox = bres.image[0][0].length / 2;
            int oy = bres.image[0].length / 2;

            bres.bresenhamLine(ox, oy, ox + x, oy + y, 255, 0, 0);
        }

        //--- Draw the circles ---//
        for (int radius = 10; radius < bres.image[0].length; radius += 25) {
            bres.bresenhamCircle(bres.image[0][0].length / 2, bres.image[0].length / 2, radius, 255, 255, 0);
        }

        bres.write("bresenham.png");
    }
}



/*------------------------------------------------------------------------------
 * Class Bresenham: Drawing Line using Bresenham Algorithm
 * 
 * Reference: 
 * Line using bresenham algorithm
 * Circle-explained
 * 
 */

import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;

/*------------------------------------------------------------------------------
 * Class Bresenham to Draw an image
 */
public class Bresenham {

    int width;
    int height;
    public int image[][][];

    /*--------------------------------------------------------------------------
     * Constructor - initialize image with a size and background color
     */
    public Bresenham(int width_p, int height_p, int r, int g, int b) {

        this.width = width_p;
        this.height = height_p;
        image = new int[3][height][width];

        for (int i = 0; i < height; ++i) {

            for (int j = 0; j < width; ++j) {
                image[0][i][j] = (byte) r;
                image[1][i][j] = (byte) g;
                image[2][i][j] = (byte) b;

            }
        }
    }

    /*--------------------------------------------------------------------------
     * Drawing Line using Bresenham Algorithm
     */
    public void bresenhamLine(int x0, int y0, int x1, int y1, int r, int g, int b) {

        int delta_width = x1 - x0;
        int delta_height = y1 - y0;
        int dx0 = 0, dy0 = 0, dx1 = 0, dy1 = 0;


        if (delta_width < 0) {
            dx0 = -1;
        } else if (delta_width > 0) {
            dx0 = 1;
        }
        if (delta_height < 0) {
            dy0 = -1;
        } else if (delta_height > 0) {
            dy0 = 1;
        }
        if (delta_width < 0) {
            dx1 = -1;
        } else if (delta_width > 0) {
            dx1 = 1;
        }

        int longest = Math.abs(delta_width);
        int shortest = Math.abs(delta_height);

        if (!(longest > shortest)) {

            longest = Math.abs(delta_height);
            shortest = Math.abs(delta_width);

            if (delta_height < 0) {
                dy1 = -1;
            } else if (delta_height > 0) {
                dy1 = 1;
            }
            dx1 = 0;
        }

        int numerator = longest >> 1;

        for (int i = 0; i <= longest; i++) {

            set_pixel(x0, y0, r, g, b);

            numerator += shortest;

            if (!(numerator < longest)) {
                numerator -= longest;
                x0 += dx0;
                y0 += dy0;
            } else {
                x0 += dx1;
                y0 += dy1;
            }
        }
    }

    /*--------------------------------------------------------------------------
     * Drawing Circle using Bresenham Algorithm
     */
    public void bresenhamCircle(int x, int y, int radius, int r, int g, int b) {
        int i = 0, j = radius;

        while (i <= j) {
            set_pixel(x + i, y - j, r, g, b);
            set_pixel(x + j, y - i, r, g, b);
            set_pixel(x + i, y + j, r, g, b);
            set_pixel(x + j, y + i, r, g, b);
            set_pixel(x - i, y - j, r, g, b);
            set_pixel(x - j, y - i, r, g, b);
            set_pixel(x - i, y + j, r, g, b);
            set_pixel(x - j, y + i, r, g, b);
            i++;
            j = (int) (Math.sqrt(radius * radius - i * i) + 0.5);
        }
    }
    /*--------------------------------------------------------------------------
     *  Private method set_pixel sets pixel in the image
     */

    private void set_pixel(int x, int y, int r, int g, int b) {

        try {

            //System.out.println("y=" + x + " x=" + y);
            
            if (y > -1 && x > -1 && y < height && x < width) {
                image[0][y][x] = (byte) r;
                image[1][y][x] = (byte) g;
                image[2][y][x] = (byte) b;
            }

        } catch (Exception e) {
            System.err.println("Exception: y=" + y + " x=" + x);
        }
    }


    /*--------------------------------------------------------------------------
     *  Move image into BufferedImage object then write Image into the File
     */
    public void write(String filename) {

        System.out.println("Writing image into: " + filename);

        try {

            BufferedImage bi = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);

            for (int i = 0; i < height; ++i) {

                for (int j = 0; j < width; ++j) {
                    int pixel = (image[0][i][j] << 16) | (image[1][i][j] << 8) | (image[2][i][j]);
                    bi.setRGB(j, i, pixel);
                }
            }

            File outputfile = new File(filename);
            ImageIO.write(bi, "png", outputfile);

        } catch (IOException e) {
            System.out.println("Image write error");
        }

        System.out.println("Sucessfull");
    }
}
/*----------------------------------------------------------------------------*/


Actual Image Output using Bresenham Algorithm