Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- WINDOW::
- ////////////////////////
- from Tkinter import *
- from PIL import ImageTk, Image, ImageDraw, ImageFont
- import TheEndClass as tec
- import os
- class EndWindow:
- def __init__(self):
- self.record = tec.ab(5,31)
- self.squareWidth = 4
- self.buildWindow()
- self.redrawCanvas()
- def changeRecord(self,newRec):
- self.record = newRec
- self.redrawCanvas()
- def zoomIn(self):
- self.squareWidth += 1
- self.redrawCanvas()
- def zoomOut(self):
- self.squareWidth -= 1
- if(self.squareWidth < 1):
- self.squareWidth = 1
- self.redrawCanvas()
- def updateAB(self):
- aVal = int(self.aEntry.get())
- bVal = int(self.bEntry.get())
- self.record = tec.ab(aVal,bVal)
- self.redrawCanvas()
- def createRecord(self):
- string = self.createVar.get()
- strings, creates = tec.Record.createStuff()
- i = strings.index(string)
- self.record = creates[i](int(self.oneEntry.get()), int(self.twoEntry.get()), int(self.threeEntry.get()))
- self.redrawCanvas()
- def redrawCanvas(self):
- #leftSide
- IMG = tec.generateSquare(self.record, squareWidth = self.squareWidth)
- self.canvas.delete(ALL)
- tkim = ImageTk.PhotoImage(IMG)
- self.imagesprite = self.canvas.create_image(200,200,image=tkim)
- self.canvas.pack(side=BOTTOM)
- #rightside
- self.updateRightLabels()
- self.root.mainloop()
- def updateRightLabels(self):
- frm = self.bottomRightFrame
- for widget in frm.winfo_children():
- widget.destroy()
- for i in range(len(tec.Record.strs())):
- newframe = Frame(frm)
- newframe.pack(side=LEFT)
- label = Label(newframe, text=tec.Record.strs()[i])
- value = Label(newframe, text=str(self.record.vals()[i]))
- label.pack(side=TOP)
- value.pack(side=BOTTOM)
- def upE(self,event):
- self.UPE()
- def UPE(self):
- e = self.record[0]
- n = self.record[1]
- x = self.record[3]
- newE = e + 2*n
- self.record = tec.enx(newE, n, x)
- self.redrawCanvas()
- def downE(self,event):
- self.DOWNE()
- def DOWNE(self):
- e = self.record[0]
- n = self.record[1]
- x = self.record[3]
- newE = e - 2*n
- self.record = tec.enx(newE, n, x)
- self.redrawCanvas()
- def buildWindow(self):
- self.root = Tk()
- root = self.root
- root.bind('<Left>', self.downE)
- root.bind('<Right>', self.upE)
- self.leftFrame = Frame(root)
- self.rightFrame = Frame(root)
- self.leftFrame.pack(side=LEFT)
- self.rightFrame.pack(side=RIGHT)
- leftFrame = self.leftFrame
- rightFrame = self.rightFrame
- #left side
- self.topToolFrame = Frame(leftFrame)
- topToolFrame = self.topToolFrame
- self.ZObutton = Button(topToolFrame, text="-", command=self.zoomOut)
- self.ZIbutton = Button(topToolFrame, text="+", command=self.zoomIn)
- self.ZObutton.pack(side=LEFT)
- self.ZIbutton.pack(side=RIGHT)
- self.topToolFrame.pack(side=TOP)
- self.canvas = Canvas(leftFrame, width=400, height=400)
- IMG = tec.generateSquare(self.record, squareWidth = self.squareWidth)
- tkim = ImageTk.PhotoImage(IMG)
- self.imagesprite = self.canvas.create_image(200,200,image=tkim)
- self.canvas.pack(side=BOTTOM)
- #right side
- #top right
- self.topRightFrame = Frame(self.rightFrame, padx = 10, pady = 10)
- self.topRightFrame.pack(side=TOP)
- self.ABLabel = Label(self.topRightFrame, text="AB:")
- self.ABLabel.pack(side=LEFT)
- self.aEntry = Entry(self.topRightFrame, width=5)
- self.aEntry.pack(side=LEFT)
- self.bEntry = Entry(self.topRightFrame, width=5)
- self.bEntry.pack(side=LEFT)
- self.ABButton = Button(self.topRightFrame, text="AB", command = self.updateAB)
- self.ABButton.pack(side=RIGHT)
- #mid right
- self.midRightFrame = Frame(self.rightFrame)
- self.midRightFrame.pack()
- listOptions, funcList = tec.Record.createStuff()
- self.createVar = StringVar(self.midRightFrame)
- self.popMenu = OptionMenu(self.midRightFrame, self.createVar, *listOptions)
- self.popMenu.config(width = 8)
- self.popMenu.pack(side=LEFT)
- self.oneEntry = Entry(self.midRightFrame, width=5)
- self.oneEntry.pack(side=LEFT)
- self.twoEntry = Entry(self.midRightFrame, width=5)
- self.twoEntry.pack(side=LEFT)
- self.threeEntry = Entry(self.midRightFrame, width=5)
- self.threeEntry.pack(side=LEFT)
- self.CreateButton = Button(self.midRightFrame, text="Create", command = self.createRecord)
- self.CreateButton.pack(side=RIGHT)
- #bottom right
- self.bottomRightFrame = Frame(self.rightFrame)
- self.bottomRightFrame.pack(side = BOTTOM)
- self.updateRightLabels()
- #last row
- self.bottomBottomRight = Frame(self.rightFrame)
- self.bottomBottomRight.pack(side=BOTTOM)
- self.e2nButtonU = Button(self.bottomBottomRight, text="e+2n", command=self.UPE)
- self.e2nButtonU.pack(side=LEFT)
- self.e2nButtonD = Button(self.bottomBottomRight, text="e-2n", command=self.DOWNE)
- self.e2nButtonD.pack(side=LEFT)
- root.mainloop()
- j = EndWindow()
- ///////////////
- TheEndClass.py:
- /////////////
- import math
- import NumberTheory as nt
- from PIL import ImageFont
- from PIL import Image
- from PIL import ImageDraw
- primes = nt.primes
- coprimes = nt.coprimes
- class SquareThing:
- @staticmethod
- def BR(a, b):
- if(a%2 != 1 or b%2 != 1):
- print("Error")
- return
- return SquareThing(b,a)
- @staticmethod
- def BL(a, b):
- if(a%2 != 1):
- print("Error")
- return
- if(b%2 != 1):
- print("Error")
- return
- big = (a+b)//2
- little = (b-a)//2
- if(little%2==1):
- print("Error")
- return
- corner = little//2
- center = big - little
- return SquareThing(center,corner)
- @staticmethod
- def LR(a, b):
- center = 2*b + 1
- return SquareThing(center, a)
- @staticmethod
- def MIN(a,b): #2*big rec + center square
- center = a
- corner = (b-a)//2
- return SquareThing(center,corner)
- @staticmethod
- def LM(a,b): #Big rec + center square
- center = a
- corner = b-a
- return SquareThing(center,corner)
- @staticmethod
- def EM(a,b):
- center = 2*a+1
- corner = b - (a-1)//2
- return SquareThing(center,corner)
- @staticmethod
- def E(a,b):
- center = a
- corner = b-a
- return SquareThing(center,corner)
- @staticmethod
- def waysToBuild():
- ways = [SquareThing.BL, SquareThing.BR, SquareThing.LM,
- SquareThing.MIN, SquareThing.LR, SquareThing.EM,
- SquareThing.E]
- return ways
- @staticmethod
- def printVals(recs):
- maxLen = 6
- for rec in recs:
- if(rec is None):
- continue
- for val in rec.vals():
- if(len(str(val)) > maxLen):
- maxLen = len(str(val))
- output = ""
- tops = SquareThing.strs()
- firstRow = ""
- for part in tops:
- spaces = maxLen - len(part)
- spa = " " * spaces
- firstRow += "|" + spa + str(part)
- output += firstRow + "|\n"
- for rec in recs:
- row = ""
- if(rec is None):
- continue
- for val in rec.vals():
- spaces = maxLen - len(str(val))
- spa = " " * spaces
- row += "|" + spa + str(val)
- output += row + "|\n"
- print(output)
- def __init__(self, center, corner):
- bigSquare = center + 2*corner
- self.bs = bigSquare
- self.center = center
- self.corner = corner
- triangleSide = (center - 1)/2
- if(triangleSide % 1 == 0):
- triangleSide = int(triangleSide)
- self.tri = triangleSide
- self.tradF = [bigSquare - 2*corner, bigSquare + 2*corner]
- self.trad = bigSquare**2 - 4*corner**2
- self.brF = [corner, center]
- self.br = corner*center
- self.lrF = [corner, triangleSide]
- self.lr = corner*triangleSide
- self.Em = T(triangleSide) + self.lr
- self.lm = self.br + self.center**2
- self.lmF = [self.center, self.corner + self.center]
- self.E = T(center) + self.br
- self.min = center*bigSquare
- self.minF = [center, bigSquare]
- def vals(self):
- array = [self.trad, self.br, self.lm, self.min, self.lr, self.Em, self.E,self.corner,self.tri]
- return array
- @staticmethod
- def strs():
- array = ["BL", "BR", "LM", "MIN","LR","EM","E","corner","tri"]
- return array
- def printSquares(vals):
- SquareThing.printVals(vals)
- class Record:
- @staticmethod
- def ABD(A,B,D):
- e,n,d,x,a,b = ABD(A,B,D)
- return Record(e,n,d,x,a,b)
- @staticmethod
- def ENA(E,N,A):
- e,n,d,x,a,b = ENA(E,N,A)
- return Record(e,n,d,x,a,b)
- @staticmethod
- def AB(A,B):
- e,n,d,x,a,b = AB(A,B)
- return Record(e,n,d,x,a,b)
- @staticmethod
- def ABX(A,B,X):
- d = A + X
- return Record.ABD(A,B,d)
- @staticmethod
- def DEA(D,E,A):
- e,n,d,x,a,b = DEA(D,E,A)
- return Record(e,n,d,x,a,b)
- @staticmethod
- def ENX(E,N,X):
- e,n,d,x,a,b = ENX(E,N,X)
- return Record(e,n,d,x,a,b)
- @staticmethod
- def BSLS(bs,ls):
- c = bs*bs - ls*ls
- d = int(math.sqrt(c))
- e = c - d * d
- n = bs - d
- x = ls - n
- a = d - x
- b = a + 2*n + 2*x
- return Record(e,n,d,x,a,b)
- @staticmethod
- def tMethod(t,Ac, Bc, Xc, Dc):
- a = ABDguy(t,Ac)
- b = ABDguy(t,Bc)
- d = ABDguy(t,Dc)
- x = Xguy(t,Xc)
- e = a*b - d*d
- n = (b - a - 2*x)//2
- return Record(e,n,d,x,a,b)
- @staticmethod
- def printRecs(recs):
- maxLen = 5
- for i in Record.strs():
- if(len(i)>maxLen):
- maxLen = len(i)
- for i in recs:
- for j in i.vals():
- if(len(str(j)) > maxLen):
- maxLen = len(str(j))
- start = ""
- for i in Record.strs():
- start += " "*(maxLen-len(i)) + i + "|"
- start += "\n"
- for rec in recs:
- for val in rec.vals():
- start += " "*(maxLen-len(str(val))) + str(val) + "|"
- start += "\n"
- print(start)
- @staticmethod
- def createStuff():
- strings = ["ABD","ABX","ENA","DEA","ENX"]
- funcs = [Record.ABD, Record.ABX, Record.ENA, Record.DEA, Record.ENX]
- return strings,funcs
- @staticmethod
- def strs():
- return ['e','n','d','x','a','b','c','bs','ls','f','nn','2d1n1g','f-1','n-1']
- def vals(self):
- return [self.e, self.n, self.d, self.x, self.a, self.b, self.c,
- self.bs, self.ls, self.f, (self.nn,self.nn%8),
- (self.dd1n1,self.dd1n1%8), (self.f1,self.f1%8),
- (self.n1,self.n1%8)]
- def __init__(self,E,N,D,X,A,B):
- self.e = E
- self.n = N
- self.d = D
- self.x = X
- self.a = A
- self.b = B
- self.c = A*B
- self.bs = D+N
- self.ls = X+N
- self.f = 2*D + 1 - E
- self.nn = N*N
- self.dd1n1 = (2*D-1)*(N-1)
- self.f1 = self.f - 1
- self.n1 = self.n - 1
- def __str__(self):
- return str((self.e, self.n, self.d, self.x, self.a, self.b))
- def __getitem__(self,indices):
- return self.vals()[indices]
- def F(self):
- self.d = self.d+1
- self.e = self.c - self.d**2
- self.x = self.d - self.a
- self.n = (self.b + self.a)//2 - self.d
- def FI(self):
- self = Record.ABD(self.a, self.b, self.d-1)
- def bigRec(self):
- return self.b**2 + self.b * self.a
- def shiftE(self,W):
- e,n,d,x,a,b = self.e, self.n,self.d,self.x,self.a,self.b
- return Record(e+2*W*n, n, d+W, x, a+W, b+W)
- def swapDN(self):
- return Record.ABD(self.a, self.b, self.n)
- def zeroD(self):
- return Record.ABD(self.a, self.b, 0)
- def shiftN(self,amt):
- e,n,d,x,a,b = self.e, self.n, self.d, self.x, self.a, self.b
- return Record(e + 2*a*amt, n + amt, d, x, a, b+2*amt)
- def zeroN2(self):
- return self.shiftN(-self.n)
- def zeroN(self):
- return Record.ABD(self.a, self.b, self.bs)
- def oneN(self):
- return Record.ABD(self.a, self.b, 1).swapDN()
- def shiftD(self,n):
- return Record.ABD(self.a, self.b, self.d + n)
- def upAB(self):
- return Record.AB(self.a + 1, self.b + 1)
- def downAB(self):
- return Record.AB(self.a - 1, self.b - 1)
- def aubd(self):
- return Record.AB(self.a + 1, self.b - 1)
- def abdu(self):
- return Record.AB(self.a - 1, self.b + 1)
- def shiftAB(self, n):
- return Record.AB(self.a + n, self.b + n)
- def factors(n): return nt.factors(n)
- def gcd(a,b): return nt.gcd(a,b)
- def lcm(a,b): return int(a*b)//int(gcd(a,b))
- def sqrt(n): return nt.intSqrt(n)
- def pf(n): return nt.pf(n)
- def legendre(a,p): return int(nt.legendre(a,p))
- def ab(a,b): return Record.AB(a,b)
- def abd(a,b,d): return Record.ABD(a,b,d)
- def ABD(a,b,d): return nt.ABD(a,b,d)
- def dea(d,e,a): return Record.DEA(d,e,a)
- def DEA(d,e,a): return nt.DEA(d,e,a)
- def makeTree(n): return nt.makeTree(n)
- def genFracs(cf,iterations): return nt.genFracs(cf,iterations)
- def cfroot(n): return nt.CFRoot(n)
- def pell(n): return nt.pell(n)
- def L(n): return 2*n-1
- def T(n): return (n*(n+1))//2
- def printRecs(n): Record.printRecs(n)
- def gauss(a,p):
- P = int((p-1)/2)
- num = a
- j = 1
- for i in range(P):
- res = num % p
- if(res > P):
- j *= -1
- num += a
- return j
- def AB(a,b):
- c = a*b
- d = sqrt(c)
- e = c - d*d
- x = d - a
- if(a==0):
- n = (b - a - 2*x)//2
- else:
- n = (x*x + e) // (2*a)
- if(2*n*a != x*x + e):
- print("Error AB", a,b)
- if((d+n)**2 - (x+n)**2 != c):
- print("Error AB", a,b)
- return (e,n,d,x,a,b)
- def ena(e,n,a): return Record.ENA(e,n,a)
- def ENA(e,n,a):
- x = math.sqrt(2*n*a - e)
- if(x%1 != 0):
- print("error ENA",e,n,a)
- x = int(x)
- d = x + a
- return (e,n,d,x,a, a + 2*x + 2*n)
- def enx(e,n,x): return Record.ENX(e,n,x)
- def ENX(e,n,x):
- a = (x*x + e)/(2*n)
- if(a%1!=0):
- print("enx err", e,n,x)
- a = int(a)
- d = x + a
- b = a + 2*x + 2*n
- return (e,n,d,x,a,b)
- def abx(a,b,x): return Record.ABX(a,b,x)
- def ABDguy(t,const,start):
- return T(t)*4 + t*const + start
- def Xguy(t, const):
- return 2*t + const
- def DE(c):
- D = sqrt(c)
- return (D,c-D*D)
- records = []
- def generateRecords(coprimes = True):
- if(coprimes):
- for i in primes:
- for j in primes:
- if(i<j):
- records.append(ab(i,j))
- return records
- for i in range(200):
- for j in range(i):
- records.append(ab(i-j,i+j))
- return records
- generateRecords()
- def quad(A,B):
- rec = AB(A,B)
- n = rec[1]
- e = rec[0]
- #b^2-4ac = d, b^2 - d = 4ac, c = (b^2-d)/2a
- print(rec)
- for num in rec[:5]:
- print((e*e-num)/(4*n))
- def getRecordType(recs, char, goal = "R"):
- if(char == "e"): ind = 0
- elif(char == "n"): ind = 1
- elif(char == "d"): ind = 2
- elif(char == "x"): ind = 3
- elif(char == "a"): ind = 4
- elif(char == "b"): ind = 5
- elif(char == "c"): ind = 6
- elif(char == "d+n"): ind = 7
- elif(char == "x+n"): ind = 8
- elif(char == "f"): ind = 9
- elif(char == "r"): ind = 10
- elif(char == "br"): ind = 11
- elif(char == "nn"): ind = 12
- elif(char == "2d1n1"): ind = 13
- def compare(rec):
- return rec.vals()[ind]
- if(goal == "R"):
- printList = sorted(recs, key=compare)
- else:
- printList = []
- for rec in recs:
- if(rec.vals()[ind] == goal):
- printList.append(rec)
- return printList
- def fact(n):
- for i in range(1,n):
- if(2*T(i) + (i*i-2)*i == n):
- return i
- return -1
- def asthing(c):
- a = pf(c)[0]
- b = c//a
- e,n,d,x,a,b = AB(a,b)
- return a, 2*(x+n)-1
- def nums(a,b):
- return 2*T(a) + a*b, 2*T(b) + a*b
- def legTest(a):
- base = 4*abs(a)
- dic = {}
- for p in primes:
- res = legendre(a,p)
- mod = p % base
- if(mod not in dic.keys()):
- dic[mod] = []
- dic[mod].append(res)
- for p in sorted(dic.keys()):
- print(str(p) + "\t"+str( dic[p][0]))
- def factor(c):
- d= sqrt(c)
- e = c - d*d
- def test(c):
- rec = ab(1,c)
- start = rec[9]
- lim = 0
- while(ab(1,c + start**2)[0] != 0):
- if(start % 2 == 1):
- start += 1
- else:
- start >> 1
- lim+= 1
- if(lim > 50):
- return -1
- def minRec(f):
- factors = pf(f)
- if(len(factors) == 1):
- return ab(1,f)
- else:
- small = min(factors)
- return ab(small, f//small)
- def test(a,b):
- goal = ab(a,b)[8]
- e,n,d,x,a,b,c,dn,xn,f,r,br,ggg = ab(1,a*b)
- options = [sqrt(f), gcd(xn,f), gcd(f,d), f*2-1, n%f, e%f, e>>1, f>>1, f<<1,
- f<<1 + 1, e<<1, e<<1+1]
- stri = []
- for i in range(len(options)):
- if(options[i] == goal):
- stri.append(i)
- return stri
- def reduce(c):
- return rebuild(makeTree(c))
- def rebuild(tree):
- if(type(tree) is int):
- return tree
- if(len(tree) == 1):
- return tree[0]
- return rebuild(tree[0])**2 + rebuild(tree[1])
- def isSquare(n):
- d = sqrt(n)
- return n-d*d == 0
- def checkXn(c,xn):
- return isSquare(c+xn*xn)
- def findE1(e):
- recs = []
- for i in range(e%2, 50, 2):
- recs.append(enx(e,1,i))
- printRecs(recs)
- def makeRecs():
- recDic = {}
- # Cases:
- # 0 = (x+n) odd and e odd
- # 1 = (x+n) odd and e even
- # 2 = (x+n) even and e odd
- # 3 = (x+n) even and e even
- for i in range(4):
- recDic[i] = []
- for i in primes:
- for j in primes:
- if(i<j):
- rec = ab(1,i*j)
- goal = ab(i,j)
- e,n,d,x,a,b,c,dn,xn,f,rr,br,gc = ab(1,i*j)
- if(xn%2 == 1 and e%2 == 1):
- recDic[0].append(rec)
- recDic[0].append(goal)
- if(xn%2 == 1 and e%2 == 0):
- recDic[1].append(rec)
- recDic[1].append(goal)
- if(xn%2 == 0 and e%2 == 1):
- recDic[2].append(rec)
- recDic[2].append(goal)
- if(xn%2 == 0 and e%2 == 0):
- recDic[3].append(rec)
- recDic[3].append(goal)
- return recDic
- def n1Recs(e,lim):
- part = e & 1
- recs = []
- for i in range(lim):
- recs.append(enx(e,1,i*2+part))
- return recs
- def goodGo(a,b):
- start = ab(1,a*b)
- recs = [start, ab(a,b)]
- dd = start[2]
- nn = start[1]
- other = n1Recs(recs[0][0], 30)
- for rec in other:
- recs.append(rec)
- printRecs(recs)
- def factor(c):
- d = sqrt(c)
- e = c-d*d
- x = d-1
- n = (x*x + e)//2
- GCD = 1
- t = 0
- eo = e%2
- while(GCD == 1 or c%GCD != 0):
- x = 2*t + e%2
- A = (x*x + e)//2
- D = A + x
- GCD = gcd((D-d)%(n-1), A%n)
- t+=1
- return d-x
- def printValues(categories, values, returnString = False):
- lens = []
- for name in categories:
- if(len(name) > 5):
- lens.append(len(name))
- else:
- lens.append(5)
- for i in range(len(categories)):
- for value in values:
- if(len(str(value[i])) > lens[i]):
- lens[i] = len(str(value[i]))
- string = ""
- for i in range(len(categories)):
- string += " "*(lens[i] - len(categories[i])) + categories[i]+ "|"
- string += "\n" + "-"*len(string) + "\n"
- for value in values:
- for i in range(len(categories)):
- string += " "*(lens[i]-len(str(value[i]))) + str(value[i]) + "|"
- string += "\n"
- if(returnString):
- return string
- print(string)
- def go(a,b):
- rec = ab(1,a*b)
- goal = ab(a,b)
- intermediate = enx(goal[0], 1, goal[3])
- printValues(Record.strs(),[rec, goal, intermediate, ena(rec[0], 1, rec[1]), ab(1,a*a*b*b)])
- samples = []
- for i in range(rec[0]%2,60,2):
- boii = enx(rec[0],1,i)
- val = boii.vals()
- val.append(boii[2]-rec[2])
- val.append(boii[4])
- val.append(gcd(boii[2]-rec[2],boii[4]))
- val.append((boii[2]-rec[2])%(rec[1]-1))
- val.append((boii[4]%rec[1]))
- samples.append(val)
- strings = Record.strs()
- strings.append("D[t]-d")
- strings.append("A[t]")
- strings.append("GCD")
- strings.append("D[t]-d%(n-1)")
- strings.append("A[t]%n")
- printValues(strings,samples)
- #//////////////////////////
- recordBook = {}
- def generateSquare(record, squareWidth = 4):
- n = record[1]
- x = record[3]
- d = record[2]
- e = record[0]
- nn = n*n
- f = 2*d + 1 - e
- 2
- xn = x+n
- width = xn
- if(xn%2==0):
- return
- vals = [n*n, (2*d-1)*(n-1), f-1, n-1]
- red = (166,52,70)
- green = (96,211,148)
- yellow = (247,240,82)
- blue = (35,116,171)
- orange = (242,129,35)
- colors = [blue, green, yellow, orange]
- divs = []
- mods = []
- for val in vals:
- divs.append(val//8)
- mods.append(val%8)
- miniSquares = [[0 for i in range(width)] for j in range(width)]
- center = xn//2
- baseColors = []
- for j in range(len(divs)):
- for i in range(divs[j]):
- baseColors.append(colors[j])
- if(j == 1):
- baseColors.append(red)
- baseColors.append(red)
- #draw triangles part
- for triangle in range(1,9):
- colorDex = 0
- if(triangle in [2,3,6,7]): #horizontal drawing
- if(triangle == 2):
- xStart = center
- yStart = center - 1
- yDir = -1
- legDir = -1
- if(triangle == 3):
- xStart = center + 1
- yStart = center - 1
- yDir = -1
- legDir = 1
- if(triangle == 6):
- xStart = center - 1
- yStart = center + 1
- yDir = 1
- legDir = -1
- if(triangle == 7):
- xStart = center
- yStart = center + 1
- yDir = 1
- legDir = 1
- for y in range(1,center+1):
- for x in range(y):
- xCoord = xStart + (legDir * x)
- yCoord = (yStart + (yDir) * (y-1))
- miniSquares[yCoord][xCoord] = baseColors[colorDex]
- colorDex += 1
- else:
- if(triangle == 1):
- xStart = center - 1
- yStart = center - 1
- xDir = -1
- legDir = -1
- if(triangle == 4):
- xStart = center + 1
- yStart = center
- xDir = 1
- legDir = -1
- if(triangle == 5):
- xStart = center - 1
- yStart = center
- xDir = -1
- legDir = 1
- if(triangle == 8):
- xStart = center + 1
- yStart = center + 1
- xDir = 1
- legDir = 1
- for x in range(1,center+1):
- for y in range(x):
- xCoord = xStart + (xDir * (x-1))
- yCoord = yStart + (legDir * y)
- miniSquares[yCoord][xCoord] = baseColors[colorDex]
- colorDex += 1
- res = makeSquare(squareWidth,width,miniSquares)
- return res
- def makeSquare(squareWidth, squares, entries):
- totalWidth = (squares-1) + squareWidth*(squares)
- img = Image.new('RGB', (totalWidth,totalWidth))
- draw = ImageDraw.Draw(img)
- for i in range(len(entries)):
- row = entries[i]
- for j in range(len(row)):
- color = row[j]
- fillSquare(j,i,squareWidth,draw, color)
- return img
- def fillSquare(x,y,squareWidth, draw, color):
- xPix = x*(squareWidth + 1)
- yPix = y*(squareWidth + 1)
- draw.rectangle( ((xPix,yPix),(xPix + squareWidth, yPix + squareWidth)), fill = color)
- def newGenerateCGif(cLim):
- i_min = -100
- i_max = 300
- j_min = -100
- j_max = 100
- width = i_max - i_min
- height = j_max - j_min
- bounds = (i_min, i_max, j_min, j_max)
- imgs = []
- for i in range(1,cLim):
- nextPic = Image.new('RGB', (width,height))
- imgs.append(nextPic)
- for img in imgs:
- for dcoords in DGrid:
- if(inBounds(dcoords,bounds)):
- img.putpixel(endToPic(dcoords,bounds),(255,255,255))
- flip = (-dcoords[0], dcoords[1])
- if(inBounds(flip,bounds)):
- img.putpixel(endToPic(flip,bounds),(100,100,255))
- for newProd in range(1,cLim):
- dVal = int(math.sqrt(newProd))
- eLine = square(dVal)
- nLine = dVal
- for i in range(width):
- cord = (eLine,i)
- if(inBounds(cord,bounds)):
- imgs[newProd-1].putpixel(endToPic(cord,bounds), (200,200,0))
- for i in range(height):
- cord = (i,nLine)
- if(inBounds(cord,bounds)):
- imgs[newProd-1].putpixel(endToPic(cord,bounds),(200,200,0))
- for factor in range(1,newProd):
- factors = [1,-1]
- if(newProd % factor == 0):
- for sign in factors:
- newRec = AB(factor*sign, sign*newProd/factor)
- dVal = newRec[d]
- newCoords = (newRec[e] + square(dVal), newRec[n] + dVal)
- if(inBounds(newCoords, bounds)):
- if(sign == 1):
- color = (0,255,0)
- else:
- color = (255,0,0)
- imgs[newProd-1].putpixel(endToPic(newCoords,bounds),color)
- font = ImageFont.truetype("Arial.ttf",14)
- for i in range(1,cLim):
- draw = ImageDraw.Draw(imgs[i-1])
- draw.text((20,height-20),"C = "+str(i),(255,255,255), font = font)
- draw = ImageDraw.Draw(imgs[i-1])
- fp = Image.new("RGB", (width,height))
- fp.save("DGrid.gif", save_all = True, append_images = imgs, loop = True)
- fp.close()
Advertisement
Add Comment
Please, Sign In to add comment