Bresenham Line Algorithm
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 3 of 3

Thread: Bresenham Line Algorithm

  1. #1
    Join Date
    Mar 2011
    Posts
    3

    Resolved SOLVED: Bresenham Line Algorithm

    Hi, I've written a Bresenham Line drawing application in Java. When the scale is set to either equal (eg. 10x10) or the Y scale > the X scale then the line draws perfectly (see images attached).

    When the X scale < Y scale then the lines don't fill the pixel grid correctly at one end and where lines cross.

    Could anyone explain what I've done wrong?

    Thanks.

    Code:


    import java.awt.Graphics2D;

    public class bresLine {

    private float xStart, yStart, xEnd, yEnd;

    //sets start point
    public void setStartPoint(float x, float y) {
    this.xStart = x;
    this.yStart = y;
    }

    //sets end point
    public void setEndPoint(float x, float y) {
    this.xEnd = x;
    this.yEnd = y;
    }

    //draw on fractal panel
    public void drawBresenham(Graphics2D f2Buffer) {

    float dy = yEnd - yStart;
    float dx = xEnd - xStart;
    int stepx, stepy;

    if (dy < 0) {
    dy = -dy;
    stepy = -1;
    } else {
    stepy = 1;
    }
    if (dx < 0) {
    dx = -dx;
    stepx = -1;
    } else {
    stepx = 1;
    }
    dy = dy * 2;
    dx = dx * 2;

    f2Buffer.drawLine((int) xStart, (int) yStart, (int) xStart, (int) yStart);
    if (dx > dy) {
    float fraction = (2 * dy) - dx;
    while (xStart != xEnd) {
    if (fraction >= 0) {
    yStart += stepy;
    fraction -= dx; // same as fraction -= 2*dx
    }
    xStart += stepx;
    fraction += dy; // same as fraction -= 2*dy
    f2Buffer.drawLine((int) xStart, (int) yStart, (int) xStart, (int) yStart);
    }
    } else {
    float fraction = (2 * dx) - dy;
    while (yStart != yEnd) {
    if (fraction >= 0) {
    xStart += stepx;
    fraction -= dy;
    }
    yStart += stepy;
    fraction += dx;
    f2Buffer.drawLine((int) xStart, (int) yStart, (int) xStart, (int) yStart);
    }
    }

    }
    }
    Attached Images Attached Images   
    Last edited by pikadrew; March 27th, 2011 at 10:57 PM. Reason: SOLVED

  2. #2
    Join Date
    Mar 2011
    Posts
    3

    Re: Bresenham Line Algorithm

    I should show you how its called as well, including the setXORmode:

    if (drawing) { // ignore if not in drawing mode

    g2Buffer.setColor(Color.white);
    g2Buffer.setXORMode(MainFrame.colour); // go into the overwrite mode
    bresLine.setStartPoint(xStart / MainFrame.hScale, yStart / MainFrame.vScale);
    bresLine.setEndPoint(xOld / MainFrame.hScale, yOld / MainFrame.vScale);
    bresLine.drawBresenham(g2Buffer);
    bresLine.setStartPoint(xStart / MainFrame.hScale, yStart / MainFrame.vScale);
    bresLine.setEndPoint(xEnd / MainFrame.hScale, yEnd / MainFrame.vScale);
    bresLine.drawBresenham(g2Buffer);
    g2Buffer.setPaintMode(); // out of XOR overwrite mode
    xOld = xEnd; // store last end point
    yOld = yEnd;
    }

    if (mouseReleased) { // final time through
    g2Buffer.setColor(MainFrame.colour);
    bresLine.setStartPoint(xStart / MainFrame.hScale, yStart / MainFrame.vScale);
    bresLine.setEndPoint(xEnd / MainFrame.hScale, yEnd / MainFrame.vScale);
    bresLine.drawBresenham(g2Buffer);
    mouseReleased = false;

    }
    }

  3. #3
    Join Date
    Mar 2011
    Posts
    3

    Re: Bresenham Line Algorithm

    SOLVED:
    f2Buffer.drawLine((int) xStart, (int) yStart, (int) xStart, (int) yStart);
    should instead be
    f2Buffer.fillRect((int) xStart, (int) yStart, 1, 1);

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center