Debian Codegraph @

texmf-dist/tex/latex/tikz-mirror-lens/tikz-mirror-lens.sty

raw
1 % !TeX spellcheck = en_US
2 % !TeX encoding = UTF-8
3 % =============================
4
5 \NeedsTeXFormat{LaTeX2e}[1994/06/01]
6 \ProvidesPackage{tikz-mirror-lens}[2023-01-08 Custom Package for drawing spherical mirrors and lens -- FHZ -- Version 1.0.2]
7
8 \RequirePackage{amsmath}
9 \RequirePackage{tikz}
10 \usetikzlibrary{calc}
11 \usetikzlibrary{math}
12 \usetikzlibrary{decorations}
13 \usetikzlibrary{decorations.markings}
14
15 % =======================================================
16 % Variables
17 % f: focus of the mirror or lens
18 % p: position along x axis of the object
19 % pp: position along x axis of the object
20 % o: object height
21 % i: image height
22 % ymirror: mirror height
23 % epsilon: absolute distance between p and f
24 % yM: height of the mirror
25 % xL: Left extension of X axis
26 % xR: Right extension of X axis
27 % (x_C,y_C): Coordinates of the localization of displayed data
28 % arrows: optional argument to change arrows density
29 % =======================================================
30
31
32 % =======================================================
33 % Summary of main commands
34 % -- Mirrors
35 %\mirrorSphGauss[arrows]{f}{p}{o}{epsilon}
36 %\mirrorSphGaussCoord[arrows]{f}{p}{o}{epsilon}
37 %\mirrorSphGaussFixed[arrows]{f}{p}{o}{epsilon}{yM}{xL}{xR}
38 %\mirrorSphGaussFixedCoord[arrows]{f}{p}{o}{epsilon}{yM}{xL}{xR}{(x_C,y_C)}
39 % -- Lens
40 %\lensSphGauss[arrows]{f}{p}{o}{epsilon}
41 %\lensSphGaussCoord[arrows]{f}{p}{o}{epsilon}
42 %\lensSphGaussFixed[arrows]{f}{p}{o}{epsilon}{yM}{xL}{xR}
43 %\lensSphGaussFixedCoord[arrows]{f}{p}{o}{epsilon}{yM}{xL}{xR}{(x_C,y_C)}
44 % =======================================================
45
46 % =======================================================
47 % Mirrors and Lens -- Math
48 % =======================================================
49 % Summary of commands
50 % \mirrorMath{f}{p}{o}{epsilon}{ymirror}
51 % \lensMath{f}{p}{o}{epsilon}{ymirror}
52 % \lensMathL{f}{p}{o}{epsilon}{ymirror}
53
54 % =======================================================
55 \newcommand{\mirrorMath}[5]{
56 % Define mirror and object
57 \tikzmath{\f = #1; \v = 0; \c = 2*\f;}
58 \tikzmath{\p = #2; \o = #3;}
59 % Calculate image
60 \tikzmath{
61 if (abs(\p - \f) >= #4) then {
62 \pp = \p*\f/(\p-\f);
63 \i = -(\pp/\p)*\o;
64 } else {
65 \pp = 0;
66 \i = 0;
67 };
68 }
69 % Mirror size, math as parameter
70 \tikzmath{\ymirror = #5;}
71 }
72 \newcommand{\lensMath}[5]{
73 % f > 0 convergente, f < 0 divergente
74 % Define lens and object
75 \tikzmath{\f = #1; \v = 0; \a = 2*\f;}
76 \tikzmath{\p = #2; \o = #3;}
77 % Calculate image
78 \tikzmath{
79 if (abs(\p - \f) >= #4) then {
80 \pp = \p*(\f)/(\f-(\p));
81 \i = (\pp/\p)*\o;
82 } else {
83 \pp = 0;
84 \i = 0;
85 };
86 }
87 % Lens size, math as parameter
88 \tikzmath{\ymirror = #5;}
89 }
90 \newcommand{\lensMathL}[5]{
91 % f > 0 convergente, f < 0 divergente
92 % Define lens and object
93 \tikzmath{\f = #1; \v = 0; \a = 2*\f;}
94 \tikzmath{\p = #2; \o = #3;}
95 % Calculate image
96 \tikzmath{
97 if (abs(\p + \f) >= #4) then {
98 \pp = \p*\f/(\p+\f);
99 \i = (\pp/\p)*\o;
100 } else {
101 \pp = 0;
102 \i = 0;
103 };
104 }
105 % Lens size, math as parameter
106 \tikzmath{\ymirror = #5;}
107 }
108 % *******************************************************
109
110 % =======================================================
111 % Mirrors and Lens -- base
112 % =======================================================
113 % Summary of commands
114 % \mirrorLensObjIma{p}{pp}{o}{i}
115 % \mirrorLensCoord{p}{pp}{o}{i}{f}{x}
116 % =======================================================
117 \newcommand{\mirrorLensObjIma}[4]{
118 \begin{scope}[-stealth]
119 \draw[green!50!black] (#1,0) node[above right]{$p$} -- ++(0,#3) node[above left]{$o$} coordinate(O);
120 \tikzmath{
121 if (#2 != 0) then {
122 {
123 \draw[green] (#2,0) node[below left]{$p^{\prime}$} -- ++(0,#4) node[below left]{$i$};
124 };
125 };
126 }
127 \end{scope}
128 }
129 \newcommand{\mirrorLensCoord}[6]{
130 \begin{scope}
131 \node[right] at #6 {
132 $\begin{aligned}
133 f & = #5 \\
134 (p,o) & = (#1,#3) \\
135 (p^{\prime},i) & = (#2,#4)
136 \end{aligned}$
137 };
138 \end{scope}
139 }
140 % *******************************************************
141
142 % =======================================================
143 % Mirrors -- base
144 % =======================================================
145 % Summary of commands
146 % \mirrorBase{f}{ymirror}{minEixoX}{maxEixoX}
147 % \mirrorPts{v}{f}{c}
148 % \mirrorRays[arrow]{p}{pp}{o}{i}
149 % =======================================================
150 \newcommand{\mirrorBase}[4]{
151 \begin{scope}
152 \draw[dashed, extended line=15pt] (#3,0) -- (#4,0);
153 \foreach \ys in {1,-1}{
154 \begin{scope}[yscale=\ys]
155 \draw[blue] (0,0) -- ++(0,#2) arc (0:-sign(#1)*90:{-sign(#1)*0.5});
156 \foreach \x/\y in {0/0.05,0.05/0.20,0.15/0.35,0.45/0.5}{
157 \draw[blue] ({sign(#1)*\x},#2+\y) -- ++({-0.5},0);
158 }
159 \end{scope}
160 }
161 \end{scope}
162 }
163 \newcommand{\mirrorPts}[3]{
164 \begin{scope}[red]
165 \fill (#1,0) coordinate(V) circle(0.05) node[below left]{$V$};
166 \fill (#2,0) coordinate(F) circle(0.05) node[below]{$f$};
167 \fill (#3,0) coordinate(C) circle(0.05) node[below]{$c$};
168 \end{scope}
169 }
170 \newcommand{\mirrorRays}[5][60]{
171 \begin{scope}[thick,extended line=25pt]
172 \tikzmath{
173 if (#3 != 0) then {
174 {
175 \draw[arrDec={#1},cyan] (#2,#4) -- (0,#4) -- (F) -- (#3,#5);
176 \draw[arrDec={#1-10},magenta] (O) -- (F) -- (0,#5) -- (#3,#5);
177 \draw[arrDec={#1-10},violet,dotted] (O) -- (V) -- (#3,#5);
178 };
179 } else {
180 {
181 \draw[arrDec={#1},cyan] (#2,#4) -- (0,#4) -- (F);
182 \draw[arrDec={#1-10},violet,dotted] (O) -- (V) -- (#2,-#4);
183 };
184 };
185 }
186 \end{scope}
187 }
188 % *******************************************************
189
190 % =======================================================
191 % Lens - Base
192 % =======================================================
193 % Summary of commands
194 % \lensBase{f}{ymirror}{minEixoX}{maxEixoX}
195 % \lensPts{v}{f}{a}
196 % \lensRays[arrows]{p}{pp}{o}{i}
197 % =======================================================
198 \newcommand{\lensBase}[4]{
199 \begin{scope}
200 \draw[dashed, extended line=15pt] (#3,0) -- (#4,0);
201 \foreach \ys in {1,-1}{
202 \begin{scope}[yscale=\ys]
203 \draw[blue] (0,0) -- ++(0,#2);% arc (0:-sign(\f)*90:{-sign(\f)*0.5});
204 \draw[blue, fill=white] (0,#2) -- ++(0.25,0) -- ++(-0.25,{sign(#1)*0.25}) -- ++(-0.25,{-sign(#1)*0.25}) -- cycle;
205 \end{scope}
206 }
207 \end{scope}
208 }
209 \newcommand{\lensPts}[3]{
210 \begin{scope}[red]
211 \fill (#1,0) coordinate(V) circle(0.05) node[below left]{$V$};
212 \fill (#2,0) coordinate(F) circle(0.05) node[below]{$f_o$};
213 \fill (#3,0) coordinate(C) circle(0.05) node[below]{$A_0$};
214 \fill (-#2,0) coordinate(Fi) circle(0.05) node[below]{$f_i$};
215 \fill (-#3,0) coordinate(Ci) circle(0.05) node[below]{$A_i$};
216 \end{scope}
217 }
218 \newcommand{\lensRays}[5][60]{
219 \begin{scope}[thick,extended line=25pt]
220 \tikzmath{
221 if (#3 != 0) then {
222 {
223 \draw[arrDec={#1},cyan] (#2,#4) -- (0,#4) -- (Fi) -- (#3,#5);
224 \draw[arrDec={#1-10},magenta] (O) -- (F) -- (0,#5) -- (#3,#5);
225 \draw[arrDec={#1-10},violet,dotted] (O) -- (V) -- (#3,#5);
226 };
227 } else {
228 {
229 \draw[arrDec={#1},cyan] (#2,#4) -- (0,#4) -- (Fi);
230 \draw[arrDec={#1-10},violet,dotted] (O) -- (V) -- (-#2,-#4);
231 };
232 };
233 }
234 \end{scope}
235 }
236 % *******************************************************
237
238 % =======================================================
239 % Mirrors
240 % =======================================================
241 \newcommand{\mirrorSphGauss}[5][60]{
242 \begin{tikzpicture}[very thick,
243 arrDec/.style={
244 decoration={
245 markings,
246 mark= between positions 0.1 and 0.99 step #1pt with {\arrow{latex}}
247 },
248 postaction={decorate},
249 },
250 arrDec/.default=20,
251 extended line/.style={shorten >=-#1,shorten <=-#1},
252 extended line/.default=1cm
253 ]
254 % Math
255 \mirrorMath{#2}{#3}{#4}{#5}{max(abs(\o),abs(\i)) + 0.5}
256 % Mirror, Notable points, Object and image, Notable Rays, Coordinates
257 \mirrorBase{\f}{\ymirror}{{min(\v,\pp,\c)}}{{max(\c+0.5,\p,\pp)}}
258 \mirrorPts{\v}{\f}{\c}
259 \mirrorLensObjIma{\p}{\pp}{\o}{\i}
260 \mirrorRays[#1]{\p}{\pp}{\o}{\i}
261 \end{tikzpicture}
262 }
263 \newcommand{\mirrorSphGaussCoord}[5][60]{
264 \begin{tikzpicture}[very thick,
265 arrDec/.style={
266 decoration={
267 markings,
268 mark= between positions 0.1 and 0.99 step #1pt with {\arrow{latex}}
269 },
270 postaction={decorate},
271 },
272 arrDec/.default=20,
273 extended line/.style={shorten >=-#1,shorten <=-#1},
274 extended line/.default=1cm
275 ]
276 % Math
277 \mirrorMath{#2}{#3}{#4}{#5}{max(abs(\o),abs(\i)) + 0.5}
278 % Mirror, Notable points, Object and image, Notable Rays, Coordinates
279 \mirrorBase{\f}{\ymirror}{{min(\v,\pp,\c)}}{{max(\c+0.5,\p,\pp)}}
280 \mirrorPts{\v}{\f}{\c}
281 \mirrorLensObjIma{\p}{\pp}{\o}{\i}
282 \mirrorRays[#1]{\p}{\pp}{\o}{\i}
283 \mirrorLensCoord{\p}{\pp}{\o}{\i}{\f}{({max(\c+0.5,\p,\pp)+1},-1)}
284 \end{tikzpicture}
285 }
286 \newcommand{\mirrorSphGaussFixed}[8][60]{
287 \begin{tikzpicture}[very thick,
288 arrDec/.style={
289 decoration={
290 markings,
291 mark= between positions 0.1 and 0.99 step #1pt with {\arrow{latex}}
292 },
293 postaction={decorate},
294 },
295 arrDec/.default=20,
296 extended line/.style={shorten >=-#1,shorten <=-#1},
297 extended line/.default=1cm
298 ]
299 % Math
300 \mirrorMath{#2}{#3}{#4}{#5}{#6}
301 % Mirror, Notable points, Object and image, Notable Rays, Coordinates
302 \mirrorBase{\f}{\ymirror}{#7}{#8}
303 \mirrorPts{\v}{\f}{\c}
304 \mirrorLensObjIma{\p}{\pp}{\o}{\i}
305 \mirrorRays[#1]{\p}{\pp}{\o}{\i}
306 \end{tikzpicture}
307 }
308 \newcommand{\mirrorSphGaussFixedCoord}[9][60]{
309 \begin{tikzpicture}[very thick,
310 arrDec/.style={
311 decoration={
312 markings,
313 mark= between positions 0.1 and 0.99 step #1pt with {\arrow{latex}}
314 },
315 postaction={decorate},
316 },
317 arrDec/.default=20,
318 extended line/.style={shorten >=-#1,shorten <=-#1},
319 extended line/.default=1cm
320 ]
321 % Math
322 \mirrorMath{#2}{#3}{#4}{#5}{#6}
323 % Mirror, Notable points, Object and image, Notable Rays, Coordinates
324 \mirrorBase{\f}{\ymirror}{#7}{#8}
325 \mirrorPts{\v}{\f}{\c}
326 \mirrorLensObjIma{\p}{\pp}{\o}{\i}
327 \mirrorRays[#1]{\p}{\pp}{\o}{\i}
328 \mirrorLensCoord{\p}{\pp}{\o}{\i}{\f}{#9}
329 \end{tikzpicture}
330 }
331 % *******************************************************
332
333 % =======================================================
334 % Lens
335 % =======================================================
336 \newcommand{\lensSphGauss}[5][60]{
337 \begin{tikzpicture}[very thick,
338 arrDec/.style={
339 decoration={
340 markings,
341 mark= between positions 0.1 and 0.99 step #1pt with {\arrow{latex}}
342 },
343 postaction={decorate},
344 },
345 arrDec/.default=20,
346 extended line/.style={shorten >=-#1,shorten <=-#1},
347 extended line/.default=1cm
348 ]
349 % Math
350 \lensMath{#2}{#3}{#4}{#5}{max(abs(\o),abs(\i)) + 0.5}
351 % Lens, Notable points, Object and image, Notable Rays, Coordinates.
352 \lensBase{\f}{\ymirror}{{min(\v,\pp,\a)}}{{max(\a+0.5,\p,\pp)}}
353 \lensPts{\v}{\f}{\a}
354 \mirrorLensObjIma{\p}{\pp}{\o}{\i}
355 \lensRays[#1]{\p}{\pp}{\o}{\i}
356 \end{tikzpicture}
357 }
358 \newcommand{\lensSphGaussCoord}[5][60]{
359 \begin{tikzpicture}[very thick,
360 arrDec/.style={
361 decoration={
362 markings,
363 mark= between positions 0.1 and 0.99 step #1pt with {\arrow{latex}}
364 },
365 postaction={decorate},
366 },
367 arrDec/.default=20,
368 extended line/.style={shorten >=-#1,shorten <=-#1},
369 extended line/.default=1cm
370 ]
371 % Math
372 \lensMath{#2}{#3}{#4}{#5}{max(abs(\o),abs(\i)) + 0.5}
373 % Lens, Notable points, Object and image, Notable Rays, Coordinates.
374 \lensBase{\f}{\ymirror}{{min(\v,\pp,\a)}}{{max(\a+0.5,\p,\pp)}}
375 \lensPts{\v}{\f}{\a}
376 \mirrorLensObjIma{\p}{\pp}{\o}{\i}
377 \lensRays[#1]{\p}{\pp}{\o}{\i}
378 \mirrorLensCoord{\p}{\pp}{\o}{\i}{\f}{({max(\a+0.5,\p,\pp)+1},-1)}
379 \end{tikzpicture}
380 }
381 \newcommand{\lensSphGaussFixed}[8][60]{
382 \begin{tikzpicture}[very thick,
383 arrDec/.style={
384 decoration={
385 markings,
386 mark= between positions 0.1 and 0.99 step #1pt with {\arrow{latex}}
387 },
388 postaction={decorate},
389 },
390 arrDec/.default=20,
391 extended line/.style={shorten >=-#1,shorten <=-#1},
392 extended line/.default=1cm
393 ]
394 % Math
395 \lensMath{#2}{#3}{#4}{#5}{#6}
396 % Lens, Notable points, Object and image, Notable Rays, Coordinates.
397 \lensBase{\f}{\ymirror}{#7}{#8}
398 \lensPts{\v}{\f}{\a}
399 \mirrorLensObjIma{\p}{\pp}{\o}{\i}
400 \lensRays[#1]{\p}{\pp}{\o}{\i}
401 \end{tikzpicture}
402 }
403 \newcommand{\lensSphGaussFixedCoord}[9][60]{
404 \begin{tikzpicture}[very thick,
405 arrDec/.style={
406 decoration={
407 markings,
408 mark= between positions 0.1 and 0.99 step #1pt with {\arrow{latex}}
409 },
410 postaction={decorate},
411 },
412 arrDec/.default=20,
413 extended line/.style={shorten >=-#1,shorten <=-#1},
414 extended line/.default=1cm
415 ]
416 % Math
417 \lensMath{#2}{#3}{#4}{#5}{#6}
418 % Lens, Notable points, Object and image, Notable Rays, Coordinates.
419 \lensBase{\f}{\ymirror}{#7}{#8}
420 \lensPts{\v}{\f}{\a}
421 \mirrorLensObjIma{\p}{\pp}{\o}{\i}
422 \lensRays[#1]{\p}{\pp}{\o}{\i}
423 \mirrorLensCoord{\p}{\pp}{\o}{\i}{\f}{#9}
424 \end{tikzpicture}
425 }
426 % *******************************************************
427
428 % =======================================================
429 % Lens - L
430 % =======================================================
431 \newcommand{\lensSphGaussL}[5][60]{
432 \begin{tikzpicture}[very thick,
433 arrDec/.style={
434 decoration={
435 markings,
436 mark= between positions 0.1 and 0.99 step #1pt with {\arrow{latex}}
437 },
438 postaction={decorate},
439 },
440 arrDec/.default=20,
441 extended line/.style={shorten >=-#1,shorten <=-#1},
442 extended line/.default=1cm
443 ]
444 % Math
445 \lensMathL{#2}{#3}{#4}{#5}{max(abs(\o),abs(\i)) + 0.5}
446 % Lens, Notable points, Object and image, Notable Rays, Coordinates.
447 \lensBase{\f}{\ymirror}{{min(\v,\p,\pp,\a,-\a)}}{{max(\v,\p,\pp,\a,-\a)}}
448 \lensPts{\v}{-\f}{-\a}
449 \mirrorLensObjIma{\p}{\pp}{\o}{\i}
450 \lensRays[#1]{\p}{\pp}{\o}{\i}
451 \end{tikzpicture}
452 }
453 \newcommand{\lensSphGaussLCoord}[5][60]{
454 \begin{tikzpicture}[very thick,
455 arrDec/.style={
456 decoration={
457 markings,
458 mark= between positions 0.1 and 0.99 step #1pt with {\arrow{latex}}
459 },
460 postaction={decorate},
461 },
462 arrDec/.default=20,
463 extended line/.style={shorten >=-#1,shorten <=-#1},
464 extended line/.default=1cm
465 ]
466 % Math
467 \lensMathL{#2}{#3}{#4}{#5}{max(abs(\o),abs(\i)) + 0.5}
468 % Lens, Notable points, Object and image, Notable Rays, Coordinates.
469 \lensBase{\f}{\ymirror}{{min(\v,\p,\pp,\a,-\a)}}{{max(\v,\p,\pp,\a,-\a)}}
470 \lensPts{\v}{-\f}{-\a}
471 \mirrorLensObjIma{\p}{\pp}{\o}{\i}
472 \lensRays[#1]{\p}{\pp}{\o}{\i}
473 \mirrorLensCoord{\p}{\pp}{\o}{\i}{\f}{({max(\v,\p,\pp,\a,-\a)},-1)}
474 \end{tikzpicture}
475 }
476 \newcommand{\lensSphGaussLFixed}[8][60]{
477 \begin{tikzpicture}[very thick,
478 arrDec/.style={
479 decoration={
480 markings,
481 mark= between positions 0.1 and 0.99 step #1pt with {\arrow{latex}}
482 },
483 postaction={decorate},
484 },
485 arrDec/.default=20,
486 extended line/.style={shorten >=-#1,shorten <=-#1},
487 extended line/.default=1cm
488 ]
489 % Math
490 \lensMathL{#2}{#3}{#4}{#5}{#6}
491 % Lens, Notable points, Object and image, Notable Rays, Coordinates.
492 \lensBase{\f}{\ymirror}{#7}{#8}
493 \lensPts{\v}{-\f}{-\a}
494 \mirrorLensObjIma{\p}{\pp}{\o}{\i}
495 \lensRays[#1]{\p}{\pp}{\o}{\i}
496 \end{tikzpicture}
497 }
498 \newcommand{\lensSphGaussLFixedCoord}[9][60]{
499 \begin{tikzpicture}[very thick,
500 arrDec/.style={
501 decoration={
502 markings,
503 mark= between positions 0.1 and 0.99 step #1pt with {\arrow{latex}}
504 },
505 postaction={decorate},
506 },
507 arrDec/.default=20,
508 extended line/.style={shorten >=-#1,shorten <=-#1},
509 extended line/.default=1cm
510 ]
511 % Math
512 \lensMathL{#2}{#3}{#4}{#5}{#6}
513 % Lens, Notable points, Object and image, Notable Rays, Coordinates.
514 \lensBase{\f}{\ymirror}{#7}{#8}
515 \lensPts{\v}{-\f}{-\a}
516 \mirrorLensObjIma{\p}{\pp}{\o}{\i}
517 \lensRays[#1]{\p}{\pp}{\o}{\i}
518 \mirrorLensCoord{\p}{\pp}{\o}{\i}{\f}{#9}
519 \end{tikzpicture}
520 }
521 % *******************************************************
522
523 \endinput