How to resolve the algorithm Cantor set step by step in the BASIC programming language

Published on 12 May 2024 09:40 PM

How to resolve the algorithm Cantor set step by step in the BASIC programming language

Table of Contents

Problem Statement

Draw a Cantor set.

See details at this Wikipedia webpage:   Cantor set

Let's start with the solution:

Step by Step solution about How to resolve the algorithm Cantor set step by step in the BASIC programming language

Source code in the basic programming language

10 DEFINT A-Z
20 N = 4
30 W = 3^(N-1)
40 S = W
50 L$ = STRING$(W, "#")
60 PRINT L$
70 IF S = 1 THEN END
80 S = S\3
90 P = 1
100 IF P >= W-S GOTO 60
110 P = P+S
120 MID$(L$,P,S) = SPACE$(S)
130 P = P+S
140 GOTO 100

global ancho, alto, intervalo
ancho = 81 : alto = 5
dim intervalo(alto, ancho)

subroutine Cantor()
	for i = 0 to alto - 1
		for j = 0 to ancho - 1
			intervalo[i, j] = "■"
		next j
	next i
end subroutine

subroutine ConjCantor(inicio, longitud, indice)
	segmento = longitud / 3
	if segmento = 0 then return
	for i = indice to alto - 1
		for j = inicio + segmento to inicio + segmento * 2 - 1
			intervalo[i, j] = " "
		next j
	next i
	call ConjCantor(inicio, segmento, indice + 1)
	call ConjCantor(inicio + segmento * 2, segmento, indice + 1)
end subroutine

call Cantor()
call ConjCantor(0, ancho, 1)
for i = 0 to alto - 1
	for j = 0 to ancho - 1
		print intervalo[i, j];
	next j
	print
next i
end

100 cls
110 ancho = 81
120 alto = 5
130 dim intervalo$(alto,ancho)
140 '
150 sub cantor()
160   for i = 0 to alto-1
170     for j = 0 to ancho-1
180       intervalo$(i,j) = chr$(254)
190     next j
200   next i
210 end sub
220 '
230 sub conjcantor(inicio,longitud,indice)
240   segmento = longitud/3
250   if segmento = 0 then exit sub
260   for i = indice to alto-1
270     for j = inicio+segmento to inicio+segmento*2-1
280       intervalo$(i,j) = chr$(32)
290     next j
300   next i
310   conjcantor(inicio,segmento,indice+1)
320   conjcantor(inicio+segmento*2,segmento,indice+1)
330 end sub
340 '
350 cantor()
360 conjcantor(0,ancho,1)
370 for i = 0 to alto-1
380   for j = 0 to ancho-1
390     print intervalo$(i,j);
400   next j
410   print
420 next i
430 end


SUB Cantor
    FOR i = 0 TO alto - 1
        FOR j = 0 TO ancho - 1
            intervalo$(i, j) = CHR$(254) '"#"
        NEXT j
    NEXT i
END SUB

SUB ConjCantor (inicio, longitud, indice)
    segmento = INT(longitud / 3)
    IF segmento = 0 THEN EXIT SUB
    FOR i = indice TO alto - 1
        FOR j = inicio + segmento TO inicio + segmento * 2 - 1
            intervalo$(i, j) = CHR$(32) '" "
        NEXT j
    NEXT i
    CALL ConjCantor(inicio, segmento, indice + 1)
    CALL ConjCantor(inicio + segmento * 2, segmento, indice + 1)
END SUB

CONST ancho = 81
CONST alto = 5
DIM SHARED intervalo$(alto, ancho)

CLS
CALL Cantor
CALL ConjCantor(0, ancho, 1)
FOR i = 0 TO alto - 1
    FOR j = 0 TO ancho - 1
        PRINT intervalo$(i, j);
    NEXT j
    PRINT
NEXT i
END


LET ancho = 81
LET alto = 5
DIM intervalo$(0,0)
MAT REDIM intervalo$(0 TO alto, 0 TO ancho)

SUB cantor
    FOR i = 0 TO alto-1
        FOR j = 0 TO ancho-1
            LET intervalo$(i, j) = "#"      !CHR$(254)
        NEXT j
    NEXT i
END SUB

SUB conjcantor (inicio,longitud,indice)
    LET segmento = INT(longitud/3)
    IF segmento = 0 THEN EXIT SUB
    FOR i = indice TO alto-1
        FOR j = inicio+segmento TO inicio+segmento*2-1
            LET intervalo$(i, j) = CHR$(32)      !" "
        NEXT j
    NEXT i
    CALL conjcantor (inicio, segmento, indice+1)
    CALL conjcantor (inicio+segmento*2, segmento, indice+1)
END SUB

CALL cantor
CALL conjcantor (0, ancho, 1)
FOR i = 0 TO alto-1
    FOR j = 0 TO ancho-1
        PRINT intervalo$(i, j);
    NEXT j
    PRINT
NEXT i
END


ancho = 81 
alto = 5
dim intervalo$(alto, ancho)

Cantor()
ConjCantor(0, ancho, 1)
for i = 0 to alto - 1
  for j = 0 to ancho - 1
    print intervalo$(i, j);
  next j
  print
next i
end

sub Cantor()
  for i = 0 to alto - 1
    for j = 0 to ancho - 1
      intervalo$(i, j) = chr$(254)  //"#"
    next j
  next i
end sub

sub ConjCantor(inicio, longitud, indice)
  segmento = longitud / 3
  if segmento = 0  return
  for i = indice to alto - 1
    for j = inicio + segmento to inicio + segmento * 2 - 1
      intervalo$(i, j) = " "
    next j
  next i
  ConjCantor(inicio, segmento, indice + 1)
  ConjCantor(inicio + segmento * 2, segmento, indice + 1)
end sub

  

You may also check:How to resolve the algorithm Function definition step by step in the hexiscript programming language
You may also check:How to resolve the algorithm Fibonacci sequence step by step in the BASIC programming language
You may also check:How to resolve the algorithm Latin Squares in reduced form step by step in the J programming language
You may also check:How to resolve the algorithm Generate lower case ASCII alphabet step by step in the Python programming language
You may also check:How to resolve the algorithm Ramanujan's constant step by step in the Perl programming language