Created
November 28, 2017 00:32
-
-
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/
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.