the-circle.html
001|
002| from sys import argv
003| from os import path
004| from math import pi, sin,cos
005| from cmath import exp
006| from PIL import Image, ImageDraw, ImageFont
007|
008| BasePath = r'C:\CJS\prj\Python\ws\PIL'
009| AnimPath = r'C:\CJS\prj\pov\ws\xyz\ani'
010| ImgsPath = path.join(BasePath, 'img')
011|
012| shrink_image = True
013|
014| dims = (6000, 6000)
015| shrink = (600, 600)
016| lims = ((-1.08,+1.08), (-1.08,+1.08))
017| frame = (lims[0][1]-lims[0][0], lims[1][1]-lims[1][0])
018| scale = (dims[0]//frame[0], dims[1]//frame[1])
019| edge = (dims[0]-1, dims[1]-1)
020|
021| border_width = 21
022| axis_width = 13
023| radius_tan = 0.30
024| radius_sine = 0.40
025| radius_half = 0.50
026| radius_full = 0.60
027| radius_1 = 0.65
028| radius_2 = 0.70
029| radius_3 = 0.75
030| radius_4 = 0.80
031| radius_5 = 0.85
032| radius_6 = 0.90
033| radius_7 = 0.95
034|
035| color_tang = (223, 191, 255)
036| color_sine = (223, 191, 255)
037| color_half = ( 63, 127, 191)
038| color_radians = (191, 223, 255)
039|
040| CircleAngles = [0,1, 1/2, 3/2,
041| 1/3, 2/3, 4/3, 5/3,
042| 1/4, 3/4, 5/4, 7/4,
043| 1/6, 5/6, 7/6, 11/6,
044| 1/12,5/12,7/12,11/12,13/12,17/12,19/12,23/12,
045| 1/pi]
046|
047| px = lambda x: ((x * scale[0]) + dims[0]//2)
048| py = lambda y: (dims[1]//2 - (y * scale[1]))
049|
050| euler = lambda a:complex(cos(a), sin(a))
051| z2t = lambda z:(px(z.real), py(z.imag))
052| ulc = lambda p,d:(p[0]-d, p[1]-d)
053| lrc = lambda p,d:(p[0]+d, p[1]+d)
054|
055| eye = complex(0, 1)
056| ipi = complex(pi,0) * eye
057|
058|
059|
060| def main (*args):
061| outname = path.join(BasePath, 'the-circle.png' if shrink_image else 'the-circle-big.png')
062|
063|
064| im = Image.new('RGB', dims, color=(255,255,255))
065| gi = ImageDraw.Draw(im)
066|
067| fnt0 = ImageFont.truetype('corbel.ttf', size=132)
068| fnt1 = ImageFont.truetype('corbelb.ttf', size=164)
069|
070|
071| def draw_circle (radius=1.0, parts=500, width=7, color=(127,127,255), stop=None):
072| ang = (2.0 * pi)/parts
073| rot = euler(ang)
074| dot = complex(radius, 0)
075|
076| point_list = [dot]
077| for ix in range(parts):
078| pt = point_list[-1] * rot
079| point_list.append(pt)
080| if stop and ((parts*stop) <= ix):
081| break
082| if stop is None:
083| point_list.append(dot)
084|
085| gi.line([z2t(z) for z in point_list], width=width, fill=color)
086|
087| draw_circle(radius=radius_tan , width= 9, color=color_tang, stop=0.25)
088| draw_circle(radius=radius_sine, width= 9, color=color_sine, stop=0.25)
089| draw_circle(radius=radius_half, width= 9, color=color_half, stop=0.50)
090|
091| draw_circle(radius=radius_full, width=37, color=(223,223,255))
092| draw_circle(radius=radius_full, width=29, color=(191,191,255))
093| draw_circle(radius=radius_full, width=21, color=(159,159,255))
094| draw_circle(radius=radius_full, width=13, color=(127,127,255))
095|
096| draw_circle(radius=radius_1, width= 9, color=color_radians)
097| draw_circle(radius=radius_2, width= 9, color=color_radians)
098| draw_circle(radius=radius_3, width= 9, color=color_radians)
099| draw_circle(radius=radius_4, width= 9, color=color_radians)
100| draw_circle(radius=radius_5, width= 9, color=color_radians)
101| draw_circle(radius=radius_6, width= 9, color=color_radians)
102| draw_circle(radius=radius_7, width= 9, color=color_radians)
103|
104|
105|
106| def draw_diagonal (angle, width=5, color=(63,159,63), radius=1.0, half=False):
107| pt0 = radius * exp(ipi*angle)
108| if half:
109| pt1 = complex(0,0)
110| else:
111| pt1 = radius * exp(ipi*angle) * exp(ipi)
112| gi.line([z2t(pt0), z2t(pt1)], width=width, fill=color)
113|
114|
115|
116|
117|
118|
119| draw_diagonal( 1/12, color=(0xff,0x66,0x33))
120| draw_diagonal( 5/12, color=(0xff,0x66,0x33))
121| draw_diagonal( 7/12, color=(0xff,0x66,0x33))
122| draw_diagonal(11/12, color=(0xff,0x66,0x33))
123|
124| draw_diagonal(1/6)
125| draw_diagonal(5/6)
126|
127| draw_diagonal(1/3, width=9)
128| draw_diagonal(2/3, width=9)
129|
130| draw_diagonal(1/4, width=13, color=(0x33,0x33,0x99))
131| draw_diagonal(3/4, width=13, color=(0x33,0x33,0x99))
132|
133| draw_diagonal(1/pi, width=17, color=(255,0,0), half=True, radius=1.10)
134|
135|
136|
137| gi.line([(px(0),py(-2)), (px(0),py(+2))], fill=(0x3f,0x3f,0x66), width=axis_width)
138| gi.line([(px(-2),py(0)), (px(+2),py(0))], fill=(0x3f,0x3f,0x66), width=axis_width)
139|
140|
141|
142| draw_circle(radius=1.0, width=69, color=(223,223,255))
143| draw_circle(radius=1.0, width=61, color=(207,207,255))
144| draw_circle(radius=1.0, width=53, color=(191,191,255))
145| draw_circle(radius=1.0, width=45, color=(159,159,255))
146| draw_circle(radius=1.0, width=37, color=(127,127,255))
147|
148|
149| for ang in CircleAngles:
150| pt = z2t(exp(ipi*ang))
151| gi.ellipse([ulc(pt,30),lrc(pt,30)], fill=(0,0,255), outline=(0,0,0), width=3)
152|
153|
154|
155| txt = '1 radian (57.296°)'
156| x,y = z2t(1.10*exp(eye))
157| bb = gi.textbbox((x,y), txt, font=fnt1)
158| gi.text((x+0,y-((bb[3]-bb[1])*1.0)), txt, font=fnt1, fill=(255,0,0))
159|
160| txt = 'tan'
161| x,y = (px(radius_tan), py(0))
162| gi.text((x,y), txt, font=fnt0, fill=(127,0,127))
163|
164| txt = 'cos\nsin'
165| x,y = (px(radius_sine), py(0))
166| gi.text((x,y), txt, font=fnt0, fill=(127,0,127))
167|
168|
169|
170| border_lines = [(0,0), (edge[0],0), (edge[0],edge[1]), (0,edge[1]), (0,0)]
171| shift_templt = [(+1,+1), (-1,+1), (-1,-1), (+1,-1), (+1,+1)]
172| border_color = (0, 0, 0)
173|
174| for ix in range(border_width):
175| gi.line(border_lines, fill=border_color, width=1)
176| border_lines = [(b[0]+s[0], b[1]+s[1]) for b,s in zip(border_lines,shift_templt)]
177| border_color = tuple([c+8 for c in border_color])
178|
179|
180|
181| if shrink_image:
182| im = im.resize(shrink)
183| im.save(outname)
184| print()
185| print('wrote: %s' % outname)
186| print()
187| print('dims=%s' % str(dims))
188| print('lims=%s' % str(lims))
189| print('frame=%s' % str(frame))
190| print('scale=%s' % str(scale))
191| print()
192| print('border_width=%d' % border_width)
193| print('axis_width=%d' % axis_width)
194| print()
195|
196|
197| if __name__ == '__main__':
198| print(argv[0])
199| print()
200| main(*argv[1:])
201| print()
202|
203| '''eof'''
204|