Author Topic: Cantor Set (Rosetta Code task)  (Read 2939 times)

0 Members and 1 Guest are viewing this topic.

Offline AndyA

  • Newbie
  • Posts: 73
    • View Profile
Cantor Set (Rosetta Code task)
« on: April 13, 2021, 11:02:30 pm »
https://rosettacode.org/wiki/Cantor_set


The task description basically says draw the Cantor Set.
It's refers to the Wikipedia page here https://en.wikipedia.org/wiki/Cantor_set

[begin edit] Had to delete timing variables not used for posting [end edit]

Code: QB64: [Select]
  1. _Title "Cantor Set"
  2.  
  3. Const sw% = 800
  4. Const sh% = 200
  5. Const wide% = 729
  6. Const high% = 7
  7.  
  8. Screen _NewImage(sw, sh, 8)
  9.  
  10. Dim Shared As Integer a(wide, high)
  11.  
  12. Call calc(0, wide, 1)
  13. Call CantorSet
  14.  
  15.  
  16. Sub calc (start As Integer, length As Integer, index As Integer)
  17.     Dim As Integer i, j, newLength
  18.     newLength = length \ 3
  19.     If newLength = 0 Then Exit Sub
  20.     For j = index To high - 1
  21.         For i = start + newLength To start + newLength * 2 - 1
  22.             a(i, j) = 1
  23.         Next
  24.     Next
  25.     Call calc(start, newLength, index + 1)
  26.     Call calc(start + newLength * 2, newLength, index + 1)
  27.  
  28. Sub CantorSet
  29.     Dim As Integer i, j, x, y
  30.     For y = 0 To high - 1
  31.         j = y + 1
  32.         For x = 0 To wide - 1
  33.             i = x + 34
  34.             If a(x, y) = 0 Then Line (i, j * 24 - 5)-(i, j * 24 + 17)
  35.         Next
  36.     Next
« Last Edit: April 13, 2021, 11:08:03 pm by AndyA »