Skip to content

Comment dessiner une flèche avec deux couleurs ?

Ce poste a été évalué par nos spécialistes afin que vous ayez la garantie de l'exactitude de notre division.

Solution :

Hmm, donc je suis le seul à avoir pensé que l'exigence était d'ombrer une flèche sur un chemin. Ah bon...

Les décorations peuvent être utilisées, mais sont lentes. Ce qui suit est loin d'être parfait, mais est fondamentalement utilisable. Dans les deux premières versions les transformations comme la mise à l'échelle et la rotation vont tout ruiner, ceci est (en grande partie) corrigé dans la troisième version.

L'approche consiste à créer un fondu à partir du chemin requis, ce qui en principe est facile, mais pour réduire la quantité de frappe impliquée, j'ai essayé de l'automatiser en utilisant une touche. Il faut faire quelques folies pour éviter que les pointes de flèches ne soient coupées.

documentclass[border=0.125cm]{standalone}
usepackage{tikz}
usetikzlibrary{fadings,decorations.pathmorphing}

makeatletter
newif[email protected]@path
tikzset{
    % Need this for the bounding box calculation of the
    % fading and the rectangle that bounds the fading.
    % If set to zero things (e.g., arrows) may get cut off.
    fading sep/.store in=fadingsep,
    fading sep=0.25cm,
    shaded path/.code={%
        % Prevent this stuff happning recursively.
        [email protected]@path%
        else%
            [email protected]@pathtrue%
            [email protected]{%
                % Interrupt the picture to create a fading.
                pgfinterruptpicture%
                    begin{tikzfadingfrompicture}[name=.]
                        path [shade=none,fill=none]#1;%
                        % Need to set the bounding box manually. Include the fadingsep border.
                        xdeffadingboundingbox{{noexpandpgfpoint{the[email protected]fadingsep}{the[email protected]fadingsep}}%
                            {noexpandpgfpoint{the[email protected]+fadingsep}{the[email protected]+fadingsep}}}%
                        expandafterpgfpathrectanglecornersfadingboundingbox%
                        pgfusepath{discard}%
                    end{tikzfadingfrompicture}%
                endpgfinterruptpicture%
                % Install a rectangle that covers the shaded/faded path.
                expandafterpgfpathrectanglecornersfadingboundingbox%
                % Make the fading happen.
                def[email protected]@fading{.}%
                [email protected]@[email protected]%
                [email protected]@adjustfalse%
                pgfpointscale{0.5}{expandafterpgfpointaddfadingboundingbox}%
                def[email protected]@transform{shift={(the[email protected],the[email protected])}}%
            }%
        fi%
    }
}
begin{document}

begin{tikzpicture}[line cap=round, line join=round]

path[top color=blue, bottom color=green, line cap=rect, fading sep=1cm, 
    shaded path={
    [draw=transparent!0, line width=1cm, -stealth] (-1.5,3) -- (1.5,3)
}];

path [left color=red, right color=blue, shaded path={ 
    [draw=transparent!0, ultra thick, -stealth] (2,-1.5) .. controls ++(0,2) and ++(0,-2) .. (-2,1.5)
}];

path [top color=green, bottom color=yellow,
    shaded path={ 
        [draw=transparent!25,   ultra thick, dashed,    stealth-stealth] (-2,-1.5) -- (2,1.5)
    }];

path [left color=orange, right color=purple,
    shaded path={
        [draw=transparent!0, very thick, loosely dotted, stealth-stealth] (240:2 and 1.5) arc (240:-60:2 and 1.5)
    }];

path [left color=black!5, right color=black,
    shaded path={
        [draw=transparent!20, very thick, <->, 
            decoration={zigzag, segment length=0.125cm, amplitude=0.0625cm, pre length=0.25cm, post length=0.25cm}, 
        decorate] (-3, 0) -- (3,0)
    }];

end{tikzpicture}

end{document}

enter image description here

OK, voici donc une version dans laquelle le chemin peut être spécifié de la manière habituelle. Un peu plus d'imbécillité doit être faite pour obtenir les boîtes de délimitation correctes. Le résultat est le même que ci-dessus.

