Bresenham's line algorithm

Discussion in 'Questions (Windows Mobile)' started by taximania, Nov 8, 2008.

  1. taximania

    taximania Well-Known Member Licensed User

    I'm sending data to a graphic LCD via Bluetooth.
    I've only got a PlotPixel(x,y) available.

    Does anybody have an example of Line(x1,x2,y1,y2)

    I've googled but can't find a 'basic' algorithm, plenty in 'C' and VB,
    but it's doing my head in trying to convert it.

    TIA
     
  2. taximania

    taximania Well-Known Member Licensed User

    Ok, I've managed it, sort of :)

    Code:
    Sub Draw_Line(x1,y1,x2,y2)
    length = x2-x1
    m = (y2-y1)/(x2-x1)
    y = y1
    For i = 0 To length
    Plot(x1+i,Int(y))
    y = y + m
    Next i
    End Sub
     
  3. agraham

    agraham Expert Licensed User

    Here's the real thing, it won't leave gaps on steep lines.
    Code:
    Sub BresenhamLine(x0, y0, x1, y1)
      steep = 
    False
      
    If Abs(y1 - y0) > Abs(x1 - x0) Then
        steep = 
    True
        
    'swap(x0, y0)
        x = x0 : x0 = y0 : y0 = x      
        
    'swap(x1, y1)
        x = x1 : x1 = y1 : y1 = x   
      
    End If
      
    If x0 > x1 Then
        
    'swap(x0, x1)
        x = x0 : x0 = x1 : x1 = x            
        
    'swap(y0, y1)
        x =y0 : y0 = y1 : y1 = x
      
    End If
      deltax = Int(x1 - x0)
      deltay = Int(
    Abs(y1 - y0))
      error = Int(deltax / 
    2)
      y = y0
      
    If y0 < y1 Then
        ystep = 
    1
      
    Else
        ystep = -
    1
      
    End If
      
    For x = x0 To x1
        
    If steep Then plot(y,x) Else plot(x,y)
        error = error - deltay
        
    If error < 0 Then
          y = y + ystep
          error = error + deltax
        
    End If
      
    Next
    End Sub
     
  4. taximania

    taximania Well-Known Member Licensed User

    You never cease to amaze me Agraham.

    I found this Atari Archive website Plotting - Computer Graphics.

    Sin waves, circles, eliptical rose (?) etc, all work on my graphic Lcd.

    My, 'Draw_line', only does 'right,up', or 'right,down'.
    I'll try your code later :sign0188:
     
  5. taximania

    taximania Well-Known Member Licensed User

    After a few test, your definately right.

    The attached program works best on the desktop. It does work on the device but the SIP covers the controls.

    Circle shows a circle :sign0060:

    Rose draws a shape :signOops:

    Sin draws a sin wave :sign0148:

    The subs code maybe of help :cool:

    If you enter the x1,y1,x2,y2 values eg:

    100,0,90,200 and 'Bres' you get a good solid line.

    then try

    100,0,110,200 and 'mine'

    That is a huge difference.

    Cheer A
     

    Attached Files:

Loading...
  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice