TransWikia.com

Shading in a rotation without a canvas transformation

TeX - LaTeX Asked on March 17, 2021

I’m currently working on a magnifying glass as an object without making use of the spy library. It is written as a newcommand since I have to use it to some extense. My problem is the shading for the handle. I work with shade but as soon as I turn to angles different than 90, 180 or 270, the shading looks strange. A canvas transformation had the desired effect, but since I use this command inside scope with scaling and shifting, a canvas transformation placed the magnifying glass at the wrong coordinates. Is there a way how can I achieve the shading of the handle like for the first magnifying glass for a different angle and get the correct coordinates when I call this command inside a scaled or shifted scope?

I hope the question is clear and I apologize in advance for the rather strange construction of the coordinates, I did not clean up the code yet. A minimal example looks like this

Thank you very much

Minimal example with some explanation:

The first argument is the x-coordinate of the center of the magnifying glass, the second argument the y-coordinate. The third one is the radius and the fourth gives the angle of the handle.

  documentclass{article}

usepackage{tikz}                               %Tikz ist kein Zeichenprogramm
usepackage{tikz-3dplot}                    %3D Plots

usetikzlibrary{arrows,intersections, decorations,circuits.ee.IEC, calc, decorations.markings,pgfplots.groupplots,3d,shadows,spy,fadings,fit}

usepackage{pgfplots}                       %Für Plots
%usepgfplotslibrary{groupplots}
usepgfplotslibrary{polar}

pgfplotsset{%
cycle list={blueredgreenorange},
every axis plot post/.append style={mark=none, smooth},
compat=1.11
}

newcommand{lupe}[4]{Slupe{#1}{#2}{#3}{#4}{5}{#3*.15}{.5}{.05}}
newcommand{Slupe}[8]{
begin{scope}[rotate around={#4:(#1,#2)}]
%begin{scope}[transform canvas={rotate around={#4:(#1,#2)}}]
draw [name path=Glassrand] (#1,#2) circle (#3);
shade[ball color=blue,opacity=#8] (#1,#2) circle (0.95*#3);
draw[fill=gray,even odd rule] (#1,#2) circle (#3) circle (0.95*#3) ;
draw (#1,#2) circle (0.95*#3);


path[draw=none,name path=linkerstiel] (#1,#2)--++  (0:#3+#3)--++(0+90:#6)--(#1,#2);
path[draw=none,name path=rechterstiel] (#1,#2)--++  (0:#3+#3)--++(0-90:#6)--(#1,#2);
path[draw=none,name path=linkerstielcenter] (#1,#2)--++(90:#6) coordinate (StielBreite1);
path[draw=none,name path=rechterstielcebter] (#1,#2)--++(-90:#6) coordinate (StielBreite2);

path[draw=none]  (#1,#2) --++ (0:3*#3) --++(90:#6) coordinate (StielEnde1);
path[draw=none]  (#1,#2) --++ (0:3*#3)--++(-90:#6) coordinate (StielEnde2);

path[draw=none,name path=StielAusrichtungEins] (StielEnde1) -- (StielBreite1);
path[draw=none,name path=StielAusrichtungZwei] (StielEnde2) -- (StielBreite2);

path [name intersections={of=Glassrand and StielAusrichtungEins}];
coordinate (StielAnfang1) at (intersection-1);

path [name intersections={of=Glassrand and StielAusrichtungZwei}];
coordinate (StielAnfang2) at (intersection-1);

coordinate (griff1) at ($(StielAnfang1) !1! (StielEnde1)$);
coordinate (griff2) at ($(StielAnfang2) !1! (StielEnde2)$);

coordinate (griff3) at ($(StielAnfang1) !.15! (StielEnde1)$);
coordinate (griff4) at ($(StielAnfang2) !.15! (StielEnde2)$);

path  (griff3)--++(0,.1*#3) coordinate (griff5);
path  (griff4)--++(0,-.1*#3) coordinate (griff6);
path  (StielEnde1)--++(0,.1*#3) coordinate (griff7);
path  (StielEnde2)--++(0,-.1*#3) coordinate (griff8);

coordinate (fixpunkt1) at ($(griff7)!.3!(griff8)$);
coordinate (fixpunkt2) at ($(griff5)!.3!(griff6)$);
coordinate (fixpunkt3) at ($(StielAnfang1)!.3!(StielAnfang2)$);
coordinate (fixpunkt4) at ($(griff1)!.3!(griff2)$);

path [draw=none] (griff1) --++(0,.1*#3) coordinate (test1);
path [draw=none] (griff1) --++(.1*#3,0) coordinate (test2);

path [draw=none] (griff2) --++(0,-.1*#3) coordinate (test3);
path [draw=none] (griff2) --++(.1*#3,0) coordinate (test4);

shade[top color=black, bottom color=white,shading angle=#4] (StielAnfang1) -- (fixpunkt3)--(fixpunkt4) -- (griff1);
shade[top color=white, bottom color=black,shading angle=#4] (StielAnfang2) -- (fixpunkt3)--(fixpunkt4) -- (griff2);
%end{scope}
end{scope}
}
begin{document}
begin{tikzpicture}
lupe{0}{0}{1}{0}
lupe{0}{-2}{1}{-45}
end{tikzpicture}


end{document}

One Answer

enter image description here

  1. I tried to have the lighting on the handle coherent with the handle's position. You can modify the result by modifying the function forHColor.

  2. I simplified the handle's construction; it is based on the introduction of the points H{+} and H{-}.

documentclass[11pt, border=1cm]{standalone}
usepackage{tikz}
usetikzlibrary{calc, math}

begin{document}

tikzmath{
  function forHColor(i, b) {
    if b<91 then {
      return 50*(cos((1-i)^2*360)+1);
    } else {
      return 50*(cos(i^2*360)+1);
    };
  };
}

tikzset{
  pics/magnifying/.code args={xcenter #1, ycenter #2, radius #3, hangle #4}{
    tikzmath{
      coordinate H{+}, H{-};
      real width, da, qw;
      da = 8;  % controls handle's width
      H{+} = (#1, #2) + (#4+da : #3);
      H{-} = (#1, #2) + (#4-da : #3);
      width = veclen(Hx{+}-Hx{-}, Hy{+}-Hy{-});  % handle's width
      qw = .05;  % controls the number of steps for handle's color
      {%
        foreach i [evaluate=i as cm using {forHColor(i, #4)}]
        in {0, qw, ..., 1}{%
          draw[gray!70!black!cm, line width={width/10}]
          ($(H{+})!i!(H{-})$) -- ++(#4 : 2.1*#3);  % handle's length
        }
      };
      { shade[ball color=blue, opacity=.07] (#1, #2) circle (.95*#3);
        draw[very thin, fill=gray, even odd rule]
        (#1, #2) circle (#3) circle (.95*#3);
      };
    }
  }
}

begin{tikzpicture}
  draw pic {magnifying={xcenter -1, ycenter -2, radius 1.4, hangle 30}};
  draw pic {magnifying={xcenter 5, ycenter 0, radius 1.7, hangle 130}};  
end{tikzpicture}

end{document}

Answered by Daniel N on March 17, 2021

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP