Skip to content

Instantly share code, notes, and snippets.

@tokestermw
Created November 28, 2017 00:32
Show Gist options
  • Select an option

  • Save tokestermw/ca1b551158ee789bc37b96380cc9c4ed to your computer and use it in GitHub Desktop.

Select an option

Save tokestermw/ca1b551158ee789bc37b96380cc9c4ed to your computer and use it in GitHub Desktop.
Implementation of Cantor set explained here: http://natureofcode.com/book/chapter-8-fractals/
from copy import deepcopy
class Line:
def __init__(self, length: int, x: int):
self.length = length
self.x = x
def __len__(self):
return self.length
def __floordiv__(self, divisor: int):
self.length = self.length // divisor
return self
def __str__(self):
string = '\n'
string += ' ' * self.x
string += '.' * self.length
return string
def cantor_set(line: Line):
if len(line) > 1:
line //= 3
left = line.x
right = left + 2 * len(line)
for x in [left, right]:
line.x = x
print(line)
cantor_set(deepcopy(line))
if __name__ == '__main__':
line = Line(36, 0)
print(line)
cantor_set(line)
@tokestermw

tokestermw commented Nov 28, 2017

Copy link
Copy Markdown
Author
....................................

............

....

.

  .

        ....

        .

          .

                        ............

                        ....

                        .

                          .

                                ....

                                .

                                  .

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment