I wanted to draw a convex lens in tikzpicture. I'm fairly new to writing these commands, so I drafted the following.
% Convex lens
% \convex[R0,R1,w,h] at (x,y);
\def\convex[#1,#2,#3,#4] at (#5,#6){
% circle-circle intersection maths
% with adjustment for clipping at specified height
\def\R{#1};\def\r{#2};\def\d{(\R+\r-#3)};
\def\x{(((\d*\d)-(\r*\r)+(\R*\R))/(2*\d))};\def\c{(\d-\x)};
\def\y{sqrt((\R*\R)-(\x*\x))};\def\yh{min(\y,#4)};
\def\g{asin(\yh/\R)};\def\p{asin(\yh/\r)};
% sphere0 curves
\draw ({#5-\x+\R},#6) arc (0:\g:\R);
\draw ({#5-\x+\R},#6) arc (0:-\g:\R);
% sphere1 curves
\draw ({#5+\c-\r},#6) arc (180:180+\p:\r);
\draw ({#5+\c-\r},#6) arc (180:180-\p:\r);
% connecting lines (if clipped)
\def\xg{\R*cos(\g)};\def\xp{\r*cos(\p)};
\draw ({#5+\xg-\x},{\yh}) -- ({#5-\xp+\c},{\yh});
\draw ({#5+\xg-\x},{-\yh}) -- ({#5-\xp+\c},{-\yh});
}
This works, but I'd like to improve it a bit. Specific questions follow.
- Does it make sense to pass R0, R1, w, and h in this way? Is there a more standard way to pass arguments that is more in line with tikz/pgf philosophy?
- How can I pass optional arguments such as
thickto my\drawcommands? It seems like\defdoes not allow optional arguments. Is there another command I can use to define my lens? - Is there a better way to do this math in tikz/pgf (i.e. can it be done in fewer commands/with less braces and parentheses)?
pic. – Dec 08 '20 at 23:57