How to resolve the algorithm Munching squares step by step in the BASIC programming language

Published on 12 May 2024 09:40 PM

How to resolve the algorithm Munching squares step by step in the BASIC programming language

Table of Contents

Problem Statement

Render a graphical pattern where each pixel is colored by the value of 'x xor y' from an arbitrary color table.

Let's start with the solution:

Step by Step solution about How to resolve the algorithm Munching squares step by step in the BASIC programming language

Source code in the basic programming language

 100  DATA 0,2, 6,10,5, 6, 7,15
 110  DATA 0,1, 3,10,5, 3,11,15
 120  DATA 0,8, 9,10,5, 9,13,15
 130  DATA 0,4,12,10,5,12,14,15
 140  LET C = 7
 150  POKE 768,169: REM LDA #
 160  POKE 770,073: REM EOR #
 170  POKE 772,133: REM STA
 180  POKE 773,235: REM   $EB
 190  POKE 774,096: REM RTS
 200  GR 
 210  FOR H = 0 TO 1
 220      FOR W = 0 TO 1
 230          FOR S = 0 TO C
 240              READ C(S)
 250          NEXT S
 260          FOR Y = 0 TO C
 270              POKE 769,Y
 280              LET Y1 = H * S * 2 + Y * 2
 290              FOR X = 0 TO C
 300                  POKE 771,X
 310                  CALL 768
 320                  COLOR= C( PEEK (235))
 330                  VLIN Y1,Y1 + 1 AT W * S + X
 340                  NEXT X,Y,W,H

      size% = 256

      VDU 23,22,size%;size%;8,8,16,0
      OFF

      DIM coltab%(size%-1)
      FOR I% = 0 TO size%-1
        coltab%(I%) = ((I% AND &FF) * &010101) EOR &FF0000
      NEXT

      GCOL 1
      FOR I% = 0 TO size%-1
        FOR J% = 0 TO size%-1
          C% = coltab%(I% EOR J%)
          COLOUR 1, C%, C%>>8, C%>>16
          PLOT I%*2, J%*2
        NEXT
      NEXT I%

      REPEAT WAIT 1 : UNTIL FALSE


100 FOR I=0 TO 24
110 : Y=INT(I*127/24)
120 : FOR J=0 TO 39
130 :   X=INT(J*127/39)
140 :   HL = (X OR Y) AND NOT (X AND Y)
150 :   H = INT(HL / 8)
160 :   L = HL - 8 * H
170 :   POKE 2048+I*40+J,L*16+H
180 :   POKE 3072+I*40+J,160
190 : NEXT J
210 NEXT I
220 GETKEY K$


let s = 255

for y = 0 to s

	for x = 0 to s

		let r = x ~ y
		fgcolor  r, r * 2, r * 3
		dot x, y

		wait

	next x

next y


' version 03-11-2016
' compile with: fbc -s gui

Dim As ULong x, y, r, w = 256

ScreenRes w, w, 32

For x = 0 To w -1
    For y = 0 To w -1
        r =(x Xor y) And 255
        PSet(x, y), RGB(r, r , r)         ' gray scale
        ' PSet(x, y), RGB(r, 255 - r, 0)  ' red + green
        ' PSet(x, y), RGB(r, 0, 0)        ' red
    Next
Next

' empty keyboard buffer
While Inkey <> "" : Wend
WindowTitle "Close window or hit any key to end program"
Sleep
End

    nomainwin

    w =512
    '   allow for title bar and window border
    WindowWidth  =w +2
    WindowHeight =w +34

    open "XOR Pattern" for graphics_nsb_nf as #w

    #w "trapclose quit"

    #w "down"

    for x =0 to w -1
        for y =0 to w -1
            b =( x xor y) and 255
            print b
            #w "color "; 255 -b; " "; b /2; " "; b
            #w "set "; x; " "; w -y -1
            scan
        next y
    next x

    #w "flush"

    wait

    sub quit j$
    close #w
    end
    end sub

' Munching squares - smallbasic  - 27/07/2018
  size=256
  GraphicsWindow.Width=size
  GraphicsWindow.Height=size
  For i=0 To size-1
    For j=0 To size-1
      BitXor() 'color=i Xor j
      GraphicsWindow.SetPixel(i,j,GraphicsWindow.GetColorFromRGB(0,color,color))
    EndFor
  EndFor

Sub BitXor '(i,j)->color
  n=i
  Int2Bit()
  ib=ret
  n=j
  Int2Bit()
  jb=ret
  color=0
  For k=1 to 8
    ki=Text.GetSubText(ib,k,1)
    kj=Text.GetSubText(jb,k,1)
    If ki="1" Or kj="1" Then
      kk="1"
    Else
      kk="0"
    EndIf
    If ki="1" And kj="1" Then
      kk="0"
    EndIf
    color=2*color+kk
  EndFor
EndSub 

Sub Int2Bit 'n->ret
  x=n
  ret=""
  For k=1 to 8
    t=Math.Floor(x/2)
    r=Math.Remainder(x,2)
    ret=Text.Append(r,ret)
    x=t
  EndFor
EndSub

#palletteSize = 128
Procedure.f XorPattern(x, y) ;compute the gradient value from the pixel values
  Protected result = x ! y
  ProcedureReturn Mod(result, #palletteSize) / #palletteSize
EndProcedure

Procedure drawPattern()
  StartDrawing(ImageOutput(0))
    DrawingMode(#PB_2DDrawing_Gradient)
    CustomGradient(@XorPattern())
    ;specify a gradient pallette from which only specific indexes will be used
    For i = 1 To #palletteSize 
      GradientColor(1 / i, i * $BACE9B) ; or alternatively use $BEEFDEAD
    Next 
    Box(0, 0, ImageWidth(0), ImageHeight(0))
  StopDrawing()
EndProcedure

If OpenWindow(0, 0, 0, 128, 128, "XOR Pattern", #PB_Window_SystemMenu)
  CreateImage(0, WindowWidth(0), WindowHeight(0))
  drawPattern()
  ImageGadget(0, 0, 0, ImageWidth(0), ImageHeight(0), ImageID(0))
  Repeat
    event = WaitWindowEvent(20)
  Until event = #PB_Event_CloseWindow
EndIf

w = 254

SCREEN 13
VIEW (0, 0)-(w / 2, w / 2), , 0

FOR x = 0 TO w
    FOR y = 0 TO w
        COLOR ((x XOR y) AND 255)
        PSET (x, y)
    NEXT y
NEXT x


'Munching squares
DECLARE SUB PaintCanvas

CREATE Form AS QForm
  ClientWidth  = 256
  ClientHeight = 256
  CREATE Canvas AS QCanvas
    Height = Form.ClientHeight
    Width  = Form.ClientWidth
    OnPaint = PaintCanvas
  END CREATE
END CREATE

SUB PaintCanvas
  FOR X = 0 TO Canvas.Width - 1
    FOR Y = 0 TO Canvas.Width - 1
      R = (X XOR Y) AND 255
      Canvas.Pset(X, Y, RGB(R, R, R)) ' gray scale
      'Canvas.Pset(X, Y, RGB(R, 255 - R, 0)) ' red + green
      'Canvas.Pset(X, Y, RGB(R, 0, 0)) ' red
    NEXT Y
  NEXT X
END SUB

Form.ShowModal

w = 100
graphic #g, w,w
for x = 0 to w
  for y = 0 to w
    b = (x xor y) and 255
    #g color(255 -b,b /2,b)
    #g "set "; x; " "; w -y -1
  next y
next x
render #g 
#g "flush"

PROGRAM:XORPATT
" •.-,+-°-1+o*:πOX"→Str1

ClrHome

{0,0,0,0}→L1
{0,0,0,0)→L2

For(I,1,8,1)
For(J,1,16,1)
J→A
I→B

If A>8
Then
A-8→A
1→L1(1)
Else
0→L1(1)
End

If A>4
Then
A-4→A
1→L1(2)
Else
0→L1(2)
End

If A>2
Then
A-2→A
1→L1(3)
Else
0→L1(3)
End

If A>1
Then
1→L1(4)
Else
0→L1(4)
End

0→L2(1)

If B>4
Then
B-4→B
1→L2(2)
Else
0→L2(2)
End

If B>2
Then
B-2→B
1→L2(3)
Else
0→L2(3)
End

If B>1
Then
1→L2(4)
Else
0→L2(4)
End

L1≠L2→L3
8L3(1)+4L3(2)+2L3(3)+L3(4)→C
Output(I,J,sub(Str1,C+1,1))

End
End
Pause

' Munching squares - 27/07/2018
Public Class MunchingSquares
    Const xsize = 256
    Dim BMP As New Drawing.Bitmap(xsize, xsize)
    Dim GFX As Graphics = Graphics.FromImage(BMP)

    Private Sub MunchingSquares_Paint(sender As Object, e As PaintEventArgs) Handles Me.Paint
        'draw
        Dim MyGraph As Graphics = Me.CreateGraphics
        Dim nColor As Color
        Dim i, j, cp As Integer
        xPictureBox.Image = BMP
        For i = 0 To xsize - 1
            For j = 0 To xsize - 1
                cp = i Xor j
                nColor = Color.FromArgb(cp, 0, cp)
                BMP.SetPixel(i, j, nColor)
            Next j
        Next i
    End Sub 'Paint

End Class


w = 256
 
open window w, w
 
For x = 0 To w-1
    For y = 0 To w-1
        r =and(xor(x, y), 255)
        color r, and(r*2, 255), and(r*3, 255)
        dot x, y
    Next
Next

  

You may also check:How to resolve the algorithm Array concatenation step by step in the Avail programming language
You may also check:How to resolve the algorithm Roman numerals/Decode step by step in the C# programming language
You may also check:How to resolve the algorithm Matrix multiplication step by step in the Maxima programming language
You may also check:How to resolve the algorithm Sierpinski triangle step by step in the Comal programming language
You may also check:How to resolve the algorithm Formatted numeric output step by step in the BASIC programming language