Menu

Line code

2020-06-02
2020-06-04
  • stan cartwright

    stan cartwright - 2020-06-02

    I looked up Bresenham's line algo and there'a a basic..just basic,not bbc basic or commodore basic but it converted easy to gcb. I did it because I had wanted to draw lines slowly ie a pixel every program loop.
    It was for a game.
    Anyway the code is simple and fast. I am amazed it worked and understand integer variables better.... the minus variables.
    https://www.youtube.com/watch?v=r0qR1BwdXf0&feature=youtu.be

    #chip mega328p,16
    #option explicit
    #include <glcd.h>
    #include <uno_mega328p.h >
    
        #define GLCD_TYPE GLCD_TYPE_ILI9341
    
        'Pin mappings for SPI - this GLCD driver supports Hardware SPI and Software SPI
        #define GLCD_DC       DIGITAL_8         ' Data command line
        #define GLCD_CS       DIGITAL_10          ' Chip select line
        #define GLCD_RESET    DIGITAL_9         ' Reset line
    
        #define GLCD_DI       DIGITAL_12          ' Data in | MISO    - Not used therefore not really required
        #define GLCD_DO       DIGITAL_11          ' Data out | MOSI
        #define GLCD_SCK      DIGITAL_13          ' Clock Line
    GLCDRotate ( Landscape_Rev )
    GLCDCLS ILI9341_Black
    
     dim dx,dy,sx,sy,er,e2 as integer
     dim x2,x1,y1,y2 as word
    
     x1=random :y1=random:x2=random:y2=random
    
     ;this the draw line routine
    draw:
     DX = ABS(X2 - X1):SX = -1:IF X1 < X2 THEN SX = 1
     DY = ABS(Y2 - Y1):SY = -1:IF Y1 < Y2 THEN SY = 1
     ER = -DY:IF DX > DY THEN ER = DX
     ER = ER / 2
     pixel:
     pset X1,Y1,ILI9341_WHITE
     IF X1 = X2 AND Y1 = Y2 THEN x1=x2:y1=y2:x2=random:y2=random:goto draw
     E2 = ER
     IF E2 > -DX THEN ER = ER - DY:X1 = X1 + SX
     IF E2 < DY THEN ER = ER + DX:Y1 = Y1 + SY
     GOTO pixel
    

    slow cos of using random

     

    Last edit: stan cartwright 2020-06-03
  • stan cartwright

    stan cartwright - 2020-06-03

    so short. got rid of x1=x2 and y1=y2 as it wasted a test every loop.
    Getting rid of the goto ...I gave up.
    This is pretty fast. how to time it draw 1000 lines and compare to glcd

    sub draw (in x1,in y1,in x2,in y2,in colour)
     DX = ABS(X2 - X1):SX = -1:IF X1 < X2 THEN SX = 1
     DY = ABS(Y2 - Y1):SY = -1:IF Y1 < Y2 THEN SY = 1
     ER = -DY:IF DX > DY THEN ER = DX
     ER = ER / 2
    pixel:
    pset X1,Y1,colour
    if x1=x2 then
      if y1=y2 then return
    end if
     E2 = ER
     IF E2 > -DX THEN ER = ER - DY:X1 = X1 + SX
     IF E2 < DY THEN ER = ER + DX:Y1 = Y1 + SY
    GOTO pixel
    
     

    Last edit: stan cartwright 2020-06-03
  • stan cartwright

    stan cartwright - 2020-06-03

    the line draw in glcd.h is longer

    Sub Line(In LineX1 as word, In LineY1 as word, In LineX2 as word, In LineY2 as word, Optional In LineColour as word = GLCDForeground)
    
          dim LineStepX as integer
          dim LineStepY as integer
          dim LineDiffX, LineDiffY as integer
          dim LineDiffX_x2, LineDiffY_x2 as integer
          dim LineErr as integer
    
    
    
          LineDiffX = 0
          LineDiffY = 0
          LineStepX = 0
          LineStepY = 0
          LineDiffX_x2 = 0
          LineDiffY_x2 = 0
          LineErr = 0
    
    
          LineDiffX =  LineX2 -   LineX1
          LineDiffY =  LineY2 -   LineY1
    
          if (LineDiffX > 0) then
                  LineStepX = 1
          else
                  LineStepX = -1
          end if
    
          if (LineDiffY > 0) then
              LineStepY = 1
           else
              LineStepY = -1
          end if
    
          LineDiffX = LineStepX * LineDiffX
          LineDiffY = LineStepY * LineDiffY
    
          LineDiffX_x2 = LineDiffX*2
          LineDiffY_x2 = LineDiffY*2
    
          if ( LineDiffX >= LineDiffY) then
    
              LineErr = LineDiffY_x2 - LineDiffX
    
              do while (   LineX1 <>  LineX2 )
    
                  PSet (   LineX1,   LineY1, LineColour )
                  LineX1 += LineStepX
                  if ( LineErr < 0) then
                      LineErr += LineDiffY_x2
                  else
                      LineErr += ( LineDiffY_x2 - LineDiffX_x2 )
                      LineY1 += LineStepY
                  end if
              loop
    
              PSet (   LineX1,   LineY1, LineColour )
          else
    
              LineErr = LineDiffX_x2 - LineDiffY
              do while (   LineY1 <>  LineY2)
                  PSet (   LineX1,   LineY1, LineColour )
                  LineY1 += LineStepY
                  if ( LineErr < 0) then
                      LineErr += LineDiffX_x2
                   else
                      LineErr += ( LineDiffX_x2 - LineDiffY_x2 )
                      LineX1 += LineStepX
                  end if
              loop
              PSet (   LineX1,   LineY1, LineColour )
    
          end if
    
    
    
    end sub
    
     
  • stan cartwright

    stan cartwright - 2020-06-03

    In my code there's er=er/2. would rotate er right simple be faster?
    it's in the init not the main loop so only occurs once but if I draw lots of lines it might matter.
    the init in the glcd line uses multiply a few times.

     
  • stan cartwright

    stan cartwright - 2020-06-04

    seems gcb line drew a line 17.19 times a second
    and mine 16.88 times a second.

     

Log in to post a comment.

Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.