Shop OBEX P1 Docs P2 Docs Learn Events
Bresenham ALGORITHM — Parallax Forums

Bresenham ALGORITHM

mxg331mxg331 Posts: 5
edited 2009-05-23 22:57 in General Discussion
I've built a memory·mapped VGA interface using the SX 48.· I've implemented many video functions such as Sprites, draw pixel, and draw box but the line has got me stumped. Does anyone know how to implement a line using the Bresenham ALGORITHM or otherwise?· Thanks!

Post Edited (mxg331) : 5/23/2009 12:28:41 AM GMT

Comments

  • LeonLeon Posts: 7,620
    edited 2009-05-22 22:13
    Don't you mean the Bresenham algorithm?

    Leon

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Amateur radio callsign: G1HSM
    Suzuki SV1000S motorcycle
  • mxg331mxg331 Posts: 5
    edited 2009-05-23 00:28
    Yes, but any line algorithm will do.· I've already developed my own but it has many issues.
  • BeanBean Posts: 8,129
    edited 2009-05-23 02:04
    Here is one in SX/B

    ' Variables used by graphics subroutines 
    cnt   VAR Byte 
    cnt2  VAR Byte 
    x1    VAR Byte 
    x2    VAR Byte 
    y1    VAR Byte 
    y2    VAR Byte 
    color VAR Byte 
    
    Line SUB 4 
    
    Line: ' (x1,y1,x2,y2) ; Set color before calling 
      x1 = __PARAM1 
      y1 = __PARAM2 
      x2 = __PARAM3 - x1 
      y2 = __PARAM4 - y1 
      IF x2 >= y2 THEN 
        cnt2 = x2 >> 1 
        FOR cnt = 0 TO x2 
          PutPixel x1, y1, color 
          INC x1 
          IF cnt2 >= y2 THEN 
            cnt2 = cnt2 - y2 
          ELSE 
            cnt2 = cnt2 - y2 
            cnt2 = cnt2 + x2 
            INC y1 
          ENDIF 
        NEXT 
      ELSE 
        cnt2 = y2 >> 1 
        FOR cnt = 0 TO y2 
          PutPixel x1, y1, color 
          INC y1 
          IF cnt2 >= x2 THEN 
            cnt2 = cnt2 - x2 
          ELSE 
            cnt2 = cnt2 - x2 
            cnt2 = cnt2 + y2 
            INC x1 
          ENDIF 
        NEXT 
      ENDIF 
      x2 = x1 
      y2 = y1 
    RETURN
    



    Bean

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
    There is a fine line between arrogance and confidence. Make sure you don't cross it...

    ·
  • mxg331mxg331 Posts: 5
    edited 2009-05-23 22:25
    Thank you for your response.· As I'm sure you can appreciate, the process I'm·implementing·is very time critical.· I would prefer (need) a line algorithm in SX assembly.· Thanks to all who are thinking about this!
  • JonnyMacJonnyMac Posts: 9,213
    edited 2009-05-23 22:57
    Remember, SX/B compiles to Assembly and you may find that Bean's code works as well as you need. If nothing else, run it through the compiler and then take the output and see if you can optimize it (I do this a lot as it helps me learn Assembly).
Sign In or Register to comment.