Task

Using the data storage type defined on the Bitmap page for raster graphics images,

draw a line given two points with Bresenham's line algorithm.




//  Daw a line using Bresenham's line algorithm.
//  #1=x1, #2=y1; #3=x2, #4=y2
 
:DRAW_LINE:
Num_Push(31,35)
#31 = abs(#3-#1)    // x distance
#32 = abs(#4-#2)    // y distance
if (#4-#2 < -#31 || #3-#1 <= -#32) {
    #99=#1; #1=#3; #3=#99 // swap start and end points
    #99=#2; #2=#4; #4=#99
}
if (#1 < #3) { #34=1 } else { #34=-1 }  // x step
if (#2 < #4) { #35=1 } else { #35=-1 }  // y step
 
if (#32 > #31) {    // steep angle, step by Y
    #33 = #32 / 2   // error distance
    while (#2 <= #4) {
  Call("DRAW_PIXEL")
  #33 -= #31
  if (#33 < 0) {
      #1 += #34   // move right
      #33 += #32
  }
  #2++      // move up
    }
} else {      // not steep, step by X
    #33 = #31 / 2
    while (#1 <= #3) {
  Call("DRAW_PIXEL")
  #33 -= #32
  if (#33 < 0) {
      #2 += #35   // move up
      #33 += #31
  }
  #1++      // move right
    }
}
Num_Pop(31,35)
return