documentclass[border=0.125cm]{standalone}
usepackage{tikz}
usetikzlibrary{fadings,decorations.pathmorphing,arrows.meta}

makeatletter
newif[email protected]@path

tikzset{
    % There are three circumstances in which the fading sep is needed:
    % 1. Arrows which do not update the bounding box (which is most of them).
    % 2. Line caps/joins and mitres that extend outside the natural bounding 
    %    box of the path (these are not calculated by PGF).
    % 3. Other reasons that haven't been anticipated.
    fading xsep/.store in=pgfpathfadingxsep,
    fading ysep/.store in=pgfpathfadingysep,
    fading sep/.style={fading xsep=#1, fading ysep=#1},
    fading sep=0.0cm,
    shading path/.code={%
        % Prevent this stuff happning recursively.
        [email protected]@path%
        else%
            [email protected]@pathtrue%
            % [email protected] installs the `modes' (e.g., fill, draw, shade) 
            % to be applied to the path. It isn't usualy for doing more
            % changes to the path's construction.
            [email protected]{%
                pgfgetpath[email protected]%
                % Get the boudning box of the current path size including the fading sep
                [email protected][email protected]@southwest{pgfpointadd{pgfqpoint{[email protected]}{[email protected]}}%
                    {pgfpoint{-pgfpathfadingxsep}{-pgfpathfadingysep}}}%%
                [email protected][email protected]@northeast{pgfpointadd{pgfqpoint{[email protected]}{[email protected]}}%
                    {pgfpoint{pgfpathfadingxsep}{pgfpathfadingysep}}}%
                % Clear the path
                pgfsetpath[email protected]%                          
                % Interrupt the path and picture to create a fading.
                pgfinterruptpath%
                pgfinterruptpicture%
                    begin{tikzfadingfrompicture}[name=.]
                        path [shade=none,fill=none, #1] pgfextra{%
                            % Set the softpath. Any transformations in #1 will have no effect.
                            % This will *not* update the bounding box...
                            pgfsetpath[email protected]%
                            % ...so it is done manually.
                            [email protected]@southwest
                            expandafter[email protected]{the[email protected]}{the[email protected]}%
                            [email protected]@northeast%
                            expandafter[email protected]{the[email protected]}{the[email protected]}%
                        };
                        % Now get the bounding of the picture.
                        xdef[email protected]@southwest{noexpandpgfqpoint{the[email protected]}{the[email protected]}}%
                        xdef[email protected]@northeast{noexpandpgfqpoint{the[email protected]}{the[email protected]}}%
                        %
                    end{tikzfadingfrompicture}%
                endpgfinterruptpicture%
                endpgfinterruptpath%
                % Install a rectangle that covers the shaded/faded path picture.                                
                pgfpathrectanglecorners{[email protected]@southwest}{[email protected]@northeast}%
                % Make the fading happen.
                def[email protected]@fading{.}%
                [email protected]@[email protected]%
                [email protected]@adjustfalse%10pt
                % Shift the fading to the mid point of the rectangle
                pgfpointscale{0.5}{pgfpointadd{[email protected]@southwest}{[email protected]@northeast}}%
                edef[email protected]@transform{shift={(the[email protected],the[email protected])}}%
            }%
        fi%
    }
}

begin{document}

begin{tikzpicture}[line cap=round, line join=round]

path[top color=blue, bottom color=green, line cap=rect, fading ysep=.75cm, 
    shading path={draw=transparent!0, line width=1cm, -stealth}] 
        (-1.5cm,3cm) -- (1.5cm, 3cm);

path [left color=red, right color=blue, shading path={draw=transparent!0, ultra thick, Stealth-Stealth}]
    (2,-1.5) .. controls ++(0,2) and ++(0,-2) .. (-2,1.5);

path [top color=green, bottom color=yellow,
    shading path={draw=transparent!25,  ultra thick, dashed,    stealth-stealth}]
     (-2,-1.5) -- (2,1.5);

path [left color=orange, right color=purple,
    shading path={draw=transparent!0, very thick, loosely dotted, stealth-stealth}]
     (240:2 and 1.5) arc (240:-60:2 and 1.5);

path [left color=black!5, right color=black,
    shading path={draw=transparent!20, very thick, <->, 
            decoration={zigzag, segment length=0.125cm, amplitude=0.0625cm, pre length=0.25cm, post length=0.25cm}, 
        decorate}]
        (-3, 0) -- (3,0);

end{tikzpicture}

end{document}

Donc, la version 3. Celle-ci permet de tout spécifier de la manière "habituelle" et le... shade path trie tout le reste. Contrairement aux versions précédentes, cette version devrait s'occuper des transformations comme la rotation, le décalage, la mise à l'échelle et ainsi de suite. Comme les versions précédentes cependant, il s'agit essentiellement d'un hack colossal : toutes sortes de codages tortueux sont utilisés pour faire fonctionner les choses, et peuvent se casser dans certaines circonstances. Le résultat est le même que celui de la première image.

Notez aussi, qu'il est possible de dire draw=white plutôt que draw=transparent!0.

documentclass[border=0.125cm]{standalone}
usepackage{tikz}
usetikzlibrary{fadings,decorations.pathmorphing}

makeatletter
newif[email protected]@path

tikzset{
    % There are three circumstances in which the fading sep is needed:
    % 1. Arrows which do not update the bounding box (which is most of them).
    % 2. Line caps/joins and mitres that extend outside the natural bounding 
    %    box of the path (these are not calculated by PGF).
    % 3. Other reasons that haven't been anticipated.
    shading xsep/.store in=[email protected],
    shading ysep/.store in=[email protected],
    shading sep/.style={shading xsep=#1, shading ysep=#1},
    shading sep=0.0cm,
}

def[email protected]#1{% 
    % [email protected] installs the `modes' (e.g., fill, draw, shade) 
    % to be applied to the path. It isn't usualy for doing more
    % changes to the path's construction.
    [email protected]@path%
    else%
        [email protected]@pathtrue%
        % Get the current path.
        pgfgetpath[email protected]%
        % Get the shading sep without setting any other keys.
        begingroup%
            [email protected]% <- may not be necessary
            tikzset{#1}%
            xdef[email protected]{noexpanddefnoexpand[email protected]{[email protected]}%
                noexpanddefnoexpand[email protected]{[email protected]}}%
            [email protected]%
        endgroup
        [email protected]%
        % Get the boudning box of the current path size including the shading sep
        [email protected][email protected]@southwest{pgfpointadd{pgfqpoint{[email protected]}{[email protected]}}%
            {pgfpoint{-[email protected]}{-[email protected]}}}%%
        [email protected][email protected]@northeast{pgfpointadd{pgfqpoint{[email protected]}{[email protected]}}%
            {pgfpoint{[email protected]}{[email protected]}}}%
        % Clear the path
        pgfsetpath[email protected]%                          
        % Save the current drawing mode and options.
        let[email protected]@saved=[email protected]%
        let[email protected]@saved=[email protected]%
        let[email protected]=[email protected]%
        let[email protected]=[email protected]%
        % [email protected] are processed later on.
        [email protected]{%
            pgfinterruptpath%
            pgfinterruptpicture%
                begin{tikzfadingfrompicture}[name=.]
                pgfscope%
                    tikzset{shade path/.style=}% Make absolutely sure shade path is not inherited.
                    path pgfextra{%
                        % Set the softpath. Any transformations,draw=none} in #1 will have no effect.
                        % This will *not* update the bounding box...
                        pgfsetpath[email protected]%
                        % ...so it is done manually.
                        [email protected]@southwest
                        expandafter[email protected]{the[email protected]}{the[email protected]}%
                        [email protected]@northeast%
                        expandafter[email protected]{the[email protected]}{the[email protected]}%
                        % Install the drawing modes and options.
                        let[email protected]=[email protected]@saved%
                        let[email protected]=[email protected]@saved%
                    };
                    % Now get the bounding box of the picture.
                    xdef[email protected]@southwest{noexpandpgfqpoint{the[email protected]}{the[email protected]}}%
                    xdef[email protected]@northeast{noexpandpgfqpoint{the[email protected]}{the[email protected]}}%
                    endpgfscope
                end{tikzfadingfrompicture}%
            endpgfinterruptpicture%
            endpgfinterruptpath%
            % Install a rectangle that covers the shaded/faded path picture.
            pgftransformreset%
            pgfpathrectanglecorners{[email protected]@southwest}{[email protected]@northeast}%
            %
            % Reset all modes.
            let[email protected]@picture=[email protected]%
            [email protected]@fillfalse%
            [email protected]@drawfalse%
            %[email protected]@tipsfalse%   <- To have successful compilation with pgf-tikz v3.0.1a
            [email protected]@doublefalse%
            [email protected]@clipfalse%
            [email protected]@boundaryfalse%
            [email protected]@[email protected]%
            [email protected]@[email protected]%
            % Now install shading options.
            tikzset{#1}%
            [email protected]%
            % Make the fading happen.
            def[email protected]@fading{.}%
            [email protected]@[email protected]%
            [email protected]@adjustfalse%
            % Shift the fading to the mid point of the rectangle
            pgfpointscale{0.5}{pgfpointadd{[email protected]@southwest}{[email protected]@northeast}}%
            edef[email protected]@transform{shift={(the[email protected],the[email protected])}}%
            pgfsetfading{[email protected]@fading}{[email protected]@[email protected]}%
            [email protected]@[email protected]%              
        }%
    fi%
}
tikzset{
    shade path/.code={%
        [email protected]{[email protected]{#1}}%
    }
}
makeatother % <- To close the makeatletter call

begin{document}

begin{tikzpicture}[line cap=round, line join=round]

path[draw=transparent!0, line width=1cm, line cap=butt, -stealth, 
    shade path={shading ysep=0.75cm, top color=blue, bottom color=green}] 
        (-1.5,3) -- (1.5, 3);

draw [draw=transparent!0, shade path={shading xsep=0.125cm, left color=red, right color=blue}, 
    ultra thick, -stealth]
    (2,-1.5) .. controls ++(0,2) and ++(0,-2) .. (-2,1.5);

path [rotate=90, scale=0.5, draw=transparent!25,   ultra thick, dashed, stealth-stealth, 
    shade path={top color=green, bottom color=yellow}]
     (-1.5,-2) -- (1.5,2);

path [draw=transparent!0, very thick, loosely dotted, stealth-stealth, 
    shade path={left color=orange, right color=purple}]
     (240:2 and 1.5) arc (240:-60:2 and 1.5);

path [shade path={left color=black!5, right color=black}, draw=transparent!20, very thick, <->, 
            decoration={zigzag, segment length=0.125cm, amplitude=0.0625cm, pre length=0.25cm, post length=0.25cm}, 
        decorate,]
        (-3, 0) -- (3,0);

end{tikzpicture}

end{document}

Voici une solution utilisant single arrow (à partir de la forme shapes.arrows bibliothèque).

documentclass{standalone}
usepackage{tikz}
usetikzlibrary{shapes.arrows}
begin{document}
begin{tikzpicture}
  node[single arrow,top color=blue,bottom color=green]{example};
end{tikzpicture}
end{document}

enter image description here

Avec Tikz (sans pgflibrary), cette alternative donne

enter image description here

où toute forme peut être dessinée via des coordonnées (Flèche est un exemple ici).

documentclass[]{article}
usepackage{tikz}
begin{document}
begin{tikzpicture}
filldraw[top color=blue, bottom color=green] 
(0,-0.5)-- (1,-0.5)--(1,-1)--(2,0)--(1,1)--(1,0.5)--(0,0.5)--cycle;
end{tikzpicture}
end{document}

Notes et commentaires

Vous pouvez soutenir notre travail en laissant un commentaire ou en laissant une appréciation, nous vous en remercions.



Utilisez notre moteur de recherche

Ricerca
Generic filters

Laisser un commentaire

Votre adresse e-mail ne sera pas publiée.