UnderwareDESIGN

PlayBASIC => Resources => Source Codes => Topic started by: kevin on August 24, 2023, 10:04:51 AM

Title: Source Code To Draw Diagonal Banding - Learn To Code
Post by: kevin on August 24, 2023, 10:04:51 AM
  Source Code To Draw Diagonal Banding


  This routine generates a colorful diagonal pattern with bands of colors across the screen. The colors in the bands change based on the position within the diagonal pattern, creating an interesting visual effect.


  This code generates a captivating visual display by enveloping the screen or any designated surface with an array of randomly colored bands. These bands are meticulously aligned to a grid-based pattern. The algorithm determines the grid tile that each pixel corresponds to, and subsequently calculates its specific displacement within that grid. Conceptually, this process involves assessing whether the point falls within the boundaries of a right-angled triangle for every scan line. While this methodology might initially seem intricate, no simpler alternative method has presented itself at this juncture


[pbcode]

   BandWidth=100

   Bands = 10   

   dim Palette(100)
   for lp =0 to Bands
      Palette(lp) =rndrgb()
   next   

   ScreenWidth   =GetScreenWidth()
   ScreenHeight=GetScreenHeight()
   
   
   lockbuffer

   for ylp=0 to ScreenHeight-1
      OffsetY=mod(Ylp,BandWidth)

      DiagX = BandWidth-OffsetY

      ColourY=mod(Ylp/BandWidth,Bands); Xor Colour

      for xlp=0 to  ScreenWidth-1

            //  compute the grid along x axis
            GridX=Xlp / BandWidth
            ColourX =mod(GridX,Bands)

            //  Add the X and y grid positions to find
            // which bandwe're in with the palette
            Colour = mod(ColourX+ColourY,Bands)

            //  compute the Xpos to side what side of the
            //  triangle we're in; if we're higher then
            // we're in the next band of colour
            Offset =mod(xlp,BandWidth)  > DiagX
            Colour =mod(Colour + Offset , bands)

            // draw it
            dotc xlp,ylp,Palette(Colour)

      next      
   next
   unlockbuffer

   Sync
   waitkey
   waitnokey
   
      
[/pbcode]


  Learn To Code , 2D , Grid, Diagonal Bands
Title: Re: Source Code To Draw Diagonal Banding - Learn To Code
Post by: kevin on August 25, 2023, 09:16:28 PM

   This is variation of the above code; except this version will read from a collection of pictures rather than just a random palette.

   You'll have to supply it a folder with a series of images named "frame01.png" , "frame02.png"  etc etc

   it assumes all the pictures
 
[pbcode]

   path$="-LOCATION-OF-FILES-FOLDER-"

   #include "freeimage"

   openscreen 1920,1080,32,1

   Screen=NewImage(GetScreenWidth(),GetSCreenHeight(),true)



   Dim Images(100)

   Bands=0
   for lp=01 to 100
      file$="Frame"+digits$(lp,2)+".png"
      if fileexist(path$+file$)
         image=LoadNewIMage(Path$+File$,2)
         Images(Bands) = Image
         drawimage image,0,0,false
         sync
         Bands++         
      endif
   next

   ;Bands=2
   BandWidth=GetScreenWidth()/Bands

   dim Palette(Bands)
   dim IMagePOinter(Bands)
   dim IMageModulo(Bands)

   
   for lp =0 to Bands
      Palette(lp) =rndrgb()

      Image=Images(lp)
      rendertoimage Image
      lockbuffer
         ThisRGB=point(0,0)
         IMagePOinter(lp)   =getimageptr(image)
         IMageModulo(lp)   =getimagepitch(image)
      unlockbuffer

   next   
   rendertoscreen



   ScreenWidth   =GetScreenWidth()
   ScreenHeight=GetScreenHeight()
   

   rendertoimage Screen
   lockbuffer
   ThisRgb=point(0,0)

   for ylp=0 to ScreenHeight-1
      OffsetY=mod(Ylp,BandWidth)

      DiagX = BandWidth-OffsetY

      ColourY=mod(Ylp/BandWidth,Bands); Xor Colour

      for xlp=0 to  ScreenWidth-1

            //  compute the grid along x axis
            GridX=Xlp / BandWidth
            ColourX =mod(GridX,Bands)

            //  Add the X and y grid positions to find
            // which band we're in with the palette
            Colour = mod(ColourX+ColourY,Bands)

            //  compute the Xpos to side what side of the
            //  triangle we're in; if we're higher then
            // we're in the next band of colour
            Offset =mod(xlp,BandWidth)  > DiagX
            Colour =mod(Colour + Offset , bands)


            //
            Ptr =IMagePOinter(Colour)+(xlp*4)
            Ptr+=IMageModulo(Colour)*Ylp
   
            ThisRGB =peekint(Ptr)
            dotc xlp,ylp,ThisRGB

      next      
   next
   unlockbuffer
   rendertoscreen
   drawimage Screen,0,0,false
      
   Sync
      
   FreeImage_SavePNG Path$+"Output.png",Screen

   waitkey
   waitnokey
   

[/pbcode]


  Walking Out the bush - Time Lapse