Created
October 5, 2017 12:35
-
-
Save cavedave/e4ab06fa69f035c2f6414a3a071c80f1 to your computer and use it in GitHub Desktop.
Python Logo Turtle code to draw paths with numbers based on numbers. A picture made of all, odd, even and prime numbers can be made with this code
This file contains 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
# !/usr/bin/python3 | |
import turtle | |
import numpy as np | |
import math | |
#the number of digits to use | |
numDigits = 1000000 | |
changeColorInc = numDigits / 10 | |
colors = iter(["#a6cee3", "#1f78b4", "#b2df8a", "#33a02c", "#fb9a99", "#e31a1c", "#fdbf6f", "#ff7f00", "#cab2d6", "#551A8B"]) | |
#scale = 10.99 #100 | |
#scale = 1.99 #1k | |
#scale = 0.699#10k | |
#scale = 0.680#loop 100k magnify | |
scale = 8.0 | |
#scale = 0.080#100k | |
#scale = 0.01#1million | |
#scale = 0.001#10million | |
#scale = 0.9#650-700k | |
filename = "allSameSize.eps" | |
canvas = turtle.Screen() | |
canvas.tracer(0, 0) | |
#canvas.setup(400,200) | |
#canvas.setup(width=500, height=400, startx=None, starty=None) | |
#canvas.setup(width=2000, height=700, startx=None, starty=None)prime same size | |
canvas.setup(width=500, height=2500, startx=None, starty=None) | |
def draw_background(a_turtle): | |
""" Draw a background rectangle. """ | |
ts = a_turtle.getscreen() | |
canvas = ts.getcanvas() | |
height = ts.getcanvas()._canvas.winfo_height() | |
width = ts.getcanvas()._canvas.winfo_width() | |
turtleheading = bob.heading() | |
turtlespeed = bob.speed() | |
penposn = bob.position() | |
penstate = bob.pen() | |
bob.penup() | |
bob.speed(0) # fastest | |
bob.goto(-width/2-2, -height/2+3) | |
bob.fillcolor(turtle.Screen().bgcolor()) | |
bob.begin_fill() | |
bob.setheading(0) | |
bob.forward(width) | |
bob.setheading(90) | |
bob.forward(height) | |
bob.setheading(180) | |
bob.forward(width) | |
bob.setheading(270) | |
bob.forward(height) | |
bob.end_fill() | |
bob.penup() | |
bob.setposition(*penposn) | |
bob.pen(penstate) | |
bob.setheading(turtleheading) | |
bob.speed(turtlespeed) | |
def getPrimeDigits(): | |
#0.12345678910111213… | |
# current = 152000; | |
current = 1; | |
newP =next_prime(current) | |
# print(newP) | |
while True: | |
newP =next_prime(current) | |
#print(newP) | |
current_str = str(newP); | |
# currentPrime =current_str | |
for digit in current_str: | |
yield int(digit) | |
current = newP + 1 | |
def getDigits(): | |
#0.12345678910111213… | |
current = 1; | |
while True: | |
current_str = str(current); | |
for digit in current_str: | |
yield int(digit) | |
current = current + 1 | |
def getdoubleDigits(): | |
#n·ln(n)+2 | |
#n* (np.log(n)+2) | |
current = 1; | |
while True: | |
current_str = str(current); | |
for digit in current_str: | |
yield int(digit) | |
current = current + 1 | |
# current = current*(math.floor(math.log(current)+2))#math.pow(x, y) | |
# cur=math.floor(math.log(current)) | |
# current = current*(math.floor(math.pow(cur,2))) | |
#math.pow(x, y) | |
#gen = getDigits() | |
#gen = getDoubleDigits | |
#gen = getPrimeDigits() | |
gen = getDigits() | |
#ts = turtle.Turtle() | |
#ts = turtle.getscreen() | |
s = turtle.Screen() | |
s.tracer(0, 0) | |
s.bgcolor("black") | |
bob = turtle.Turtle() | |
#draw_background(bob) | |
ts = bob.getscreen() | |
canvas = ts.getcanvas() | |
turtle.hideturtle() | |
turtle.speed(0) | |
turtle.tracer(0,0) | |
#canvas.bgcolor("orange") | |
#bob = turtle.Turtle() | |
draw_background(bob) | |
#ts.setup(width=1000, height=1000, startx=None, starty=None) | |
turtle.penup() | |
#turtle.setpos(-450,350)100k | |
#turtle.setpos(-100,100)#loop 100k probe | |
#turtle.setpos(-100,100)#1k | |
#turtle.setpos(-570,300)#all together prime | |
turtle.setpos(0,400)#all together odd | |
#turtle.setpos(-400,400) | |
#turtle.forward(-450) | |
#turtle.left(90) | |
#turtle.forward(100) | |
turtle.pendown() | |
turtle.pensize(1) | |
#turtle.pensize(2) | |
#i=50000 #676000 | |
for i in range(numDigits): | |
#while i <63000: | |
digit = next(gen) | |
if i % changeColorInc == 0: | |
turtle.pencolor(next(colors)) | |
# print("i is "+str(i)) | |
angle = digit * 36 | |
turtle.right(angle) | |
#print(angle) | |
#prime | |
if i>100: | |
scale = 1.0#1.8 #1k | |
if i>1000: | |
scale = 0.1#.20 #1k | |
if i>10000: | |
scale = 0.01#0.02#10k | |
if i>100000: | |
scale = 0.001#0.0020#100k | |
if i>1000000: | |
scale = 0.0001#0.002#1million | |
#odd and even numbers | |
# if i>100: | |
# scale = 1.0 #1k | |
# if i>1000: | |
# scale = .10 #1k | |
# if i>10000: | |
# scale = 0.01#10k | |
# if i>100000: | |
# scale = 0.0010#100k | |
# if i>1000000: | |
# scale = 0.001#1million | |
if i == 0: | |
turtle.pencolor("#a6cee3") | |
if i == 100: #and i > 100000: | |
turtle.pencolor("#1f78b4") | |
if i == 1000: # and i > 200000: | |
turtle.pencolor("#b2df8a") | |
if i == 10000: # and i > 300000: | |
turtle.pencolor("#33a02c") | |
if i == 100000: # and i > 400000: | |
turtle.pencolor("#fb9a99") | |
if i == 200000: # and i > 500000: | |
turtle.pencolor("#e31a1c") | |
if i == 300000: # and i > 600000: | |
turtle.pencolor("#fdbf6f") | |
if i == 500000: # and i > 700000: | |
turtle.pencolor("#ff7f00") | |
if i == 700000: # and i > 800000: | |
turtle.pencolor("#cab2d6") | |
if i == 900000: # and i > 900000: | |
turtle.pencolor("#551A8B") | |
turtle.forward(1 * scale) | |
turtle.left(angle) | |
# i=i+1 | |
turtle.update() | |
ts.getcanvas().postscript(file=filename) | |
print(ts.bgcolor()) | |
turtle.bye() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment