circle-360-60.html
001|
002| from sys import argv
003| from os import path
004| from math import pi,tau, sin,cos
005| from cmath import exp
006| from PIL import Image, ImageDraw, ImageFont
007|
008| BasePath = r'C:\CJS\prj\Python\blog\proj'
009| AnimPath = r'C:\CJS\prj\pov\ws\xyz\ani'
010| ImgsPath = path.join(BasePath, 'img')
011|
012|
013| dims = (6000, 6000)
014| lims = ((-1.08,+1.08), (-1.08,+1.08))
015| frame = (lims[0][1]-lims[0][0], lims[1][1]-lims[1][0])
016| scale = (dims[0]//frame[0], dims[1]//frame[1])
017| edge = (dims[0]-1, dims[1]-1)
018|
019| shrink = True
020| s_dims = (600, 600)
021|
022| border_width = 21
023|
024| eye = complex(0, 1)
025| ipi = complex(pi,0) * eye
026|
027| px = lambda x: ((x * scale[0]) + dims[0]//2)
028| py = lambda y: (dims[1]//2 - (y * scale[1]))
029|
030| euler = lambda r,a:complex(r*cos(a), r*sin(a))
031| z2t = lambda z:(px(z.real), py(z.imag))
032| ulc = lambda p,d:(p[0]-d, p[1]-d)
033| lrc = lambda p,d:(p[0]+d, p[1]+d)
034|
035|
036|
037| def draw_circle (draw, radius=1.0, parts=500, width=7, color=(127,127,255), start=0, stop=tau):
038| '''Draw an arc at a specified radius.'''
039|
040|
041| angles = [tau*(n/parts) for n in range(parts)]
042|
043|
044| points = [euler(radius,a) for a in angles if start <= a <= stop]
045|
046|
047| draw.line([z2t(z) for z in points], width=width, fill=color)
048|
049|
050| def draw_diagonal (draw, angle, width=5, color=(63,159,63), radius=1.0, half=False):
051| '''Draw a diagonal line intersecting the center.'''
052|
053|
054| pt0 = radius * exp(ipi*angle)
055|
056|
057| if half:
058|
059| pt1 = complex(0,0)
060| else:
061|
062|
063| pt1 = radius * exp(ipi*angle) * exp(ipi)
064|
065|
066| draw.line([z2t(pt0), z2t(pt1)], width=width, fill=color)
067|
068|
069| def draw_circle_60 (segments):
070| im = Image.new('RGB', dims, color=(255,255,255))
071| gi = ImageDraw.Draw(im)
072|
073| fnt0 = ImageFont.truetype('corbel.ttf' , size=520)
074| fnt1 = ImageFont.truetype('corbelb.ttf', size=640)
075|
076|
077|
078|
079|
080|
081|
082| for ang in [(x/segments) for x in range(segments)]:
083| draw_diagonal(gi, (1/2)+(ang*2), width=19, color=(0x33,0x33,0x99), half=True)
084|
085|
086| draw_circle(gi, width=69, color=(223,223,255))
087| draw_circle(gi, width=61, color=(207,207,255))
088| draw_circle(gi, width=53, color=(191,191,255))
089| draw_circle(gi, width=45, color=(159,159,255))
090| draw_circle(gi, width=37, color=(127,127,255))
091|
092|
093| for ang in [(x/60) for x in range(60)]:
094| pt = z2t(exp(ipi*ang*2))
095| gi.ellipse([ulc(pt,30),lrc(pt,30)], fill=(0,0,255), outline=(0,0,0), width=3)
096|
097|
098| txt = str(segments)
099| x,y = (px(-1.0), py(+1.05))
100| gi.text((x,y), txt, font=fnt1, fill=(0,0,0))
101|
102| txt = f'{60//segments:d} mins'
103| x,y = (px(-1.06), py(-0.90))
104| gi.text((x,y), txt, font=fnt0, fill=(0,0,0))
105|
106|
107| border_lines = [(0,0), (edge[0],0), (edge[0],edge[1]), (0,edge[1]), (0,0)]
108| shift_templt = [(+1,+1), (-1,+1), (-1,-1), (+1,-1), (+1,+1)]
109| border_color = (0, 0, 0)
110|
111| for ix in range(border_width):
112| gi.line(border_lines, fill=border_color, width=1)
113| border_lines = [(b[0]+s[0], b[1]+s[1]) for b,s in zip(border_lines,shift_templt)]
114| border_color = tuple([c+8 for c in border_color])
115|
116|
117| outname = path.join(BasePath, f'split60-{segments:02d}.png' if shrink else 'split60-{segments:02d}-big.png')
118| if shrink:
119| im = im.resize(s_dims)
120| im.save(outname)
121| print()
122| print('wrote: %s' % outname)
123|
124|
125| def all_draw_circle_60 ():
126| '''Draw all 60-division circle segmentations.'''
127| for segs in [1,2,3,4,5,6,10,15,20,30,60]:
128| draw_circle_60(segs)
129|
130|
131| def draw_circle_360 (segments):
132| '''Draw segments in a 360-division circle.'''
133| im = Image.new('RGB', dims, color=(255,255,255))
134| gi = ImageDraw.Draw(im)
135|
136| fnt0 = ImageFont.truetype('corbel.ttf' , size=520)
137| fnt1 = ImageFont.truetype('corbelb.ttf', size=640)
138|
139|
140| draw_circle(gi, radius=0.6, width=21, color=(0x99,0x33,0x99), stop=(tau*(1/segments)))
141|
142|
143| for ang in [(x/segments) for x in range(segments)]:
144| draw_diagonal(gi, ang*2, width=19, color=(0x33,0x33,0x99), half=True)
145|
146|
147| draw_circle(gi, width=69, color=(223,223,255))
148| draw_circle(gi, width=61, color=(207,207,255))
149| draw_circle(gi, width=53, color=(191,191,255))
150| draw_circle(gi, width=45, color=(159,159,255))
151| draw_circle(gi, width=37, color=(127,127,255))
152|
153|
154| for ang in [(x/segments) for x in range(segments)]:
155| pt = z2t(exp(ipi*ang*2))
156| gi.ellipse([ulc(pt,30),lrc(pt,30)], fill=(0,0,255), outline=(0,0,0), width=3)
157|
158|
159| txt = str(segments)
160| x,y = (px(-1.0), py(+1.05))
161| gi.text((x,y), txt, font=fnt1, fill=(0,0,0))
162|
163| txt = ('%.2f' % (360/segments))
164| x,y = (px(-1.0), py(-0.85))
165| gi.text((x,y), txt, font=fnt0, fill=(0,0,0))
166|
167|
168| border_lines = [(0,0), (edge[0],0), (edge[0],edge[1]), (0,edge[1]), (0,0)]
169| shift_templt = [(+1,+1), (-1,+1), (-1,-1), (+1,-1), (+1,+1)]
170| border_color = (0, 0, 0)
171|
172| for ix in range(border_width):
173| gi.line(border_lines, fill=border_color, width=1)
174| border_lines = [(b[0]+s[0], b[1]+s[1]) for b,s in zip(border_lines,shift_templt)]
175| border_color = tuple([c+8 for c in border_color])
176|
177|
178| outname = path.join(BasePath, f'split360-{segments:02d}.png' if shrink else 'split360-{segments:02d}-big.png')
179| if shrink:
180| im = im.resize(s_dims)
181| im.save(outname)
182| print('wrote: %s' % outname)
183| print()
184|
185|
186| def all_draw_circle_360 ():
187| '''Draw all 360-division circle segmentations.'''
188| for segs in [1,2,3,4,5,6,8,9,10,12,15,18,20,24,30,36,45,60,90,120,180,360]:
189| draw_circle_360(segs)
190|
191|
192| def main ():
193| segs = int(argv[1]) if 1 < len(argv) else 16
194|
195|
196|
197|
198|
199| all_draw_circle_360()
200|
201|
202| if __name__ == '__main__':
203| print(argv[0])
204| print()
205| main()
206| print()
207|
208| '''eof'''
209|