-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgraphPrime.py
More file actions
83 lines (77 loc) · 2.2 KB
/
graphPrime.py
File metadata and controls
83 lines (77 loc) · 2.2 KB
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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
import getPrime
import turtle
import random
from turtle import TK
import math
def sizetostr(size,pos):
return str(int(size[0]))+"x"+str(int(size[1]))+"+"+str(int(pos[0]))+"+"+str(int(pos[1]))
randerlib="abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890#*"
def rander(a=3,b=4,confirm=False):
res=""
for i in range(a):
if i!=0:
res+="-"
cur=""
num=0
for j in range(b):
i=random.randint(0,len(randerlib)-1)
cur+=randerlib[i]
num=num^i
if confirm:
cur+=randerlib[num]
res+=cur
return res
def newwindow(title=None,size=(200,200),pos=(100,100)):
wn=TK.Tk()
wn.geometry(sizetostr(size,pos))
if title==None:
title=rander()
wn.title(title)
cv=TK.Canvas(wn,width=size[0],height=size[1])
cv.pack()
sc=turtle.TurtleScreen(cv)
return (wn,cv,sc)
primes = []
def drawPrimes(rng, delayedUpdate = True, delayStep = .01):
global primes
pos = [[1, 0], [0, -1], [-1, 0], [0, 1]]
d = 0
len = 1
cnt = 0
c = [0, 0]
wnd = newwindow(size = (math.sqrt(rng) + 16, math.sqrt(rng) + 16))
if delayedUpdate:
wnd[2].tracer(0)
t = turtle.RawTurtle(wnd[2])
t.speed(0)
t.hideturtle()
t.penup()
progress = 0
print("Paint progress: {:f} ".format(progress), end = "\r");
for i in range(2, rng + 1):
c[0] += pos[d][0]
c[1] += pos[d][1]
if i in primes:
t.goto(c[0], c[1])
t.dot(1)
cnt += 1
if cnt >= len:
cnt = 0
d = (d + 1) & 3
if d & 1 == 0:
len += 1
if progress + delayStep <= i / rng:
progress += delayStep;
print("Paint progress: {:f} ".format(progress), end = "\r");
if delayedUpdate:
wnd[2].update()
print("Paint progress: {:f}".format(progress), end = "\n");
if delayedUpdate:
wnd[2].update()
return wnd
if __name__ == "__main__":
rng = int(input("range: "))
primes = getPrime.getPrime(rng, progress_show = "by prog")
wnd = drawPrimes(rng)
input("Input anything to destroy window.")
wnd[0].destroy()