Create an Account
username: password:
 
  MemeStreams Logo

Bresenham's line algorithm

search

Acidus
Picture of Acidus
My Blog
My Profile
My Audience
My Sources
Send Me a Message

sponsored links

Acidus's topics
Arts
Business
Games
Health and Wellness
Home and Garden
Miscellaneous
Current Events
Recreation
Local Information
Science
Society
Sports
Technology

support us

Get MemeStreams Stuff!


 
Bresenham's line algorithm
Topic: Miscellaneous 5:08 am EST, Feb 18, 2008
DrawingSpace.prototype.drawLine = function(x0,y0,x1,y1) {

    var steep = Math.abs(y1 - y0) > Math.abs(x1 - x0);
    var tmp = 0;
 
    if(steep) {
        //swap(x0, y0)
        tmp = y0;
        y0 = x0;
        x0 = tmp;
        //swap(x1, y1)
        tmp = y1;
        y1 = x1;
        x1 = tmp;
    }
    if(x0 > x1) {
        //swap(x0, x1)
        tmp = x1;
        x1 = x0;
        x0 = tmp;
        //swap(y0, y1)
        tmp = y0;
        y0 = y1;
        y1 = tmp;    
    }
    
    var deltax = x1 - x0;
    var deltay = Math.abs(y1 - y0)
    var error = -(deltax + 1) / 2
    var ystep;
    
    var y = y0;
    if(y0 < y1) {
        ystep = 1;
    } else {
        ystep = -1;
    }
    
    for(var x = x0; x <=x1; x++) {
        if(steep) {
            this.buffer.setXY(y,x);
        } else {
            this.buffer.setXY(x,y);
        }
        error += deltay;
        if(error >=0) {
            y += ystep;
            error -=deltax;
        }
    }

}

Now why would you ever need an integer optimized line drawing algorithm in JavaScript? :-)

Bresenham's line algorithm



 
 
Powered By Industrial Memetics
RSS2.0