From 002160ae995098583187c431d4f989a416b1bf31 Mon Sep 17 00:00:00 2001 From: Raphael Roberts Date: Fri, 2 Nov 2018 01:22:46 -0500 Subject: [PATCH] added files --- .gitignore | 96 ++++++++++++++++++++++++++++++++++++++++++++++++ cython/serp.pyx | 56 ++++++++++++++++++++++++++++ cython/setup.bat | 1 + cython/setup.py | 7 ++++ cython/test.bat | 2 + serp.py | 75 +++++++++++++++++++++++++++++++++++++ setup.bat | 1 + 7 files changed, 238 insertions(+) create mode 100644 .gitignore create mode 100644 cython/serp.pyx create mode 100644 cython/setup.bat create mode 100644 cython/setup.py create mode 100644 cython/test.bat create mode 100644 serp.py create mode 100644 setup.bat diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..6a18ad4 --- /dev/null +++ b/.gitignore @@ -0,0 +1,96 @@ +# ---> Python +# Byte-compiled / optimized / DLL files +__pycache__/ +*.py[cod] +*$py.class + +# C extensions +*.so + +# Distribution / packaging +.Python +env/ +build/ +develop-eggs/ +dist/ +downloads/ +eggs/ +.eggs/ +lib/ +lib64/ +parts/ +sdist/ +var/ +wheels/ +*.egg-info/ +.installed.cfg +*.egg + +# PyInstaller +# Usually these files are written by a python script from a template +# before PyInstaller builds the exe, so as to inject date/other infos into it. +*.manifest +*.spec + +# Installer logs +pip-log.txt +pip-delete-this-directory.txt + +# Unit test / coverage reports +htmlcov/ +.tox/ +.coverage +.coverage.* +.cache +nosetests.xml +coverage.xml +*,cover +.hypothesis/ + +# Translations +*.mo +*.pot + +# Django stuff: +*.log +local_settings.py + +# Flask stuff: +instance/ +.webassets-cache + +# Scrapy stuff: +.scrapy + +# Sphinx documentation +docs/_build/ + +# PyBuilder +target/ + +# Jupyter Notebook +.ipynb_checkpoints + +# pyenv +.python-version + +# celery beat schedule file +celerybeat-schedule + +# SageMath parsed files +*.sage.py + +# dotenv +.env + +# virtualenv +.venv +venv/ +ENV/ + +# Spyder project settings +.spyderproject + +# Rope project settings +.ropeproject + diff --git a/cython/serp.pyx b/cython/serp.pyx new file mode 100644 index 0000000..0c550e7 --- /dev/null +++ b/cython/serp.pyx @@ -0,0 +1,56 @@ +from cpython cimport array +cdef list serp(int n): + cdef list ret = [] + cdef int ind + cdef int i + cdef int v + cdef array.array t = array.array('b',(1,)) + + for ind in range(1,n+1): + ret.append(t) + l = ret[-1] + t = array.array('b') + for i in range(-1,ind): + if i == -1 or i==ind-1: + v = 1 + else: + v = l[i]^l[i+1] + t.append(v) + return ret + +from PIL import Image +class SubPix: + def __init__(self,mode,size,color = 0,s=2): + self.s = s + try: + self.image = Image.new(mode,tuple(map(lambda x: int(x*s),size)),color) + except: + print(tuple(map(lambda x: x*s,size))) + def set_subpix(self,cord,v): + s=self.s + x,y = map(lambda c: int(c*s),cord) + args = tuple(map(lambda n: ((n%s+x,n//s+y),v),range(s**2))) + for i in args: + self.image.putpixel(*i) +cpdef make_triangle1(n,b_w = False,outdir = r'x:\users\Ralphie\Documents'): + n = 2**n + if b_w: + p = (255,255,255) + np = (0,0,0) + else: + np = (255,255,255) + p = (0,0,0) + s = serp(n) + c = 0 + off = (n-1)/2 + triangle = SubPix('RGB',(n,n),np) + for row in s: + for x in range(len(row)): + xy = (x+off,c) + if row[x]: + v = p + else: + v = np + off -= .5 + c += 1 + triangle.image.save(r'{}\serp.png'.format(outdir)) diff --git a/cython/setup.bat b/cython/setup.bat new file mode 100644 index 0000000..b12f173 --- /dev/null +++ b/cython/setup.bat @@ -0,0 +1 @@ +python3 setup.py build_ext \ No newline at end of file diff --git a/cython/setup.py b/cython/setup.py new file mode 100644 index 0000000..1df877d --- /dev/null +++ b/cython/setup.py @@ -0,0 +1,7 @@ +from distutils.core import setup +from Cython.Build import cythonize + +setup( + name = 'Serpinsky', + ext_modules = cythonize("serp.pyx"), +) diff --git a/cython/test.bat b/cython/test.bat new file mode 100644 index 0000000..7f83d61 --- /dev/null +++ b/cython/test.bat @@ -0,0 +1,2 @@ +python3 -m cython -a serp.pyx +serp.html diff --git a/serp.py b/serp.py new file mode 100644 index 0000000..83fb8c6 --- /dev/null +++ b/serp.py @@ -0,0 +1,75 @@ +from math import log +from PIL import Image +from serp import serp +class SubPix: + def __init__(self,mode,size,color = 0,s=2): + self.s = s + try: + + self.image = Image.new(mode,tuple(map(lambda x: int(x*s),size)),color) + except: + print(tuple(map(lambda x: x*s,size))) + def set_subpix(self,cord,v): + s=self.s + x,y = map(lambda c: int(c*s),cord) + args = tuple(map(lambda n: ((n%s+x,n//s+y),v),range(s**2))) + for i in args: + self.image.putpixel(*i) + +def make_triangle(n,b_w = False,outdir = r'x:\Users\Ralphie\Documents'): + n = 2**n + if b_w: + p = (255,255,255) + np = (0,0,0) + else: + np = (255,255,255) + p = (0,0,0) + + + s = serp(n) + c = 0 + off = n-1 + + triangle = SubPix('RGB',(2*n-1,n),np) + for row in s: + for x in range(len(row)): + xy = (c,2*(x)+off) + if row[x]: + v = p + else: + v = np + triangle.set_subpix(xy, v) + off -= 1 + c += 1 + + triangle.image.save(r'{}\serp.png'.format(outdir)) +##make_triangle(11) +def make_triangle1(n,b_w = False,outdir = r'x:\users\Ralphie\Documents'): + n = 2**n + if b_w: + p = (255,255,255) + np = (0,0,0) + else: + np = (255,255,255) + p = (0,0,0) + s = serp(n) + c = 0 + off = (n-1)/2 + triangle = SubPix('RGB',(n,n),np) + for row in s: + for x in range(len(row)): + xy = (x+off,c) + if row[x]: + v = p + else: + v = np + try: + + triangle.set_subpix(xy,v) + except: + print(xy,triangle.image.size) + off -= .5 + c += 1 + + triangle.image.save(r'{}\serp.png'.format(outdir)) +make_triangle1(6) diff --git a/setup.bat b/setup.bat new file mode 100644 index 0000000..032f00b --- /dev/null +++ b/setup.bat @@ -0,0 +1 @@ +python3 -m cython -a serp.py \ No newline at end of file