commit
002160ae99
7 changed files with 238 additions and 0 deletions
-
96.gitignore
-
56cython/serp.pyx
-
1cython/setup.bat
-
7cython/setup.py
-
2cython/test.bat
-
75serp.py
-
1setup.bat
@ -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 |
||||
|
|
||||
@ -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)) |
||||
@ -0,0 +1 @@ |
|||||
|
python3 setup.py build_ext |
||||
@ -0,0 +1,7 @@ |
|||||
|
from distutils.core import setup |
||||
|
from Cython.Build import cythonize |
||||
|
|
||||
|
setup( |
||||
|
name = 'Serpinsky', |
||||
|
ext_modules = cythonize("serp.pyx"), |
||||
|
) |
||||
@ -0,0 +1,2 @@ |
|||||
|
python3 -m cython -a serp.pyx |
||||
|
serp.html |
||||
@ -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) |
||||
@ -0,0 +1 @@ |
|||||
|
python3 -m cython -a serp.py |
||||
Write
Preview
Loading…
Cancel
Save
Reference in new issue