How to resolve the algorithm Bitmap/Bresenham's line algorithm step by step in the PureBasic programming language

Published on 12 May 2024 09:40 PM

How to resolve the algorithm Bitmap/Bresenham's line algorithm step by step in the PureBasic programming language

Table of Contents

Problem Statement

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.

Let's start with the solution:

Step by Step solution about How to resolve the algorithm Bitmap/Bresenham's line algorithm step by step in the PureBasic programming language

Source code in the purebasic programming language

Procedure BresenhamLine(x0 ,y0 ,x1 ,y1)
     If Abs(y1 - y0) > Abs(x1 - x0);
        steep =#True 
        Swap x0, y0
        Swap x1, y1
     EndIf    
     If x0 > x1 
         Swap x0, x1
         Swap y0, y1
     EndIf 
     deltax = x1 - x0
     deltay = Abs(y1 - y0)
     error = deltax / 2
     y = y0
     If y0 < y1  
        ystep = 1
     Else
        ystep = -1 
     EndIf 
     For x = x0 To x1
         If steep 
           Plot(y,x)
         Else 
           Plot(x,y)
         EndIf
         error - deltay
         If error < 0 
             y + ystep
             error + deltax
         EndIf
     Next        
EndProcedure

#Window1   = 0
#Image1    = 0
#ImgGadget = 0
#width     = 300
#height    = 300

Define.i Event
Define.f Angle

If OpenWindow(#Window1, 0, 0, #width, #height, "Bresenham's Line PureBasic Example", #PB_Window_SystemMenu|#PB_Window_ScreenCentered)
   If CreateImage(#Image1, #width, #height)
      ImageGadget(#ImgGadget, 0, 0, #width, #height, ImageID(#Image1))
      StartDrawing(ImageOutput(#Image1))
      FillArea(0,0,-1,$FFFFFF) :FrontColor(0)
      While Angle < 2*#PI
        BresenhamLine(150,150,150+Cos(Angle)*120,150+Sin(Angle)*120)
        Angle + #PI/60     
      Wend 
      
      StopDrawing()
      SetGadgetState(#ImgGadget, ImageID(#Image1))
      Repeat
        Event = WaitWindowEvent()
      Until Event = #PB_Event_CloseWindow
   EndIf
EndIf

  

You may also check:How to resolve the algorithm Hello world/Text step by step in the Slate programming language
You may also check:How to resolve the algorithm Arithmetic evaluation step by step in the Clojure programming language
You may also check:How to resolve the algorithm Handle a signal step by step in the PL/I programming language
You may also check:How to resolve the algorithm Arrays step by step in the FutureBasic programming language
You may also check:How to resolve the algorithm Loops/N plus one half step by step in the ArnoldC programming language