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

Published on 12 May 2024 09:40 PM

How to resolve the algorithm Cantor set step by step in the Smalltalk 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 Smalltalk programming language

Source code in the smalltalk programming language

Object subclass: CantorSet [

    | intervals |

    CantorSet class >> new
        [^self basicNew
            initialize;
            yourself]

    initialize
        [intervals := Array with: (CantorInterval
            from: 0
            to: 1)]

    split
        [intervals := intervals gather: [:each | each split]]

    displayOn: aStream atScale: aNumber
        [| current |
        current := 0.
        intervals do:
            [:each |
            (each start - current) * aNumber timesRepeat: [aStream space].
            each length * aNumber timesRepeat: [aStream nextPut: $#].
            current := each stop].
        aStream nl]
]

Interval subclass: CantorInterval [

    split
        [| oneThird left right |
        oneThird := self length / 3.
        left := self class
            from: start
            to: start + oneThird.
        right := self class
            from: stop - oneThird
            to: stop.
        ^Array
            with: left
            with: right]

    start  [^start]
    stop   [^stop]
    length [^stop - start]

    printOn: aStream
        [aStream << ('%1[%2,%3]' % {self class name. start. stop})]
]

Object subclass: TestCantor [

    TestCantor class >> iterations: anInteger
        [| cantorset scale count |
        scale := 3 raisedTo: anInteger. "Make smallest interval 1"
        count := 0.
        cantorset := CantorSet new.

        [cantorset
            displayOn: Transcript
            atScale: scale.
        count < anInteger] whileTrue:
            [cantorset split.
            count := count + 1]]
]

TestCantor iterations: 4.


  

You may also check:How to resolve the algorithm Multisplit step by step in the zkl programming language
You may also check:How to resolve the algorithm Hello world/Text step by step in the ferite programming language
You may also check:How to resolve the algorithm Ackermann function step by step in the Delphi programming language
You may also check:How to resolve the algorithm Towers of Hanoi step by step in the Haskell programming language
You may also check:How to resolve the algorithm Range extraction step by step in the Elixir programming language