1

Is ist possible to apply a shading to a path perpendicular to it's current tangent.

I want to draw some kind of 3d-pipe. My question is related to this question. I want to draw 3d-molecules and I am struggeling with how to draw the bonds between the atoms.

I'd like to have something like

\draw[bond color=red; bond width=15pt] (atom1) -- (atom2);

The result should be a pole from coordinate atom1 to coordinate atom2. Pole means a rectangle with a length of the distance beween atom1 and atom2 and a height of 15pt filled with a shade red-white-red along the height axis.

I could actually do that using \shade and shading angle but then I'd have to calculate the shading angle myself depending on the coordinates of atom1 and atom2.

Any suggestions for that?

Thanks

  • 1
    Can you fill in the missing details with the code and possibly the wrong output? – percusse Sep 7 '14 at 12:59
4

Ideally a dedicated decoration could be used (similar to the one that the mindmap library provides for connecting concepts), but here is a simple example using the show path construction decoration.

The shading is applied manually to (a) illustrate how it can be done (b) bypass the default process of clipping the shading to the center quarter.

\documentclass[tikz,border=5]{standalone}
\usetikzlibrary{decorations.pathreplacing}

\colorlet{bar.top}{red}
\colorlet{bar.bottom}{blue}
\pgfdeclareverticalshading[bar.top,bar.bottom]{shaded.bar}{100bp}{
color(0bp)=(bar.bottom);color(100bp)=(bar.top)}

\tikzset{shading bar/.style args={#1 thick from #2 to #3}{
  decoration={show path construction,
  lineto code={
    \pgfscope%
    %
    % Set the colors.
    \colorlet{bar.top}{#2}%
    \colorlet{bar.bottom}{#3}%
    %
    % Manually apply the shading.
    % As the bar is a rectangle, scale the shading (which is 100bp x 100bp).
    % Other bar shapes would other adjustments before applying the shading.
    \pgftransformshift{\pgfpointdecoratedinputsegmentfirst}%
    \pgftransformrotate{\pgfdecoratedangle}%
    \pgftransformxscale{\pgfdecoratedinputsegmentlength/100bp}%
    \pgftransformyscale{#1/100bp}%
    %
    % Although the shading is a (scaled) rectangle the
    % bounding box needs to be updated to the correct size
    % which is done here using clipping.
    \pgfpathrectangle{\pgfqpoint{0bp}{-50bp}}{\pgfqpoint{100bp}{100bp}}%
    \pgfusepath{clip}% 
    \pgftransformshift{\pgfqpoint{50bp}{0bp}}%   
    \pgflowlevelsynccm%
    \pgfuseshading{shaded.bar}%
    \endpgfscope%
  }}, decorate
}}
\begin{document}

\begin{tikzpicture}
\draw (0,0) grid (5,5);
\draw [shading bar=5mm thick from red to orange] (0,0) -- (3,2);     
\draw [shading bar=5mm thick from blue to cyan] (0,2) -- (2,5); 
\draw [shading bar=10mm thick from green to yellow] (3,5) -- (5,0); 
\end{tikzpicture}

\end{document}

enter image description here

  • Thanks for your really helpful answer. I could modify your code in a way that it suits my needs. – Johannes Mueller Sep 9 '14 at 19:56

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.