1

I have a figure with various lines which needs to have a gradient on a side as in the image below:

enter image description here

The direction of the gradient should be perpendicular to the red thick line starting with dark green on the line itself and then becoming fully transparent (where now is black).

How can I fix the position of the gradient (as its starting points seems to be outside the rectangle) and make the black fully transparent (using transparent doesn't seem to work properly)? I know I should use a canvas transformation but I can’t do it properly as I can no longer use the coordinates I saved.

Here's the MWE used to generate the image:

\documentclass{article}

\usepackage{tikz,pgf}
\usetikzlibrary{shapes,positioning,decorations.pathreplacing,intersections,calc}
\usepackage{color}
\definecolor{darkgreen}{rgb}{0,0.4,0}

%Length and angle for a segment in tikz
\makeatletter      
\newcommand{\getAngle}[2]{%
    \pgfmathanglebetweenpoints{\pgfpointanchor{#1}{center}}
                              {\pgfpointanchor{#2}{center}}
    \global\let\angle\pgfmathresult % we need a global macro 
}
\makeatother

\begin{document}
\begin{tikzpicture}
    \draw[->] (0,0) coordinate(origin)
    ++(-.2,0) -- ++(6.5,0) node[right]{$x_1$};
    \draw[->] (origin) ++(0,-.2) -- ++(0,5.5) node[above]{$x_2$};

    \draw[blue,shorten >= -.1cm, shorten <=-.1cm] (4,0) coordinate(v1_1) -- (4,5) coordinate(v1_2); % eq 1
    \path ($(v1_1)!-.1cm!(v1_2)$) coordinate(v1_1) ($(v1_2)!-.1cm!(v1_1)$) coordinate(v1_2);

    \draw[red,shorten >= -.1cm, shorten <=-.1cm] (0,5) coordinate(v2_1) -- (4,5) coordinate(v2_2);% eq 2
    \path ($(v2_1)!-.1cm!(v2_2)$) coordinate(v2_1) ($(v2_2)!-.1cm!(v2_1)$) coordinate(v2_2);

    \path[shorten >= 1.1cm, shorten <=-.1cm] (6,0) coordinate(v3_1) -- (2,6) coordinate(v3_2); % eq 3
    \path ($(v3_1)!-.1cm!(v3_2)$) coordinate(v3_1) ($(v3_2)!1.1cm!(v3_1)$) coordinate(v3_2);
    \getAngle{v3_1}{v3_2}
    \begin{scope}[rotate=\angle]
        \path (v3_2) -- ++(90:.7) coordinate(v3_3) (v3_1) -- ++(90:.7) coordinate(v3_4);
        % Here black should become transparent
        \shade[left color=black,right color=darkgreen,shading angle=\angle] (v3_3) rectangle (v3_1);
    \end{scope}
    \draw[red,ultra thick] (v3_1) -- (v3_2);
\end{tikzpicture}
\end{document}
2

i suspect, that you looking for this:

enter image description here

i slightly rewrote your mwe. instead of rectangle i use node with rectangle shape and tikz library fadings. i also remove all not used coordinates.

\documentclass[tikz, margin=3pt]{standalone}
\usetikzlibrary{calc,
                fadings,
                scopes}

\usepackage{xcolor}
\definecolor{darkgreen}{rgb}{0,0.4,0}

%Length and angle for a segment in tikz
\newcommand{\getAngle}[2]{%
    \pgfmathanglebetweenpoints{\pgfpointanchor{#1}{center}}
                              {\pgfpointanchor{#2}{center}}
    \global\let\angle\pgfmathresult % we need a global macro
}

\begin{document}
    \begin{tikzpicture}[
shorten <>/.style = {shorten <=#1, shorten >=#1}
                        ]
% axis
\draw[->] (-.2,0) -- ++ (6.5,0) node[right] {$x_1$};
\draw[->] (0,-.2) -- ++ (0,5.5) node[above] {$x_2$};
%
\draw[blue,shorten <>=-.1cm]    (4,0) -- (4,5); % eq 1, coordinate (v2_1) isn't used ...
\draw[red, shorten <>=-.1cm]    (0,5) -- (4,5); % eq 2, coordinate (v2_2) isn't used ...
% shape coordinates
\coordinate (v3_1) at (6,0);
\coordinate (v3_2) at (2,6);
% coordinates position correction
\path   ($(v3_1)!-.1cm!(v3_2)$) coordinate(v3_1)
        ($(v3_2)!1.1cm!(v3_1)$) coordinate(v3_2);
\getAngle{v3_1}{v3_2}
\scoped[transform canvas={rotate around={\angle:(v3_2)}}]
\path   let \p1 = ($(v3_1)-(v3_2)$),
            \n1 = {veclen(\y1,\x1)} in
        node [minimum width=\n1, minimum height=7mm,
              path fading=north,
              top color=black, bottom color=darkgreen,
              anchor=south west,
             ]  at (v3_2) {};
\draw[red,ultra thick] (v3_1) -- (v3_2);
    \end{tikzpicture}
\end{document}

note: due to used transform canvas the node shape spill out of image bounding box.

  • Not quite the right gradient, please see my updated answer – Marco Boschi Mar 8 '18 at 14:48
  • @MarcoBoschi, it might be, but now i haven't time to any further investigation not your answer not mine :-( – Zarko Mar 8 '18 at 15:06
  • @MarcoBoschi, i corrected fading of used node. you have to be careful when using the transform canvas . see corrected answer. – Zarko Mar 8 '18 at 15:56
2

This is probably not optimal as I am no expert in LaTeX, but here's how I managed to achieve what I wanted (the objective was to do the same for all the other lines but it is easier for vertical and horizontal lines):

\usetikzlibrary{fadings} % Put in preamble
\begin{tikzpicture}
    \draw (0,0) coordinate(origin);

    \path[shorten >= -.1cm, shorten <=-.1cm] (4,0) coordinate(v1_1) -- (4,5) coordinate(v1_2); % eq 1
    \path[shorten >= -.1cm, shorten <=-.1cm] (0,5) coordinate(v2_1) -- (4,5) coordinate(v2_2);% eq 2
    \path[shorten >= .5cm, shorten <=-.1cm] (6,0) coordinate(v3_1) -- (2,6) coordinate(v3_2); % eq 3

    \path (origin) ++(-.1,0) coordinate(v4_1) (v1_1) ++(.1,0) coordinate(v4_2);
    \fill[opacity=.5,gray,path fading=north] (v4_1) ++(0,.7) rectangle (v4_2);
    \path (origin) ++(0,-.1) coordinate(v5_1) (v2_1) ++(0,.1) coordinate(v5_2);
    \fill[opacity=.5,gray,path fading=east] (v5_1) ++(.7,0) rectangle (v5_2);

    \path ($(v1_1)!-.1cm!(v1_2)$) coordinate(v1_1) ($(v1_2)!-.1cm!(v1_1)$) coordinate(v1_2);
    \fill[opacity=.5,blue,path fading=west] (v1_2) ++(-.7,0) rectangle (v1_1);
    \draw[blue] (v1_1) -- (v1_2);

    \path ($(v2_1)!-.1cm!(v2_2)$) coordinate(v2_1) ($(v2_2)!-.1cm!(v2_1)$) coordinate(v2_2);
    \fill[opacity=.5,red,path fading=south] (v2_2) ++(0,-.7) rectangle (v2_1);
    \draw[red] (v2_1) -- (v2_2);

    \path ($(v3_1)!-.1cm!(v3_2)$) coordinate(v3_1) ($(v3_2)!.5cm!(v3_1)$) coordinate(v3_2);
    \getAngle{v3_1}{v3_2}
    %Rotated rectangle with gradient
    \begin{scope}[transform canvas={rotate around={\angle:(v3_1)}}]
        %Rotate the canvas to simplify gradient drawing
        % Inverse transform the global coordinate defined outside the rotate scope
        \path ([rotate around={-\angle:(v3_1)}] v3_2) coordinate(v3_2_in);
        \path (v3_2_in) -- ++(90:.7) coordinate(v3_3);
        % Fill a rectangle (with one side over the desired line) with the desired color, then fade the opposite side
        \fill[opacity=.5,darkgreen,path fading=north] (v3_3) rectangle (v3_1);
    \end{scope}
    \draw[darkgreen,name path=v3] (v3_1) -- (v3_2);

    \draw[->,name path=ax1] ++(-.2,0) -- ++(6.5,0) node[right]{$x_1$};
    \draw[->] (origin) ++(0,-.2) -- ++(0,5.5) node[above]{$x_2$};

    \path[name intersections={of=v3 and ax1,by=ax1_6}];
    \path (origin |- v2_2) node[left]{5} (origin -| v1_2) node[below]{4} (ax1_6) node[below]{6};
\end{tikzpicture}

enter image description here

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.