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.




 
   10 rem bresenham line algorihm
   20 rem translated from purebasic
   30 x0=10 : rem start x co-ord
   40 y0=15 : rem start y co-ord
   50 x1=30 : rem end x co-ord
   60 y1=20 : rem end y co-ord
   70 se=0 :  rem 0 = steep 1 = !steep
   80 ns=25 : rem num segments
   90 dim pt(ns,2) : rem points in line
  100 sc=1024 : rem start of screen memory
  110 sw=40   : rem screen width
  120 sh=25   : rem screen height
  130 pc=42   : rem plot character '*'
  140 gosub 1000
  150 end
 1000 rem plot line
 1010 if abs(y1-y0)>abs(x1-x0) then se=1:tp=y0:y0=x0:x0=tp:tp=y1:y1=x1:x1=tp
 1020 if x0>x1 then tp=x1:x1=x0:x0=tp:tp=y1:y1=y0:y0=tp
 1030 dx=x1-x0
 1040 dy=abs(y1-y0)
 1050 er=dx/2
 1060 y=y0
 1070 ys=-1
 1080 if y0<y1 then ys = 1
 1090 for x=x0 to x1
 1100 if se=1 then p0=y: p1=x:gosub 2000:goto 1120
 1110 p0=x: p1=y: gosub 2000
 1120 er=er-dy
 1130 if er<0 then y=y+ys:er=er+dx
 1140 next x
 1150 return
 2000 rem plot individual point
 2010 rem p0 == plot point x
 2020 rem p1 == plot point y
 2030 sl=p0+(p1*sw)
 2040 rem make sure we dont write beyond screen memory
 2050 if sl<(sw*sh) then poke sc+sl,pc
 2060 return
 