trigonometry.html
001|
002| from sys import argv
003| from os import path
004| from math import sqrt, pi,tau, sin,cos
005| from cmath import exp
006| from PIL import Image, ImageDraw, ImageFont
007| from goodies import intervals
008|
009| BasePath = r'C:\CJS\prj\Python\blog\proj'
010| ImgsPath = path.join(BasePath, 'img')
011|
012|
013|
014| angle = float(argv[1]) if 1 < len(argv) else 0.103
015| frame = int(argv[2]) if 2 < len(argv) else 0
016| shrink = True
017| s_dims = (600, 600)
018| show_x = True
019| show_y = True
020| show_p = True
021| show_r = False
022|
023| dims = (6000, 6000)
024| lims = ((-1.50,+1.50), (-1.50,+1.50))
025| wndw = (lims[0][1]-lims[0][0], lims[1][1]-lims[1][0])
026| scale = (dims[0]//wndw[0], dims[1]//wndw[1])
027| edge = (dims[0]-1, dims[1]-1)
028|
029| border_width = 21
030|
031| px = lambda x: ((x * scale[0]) + dims[0]//2)
032| py = lambda y: (dims[1]//2 - (y * scale[1]))
033|
034| euler = lambda a:complex(cos(a), sin(a))
035| z2t = lambda z:(px(z.real), py(z.imag))
036| ulc = lambda p,d:(p[0]-d, p[1]-d)
037| lrc = lambda p,d:(p[0]+d, p[1]+d)
038|
039|
040| def draw_circle (draw, radius=1.0, parts=500, width=7, color=(127,127,255), stop=None):
041| '''Draw an arc at a specified radius.'''
042| if stop == 0.0:
043| return
044| rot = euler((2.0 * pi)/parts)
045| dot = complex(radius, 0)
046|
047| point_list = [dot]
048| for ix in range(parts):
049| pt = point_list[-1] * rot
050| point_list.append(pt)
051| if stop and ((parts*stop) <= ix):
052| break
053| if stop is None:
054| point_list.append(dot)
055|
056| draw.line([z2t(z) for z in point_list], width=width, fill=color)
057|
058|
059| def make_image ():
060| im = Image.new('RGB', dims, color=(255,255,255))
061| gi = ImageDraw.Draw(im)
062|
063| fnt1 = ImageFont.truetype('corbelb.ttf', size=380)
064| fnt2 = ImageFont.truetype('arial.ttf' , size=220)
065| fnt3 = ImageFont.truetype('times.ttf' , size=420)
066|
067|
068| for x in intervals(-2.0, +2.0, 0.1):
069| gi.line([(px(x),py(-2.0)), (px(x),py(+2.0))], fill=(0xbf,0xbf,0xff), width=9)
070| for y in intervals(-2.0, +2.0, 0.1):
071| gi.line([(px(-2.0),py(y)), (px(+2.0),py(y))], fill=(0xbf,0xbf,0xff), width=9)
072|
073| for x in intervals(-2.0, +2.0, 1.0):
074| gi.line([(px(x),py(-2.0)), (px(x),py(+2.0))], fill=(0x7f,0x7f,0xff), width=13)
075| for y in intervals(-2.0, +2.0, 1.0):
076| gi.line([(px(-2.0),py(y)), (px(+2.0),py(y))], fill=(0x7f,0x7f,0xff), width=13)
077|
078|
079| gi.line([(px(0),py(-2)), (px(0),py(+2))], fill=(0x3f,0x3f,0x3f), width=17)
080| gi.line([(px(-2),py(0)), (px(+2),py(0))], fill=(0x3f,0x3f,0x3f), width=17)
081|
082|
083| draw_circle(gi, width=69, color=(223,223,223))
084| draw_circle(gi, width=61, color=(207,207,207))
085| draw_circle(gi, width=53, color=(191,191,191))
086| draw_circle(gi, width=45, color=(159,159,159))
087| draw_circle(gi, width=37, color=(127,127,127))
088|
089|
090| projx = cos(tau * angle)
091| projy = sin(tau * angle)
092|
093| cpx = px(0)
094| cpy = py(0)
095| tpx = px(projx)
096| tpy = py(projy)
097|
098|
099| corners = [(cpx,cpy), (tpx,cpy), (tpx,tpy), (cpx,cpy)]
100| gi.polygon(corners, fill=(0xbf,0xbf,0xbf), outline=(0,0,0), width=1)
101|
102| corners = [(cpx,cpy), (cpx,tpy), (tpx,tpy), (cpx,cpy)]
103| gi.polygon(corners, fill=(0xef,0xef,0xef), outline=(0,0,0), width=1)
104|
105|
106| draw_circle(gi, radius=0.33, width=33, color=(0,0,0xff), stop=angle)
107|
108|
109| gi.line([(cpx,cpy), (tpx,cpy)], fill=(0xff,0x0f,0x0f), width=51)
110| gi.line([(cpx,tpy), (tpx,tpy)], fill=(0xff,0x0f,0x0f), width=21)
111|
112| gi.line([(cpx,cpy), (cpx,tpy)], fill=(0x0f,0x99,0x0f), width=21)
113| gi.line([(tpx,cpy), (tpx,tpy)], fill=(0x0f,0x99,0x0f), width=51)
114|
115| gi.line([(cpx,cpy), (tpx,tpy)], fill=(0x1f,0x1f,0xff), width=43)
116|
117|
118| pt = (cpx,cpy)
119| gi.ellipse([ulc(pt,50),lrc(pt,50)], fill=(0,0,0), outline=None, width=0)
120|
121|
122| pt = (tpx,tpy)
123| gi.ellipse([ulc(pt,50),lrc(pt,40)], fill=(0,0,0), outline=None, width=0)
124| pt = (tpx,cpy)
125| gi.ellipse([ulc(pt,40),lrc(pt,30)], fill=(0,0,0), outline=None, width=0)
126| pt = (cpx,tpy)
127| gi.ellipse([ulc(pt,40),lrc(pt,30)], fill=(0,0,0), outline=None, width=0)
128|
129| pt = (px(+1.0),py( 0.0))
130| gi.ellipse([ulc(pt,33),lrc(pt,33)], fill=(0xff,0,0), outline=None, width=0)
131| pt = (px(-1.0),py( 0.0))
132| gi.ellipse([ulc(pt,33),lrc(pt,33)], fill=(0xff,0,0), outline=None, width=0)
133| pt = (px( 0.0),py(+1.0))
134| gi.ellipse([ulc(pt,33),lrc(pt,33)], fill=(0,0xcc,0), outline=None, width=0)
135| pt = (px( 0.0),py(-1.0))
136| gi.ellipse([ulc(pt,33),lrc(pt,33)], fill=(0,0xcc,0), outline=None, width=0)
137|
138|
139| gi.text((px(+1.26), py(+0.16)), '+X', font=fnt1, fill=(0,0,0))
140| gi.text((px(-1.49), py(+0.16)), '-X', font=fnt1, fill=(0,0,0))
141|
142| gi.text((px(+0.01), py(+1.25)), '+Y', font=fnt1, fill=(0,0,0))
143| gi.text((px(+0.01), py(-1.10)), '-Y', font=fnt1, fill=(0,0,0))
144|
145| gi.text((px(-0.25), py(-0.01)), '(0,0)' , font=fnt2, fill=(0,0,0))
146| gi.text((px(+1.02), py(-0.01)), '(+1,0)', font=fnt2, fill=(0,0,0))
147| gi.text((px(-1.28), py(-0.01)), '(-1,0)', font=fnt2, fill=(0,0,0))
148| gi.text((px(-0.31), py(+1.13)), '(0,+1)', font=fnt2, fill=(0,0,0))
149| gi.text((px(-0.28), py(-1.00)), '(0,-1)', font=fnt2, fill=(0,0,0))
150|
151| if show_r:
152| bb = gi.textbbox((0,0), '1', font=fnt1)
153| if angle <= 0.5:
154|
155| tx = px(projx/2) - ((bb[2]-bb[0]) // 2)
156| ty = py(projy/2) - int((bb[3]-bb[1]) * 2.3)
157| else:
158|
159| tx = px(cos(tau*angle)*0.5) - ((bb[2]-bb[0]) // 2)
160| ty = py(sin(tau*angle)*0.5)
161| gi.text((tx, ty), '1', font=fnt1, fill=(0,0,0))
162|
163| if show_x:
164| bb = gi.textbbox((0,0), 'x', font=fnt3)
165| tx = tpx - ((bb[2]-bb[0]) // 2)
166| ty = (py(0.00)-((bb[3]-bb[1])*2.5)) if projy < 0.0 else (py(0.00)-((bb[3]-bb[1]) // 2))
167| gi.text((tx, ty), 'x', font=fnt3, fill=(0xff,0,0))
168|
169| if show_y:
170| bb = gi.textbbox((0,0), 'y', font=fnt3)
171| tx = px(0.03) if projx < 0.0 else px(-0.15)
172| ty = tpy - (bb[3]-bb[1])
173| gi.text((tx, ty), 'y', font=fnt3, fill=(0,0x99,0))
174|
175| if show_p:
176| bb = gi.textbbox((0,0), 'p', font=fnt3)
177| tx = px(1.15 * cos(tau*(angle))) - ((bb[2]-bb[0]) // 2)
178| ty = py(1.15 * sin(tau*(angle))) - (bb[3]-bb[1])
179| gi.text((tx, ty), 'p', font=fnt3, fill=(0,0,200))
180|
181| bb = gi.textbbox((0,0), 'a', font=fnt3)
182| if (1/4) <= angle:
183|
184| tx = px(0.42 * cos(tau*(1/8))) - ((bb[2]-bb[0]) // 2)
185| ty = py(0.42 * sin(tau*(1/8))) - ((bb[3]-bb[1]) * 2)
186| elif angle < (1/20):
187|
188| tx = px(0.35 * cos(tau*(24/25))) - ((bb[2]-bb[0]) // 2)
189| ty = py(0.35 * sin(tau*(24/25))) - int((bb[3]-bb[1]) * 1.5)
190| else:
191|
192| tx = px(0.42 * cos(tau*(angle/2.0))) - ((bb[2]-bb[0]) // 2)
193| ty = py(0.42 * sin(tau*(angle/2.0))) - int((bb[3]-bb[1]) * 1.5)
194| gi.text((tx, ty), 'a', font=fnt3, fill=(0,0,0))
195|
196|
197|
198|
199|
200|
201|
202|
203|
204|
205|
206|
207| if shrink:
208| im = im.resize(s_dims)
209| fn = (f'trig_{frame:04d}.png') if frame else 'trigonometry.png'
210| outname = path.join(ImgsPath, fn)
211| else:
212| outname = path.join(ImgsPath, 'trigonometry-big.png')
213| im.save(outname)
214| print('wrote: %s' % outname)
215| print()
216|
217|
218| print()
219| print('dims=%s' % str(dims))
220| print('lims=%s' % str(lims))
221| print('wndw=%s' % str(wndw))
222| print('scale=%s' % str(scale))
223| print()
224| print()
225|
226|
227| def main ():
228| make_image()
229|
230|
231| if __name__ == '__main__':
232| print(argv[0])
233| print()
234| main()
235| print()
236|
237| '''eof'''
238|