stitch_graph.html
001| """Modular Stitch Graph.
001|
001| https://apieceofthepi.substack.com/p/modular-curve-stitching
001|
001| """
002|
003| from sys import argv
004| from os import path
005| import math
006| from PIL import ImageFont
007| from goodies import divisions
008| from canvas import Canvas
009|
010| BasePath = r'C:\CJS\prj\Python\app\maths'
011|
012| ImageDims = (800, 800)
013| ImageWindow = ((-1.15, +1.15), (-1.15, +1.15))
014|
015| ColorCircle = (0xdf, 0xff, 0xef)
016| ColorMarks = (0x5f, 0x3f, 0x1f)
017| ColorLines = (0x5f, 0x4f, 0x3f)
018|
019| Examples = [
020| (12,2), (30,2), (100,2), (200,2),
021| (200,3), (200,4), (200,5), (200,7), (200,10), (200,12),
022|
023| (200,21), (50,25), (100,34), (100,51),
024| (90,31), (400,115), (100,49), (206,21),
025|
026| (300, 31),
027| ]
028|
029|
030| def main (*args):
031| M = int(args[0]) if 0 < len(args) else 189
032| N = int(args[1]) if 1 < len(args) else 360
033|
034| fname = args[2] if 2 < len(args) else 'stitch-graph.png'
035| fpath = args[3] if 3 < len(args) else ''
036|
037| filename = path.join(fpath,fname) if fpath else fname
038|
039|
040| pts = [(math.cos(a),math.sin(a)) for a in divisions(0.0, math.tau, N)]
041|
042|
043| im = Canvas(ImageDims, ImageWindow)
044|
045|
046| fnt = ImageFont.truetype('segoeuil.ttf', size=48)
047|
048|
049| p = (ImageWindow[0][0]+0.025, ImageWindow[1][1]+0.025)
050| im.text(p, f'M={M}', font=fnt, c=(0,0,0), bg=(255,254,250))
051|
052|
053| p = (ImageWindow[0][0]+0.025, ImageWindow[1][0]+0.2)
054| im.text(p, f'N={N}', font=fnt, c=(0,0,0), bg=(255,254,250))
055|
056|
057| im.circle((0,0), 1.0, fg=ColorCircle, bg=None, lw=2)
058|
059|
060| for ix,pt in enumerate(pts):
061| ix2 = (ix*M) % N
062| pt2 = pts[ix2]
063| im.line(pt, pt2, c=ColorLines, lw=1)
064| print(f'[{ix:3d}] ({pt[0]},{pt[1]}) --> ({pt2[0]},{pt2[1]})')
065| print()
066|
067|
068| for ix,pt in enumerate(pts):
069| im.point13(pt, c=ColorMarks)
070|
071|
072| im.save(filename)
073| print(f'wrote: {filename}')
074| print()
075|
076| return pts
077|
078|
079| def string_art (N=50, M=1.5, fname='line-art.png'):
080| '''Generate that old-fashioned yarn art.'''
081| filename = path.join(BasePath, fname)
082|
083|
084| im = Canvas((int(800*M),800), ((-0.05,+1.05*M), (-0.05,+1.05)))
085|
086|
087| im.line((0,0), (M, 0), c=ColorCircle, lw=3)
088| im.line((0,0), (0, 1), c=ColorCircle, lw=3)
089|
090|
091| for ix in divisions(0.0, 1.0, N):
092| ptx = (M*ix, 0)
093| pty = (0, 1.0-ix)
094| im.point13(ptx, c=ColorMarks)
095| im.point13(pty, c=ColorMarks)
096| im.line(ptx, pty, c=ColorLines, lw=1)
097|
098|
099| im.save(filename)
100| print(f'wrote: {filename}')
101| print()
102| return ()
103|
104|
105| def make_all_examples ():
106| '''Generate images for each M,N pair in Examples.'''
107|
108| for ix,t in enumerate(Examples):
109| n,m = t
110| fname = f'stitch-graph-{ix:02d}.png'
111| print(f'{m}/{n} {fname}')
112| print()
113| main(m, n, fname, BasePath)
114| print()
115| print()
116| return ()
117|
118|
119| def make_one_example (m=189, n=360, fname='stitch-graph-eg.png'):
120| '''Generate a stitch-graph image.'''
121|
122| pts = main(m, n, fname, BasePath)
123| print()
124| return ()
125|
126|
127| if __name__ == '__main__':
128| print(argv[0])
129| print()
130| rv = main(*argv[1:])
131| print()
132|
133| '''eof'''
134|