-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathdevils_staircase.py
63 lines (48 loc) · 1.51 KB
/
devils_staircase.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
import turtle
def decimalFractionToTernaryFraction(decimal, precision=10):
result = "0."
for _ in range(precision):
decimal *= 3
digit = int(decimal)
result += str(digit)
decimal -= digit
return result
def binaryFractionToDecimalFraction(binaryFraction):
decimalFraction = 0.0
for i, bit in enumerate(binaryFraction[2:]):
decimalFraction += int(bit) * (2 ** -(i + 1))
return decimalFraction
# https://en.wikipedia.org/wiki/Cantor_function
def cantorFunction(x):
numberString = decimalFractionToTernaryFraction(x)
firstOneIndex = numberString.find("1")
if firstOneIndex != -1:
numberString = numberString[:firstOneIndex + 1] + "0" * (len(numberString) - firstOneIndex - 1)
numberString = numberString.replace("2", "1")
return float(binaryFractionToDecimalFraction(numberString))
def devilsStaircase(limit):
x = []
y = []
for i in range(0, limit):
n = i / (limit)
c = cantorFunction(n)
x.append(n)
y.append(c)
return x, y
def drawDevilsStaircase(xCoords, yCoords):
window = turtle.Screen()
window.bgcolor("black")
turtle.setworldcoordinates(0, 0, 1, 1)
turt = turtle.Turtle()
turt.color("white")
turt.speed(100)
turt.penup()
turt.goto(0, 0)
turt.width(3)
turt.pendown()
for x, y in zip(xCoords, yCoords):
turt.goto(x, y)
window.exitonclick()
if __name__ == "__main__":
x, y = devilsStaircase(1000)
drawDevilsStaircase(x, y)