TikZ/pgf:如何改变路径的方向和路径的起点/原点/起始点

TikZ/pgf:如何改变路径的方向和路径的起点/原点/起始点

在我的 MWE 中,我有一条简单的路径(圆圈),我使用tikz postaction相同的路径覆盖该路径,但使用另一条线和另一种颜色。

参考(感谢@MarkWibrow) 中的一些棘手的代码片段在代码中。但我不认为这与这个问题有关。

覆盖3点开始然后从那里开始逆时针

问题)

  1. 我怎么能够影响方向顺时针或者逆时针
  2. 我怎么能够影响起点/原点/开始路径(这里显然是 3 点钟)?

代码

\documentclass[tikz,border=2mm]{standalone}

\usepackage{tikz}
\usetikzlibrary{decorations}

% taken from https://tex.stackexchange.com/questions/247742
% user https://tex.stackexchange.com/users/23215/mark-wibrow
\makeatletter
\tikzset{%
  get path length/.code={%
    \tikz@addoption{%
      \pgfgetpath\tikz@tmppath%
      \pgfprocessround\tikz@tmppath\tikz@tmppath%
      \pgf@decorate@parsesoftpath\tikz@tmppath\tikz@discard%
      \global\let#1=\pgf@decorate@totalpathlength%
    }%
  }
}
\makeatother

% taken from https://tex.stackexchange.com/questions/347336
% user https://tex.stackexchange.com/users/23215/mark-wibrow
\tikzset{myOwnStyle/.style={
  get path length=\pathlength,
  draw=black,
  line width=1mm,
  postaction={
    draw=red,
    line width=2mm,
    dash pattern= on \pathlength/100*#1 off \pathlength/100*(100-#1),
  }
}}


\begin{document}

\foreach \n in {0,...,100}{
\begin{tikzpicture}
    \draw[myOwnStyle=\n] (0,0) circle [radius = 10mm] node {\n\,\%};  
\end{tikzpicture} 
}

\end{document}

输出

在此处输入图片描述

如果有人感兴趣的话,我通过两个步骤制作了动画 GIF:

  1. 使用以下方法将多页 PDF 转换为单独的 GIF 文件服务。
  2. 使用以下方法将单独的 GIF 转换为动画 GIF或者(服务更快)。

我只是在谷歌上搜索了这类服务——我没有做进一步的研究。

更新/编辑

在得到非常有帮助的答案之后@Torbjørn T.很明显,圆形不是一个明智的选择:) - 这是一个正方形。

\foreach \n in {0,1,...,100}{
\begin{tikzpicture}[x=1.5mm,y=1.5mm]]
    \draw[myOwnStyle=\n] (0,0) -- (10,0) -- (10,10) -- (0,10) -- cycle;
    \node at (5,5) {\n\,\%};  
\end{tikzpicture} 
}

在此处输入图片描述

答案1

这是一个使用dash phase(由\myOwnStylephase宏存储)来定义起点并使用第二个自定义样式(myOwnStyle revert)来改变方向的解决方案。

在此处输入图片描述

\documentclass[tikz]{standalone}

\usepackage{tikz}
\usetikzlibrary{decorations,fit,backgrounds}

% taken from http://tex.stackexchange.com/questions/247742
% user http://tex.stackexchange.com/users/23215/mark-wibrow
\makeatletter
\tikzset{%
  get path length/.code={%
    \tikz@addoption{%
      \pgfgetpath\tikz@tmppath%
      \pgfprocessround\tikz@tmppath\tikz@tmppath%
      \pgf@decorate@parsesoftpath\tikz@tmppath\tikz@discard%
      \global\let#1=\pgf@decorate@totalpathlength%
    }%
  }
}
\makeatother

% taken from http://tex.stackexchange.com/questions/347336
% user http://tex.stackexchange.com/users/23215/mark-wibrow
\tikzset{
  myOwnStyle phase/.store in=\myOwnStylephase,
  myOwnStyle phase=0,
  myOwnStyle/.style={
    get path length=\pathlength,
    draw=black,
    line width=1mm,
    postaction={
      draw=red,
      line width=2mm,
      dash pattern= on \pathlength/100*#1 off \pathlength/100*(100-#1),
      dash phase=-\pathlength/100*\myOwnStylephase,
    }
  },
  myOwnStyle revert/.style={
    get path length=\pathlength,
    draw=black,
    line width=1mm,
    postaction={
      draw=red,
      line width=2mm,
      dash pattern=on 0pt off \pathlength/100*(100-#1) on \pathlength/100*#1 off 0pt,
      dash phase=\pathlength/100*\myOwnStylephase,
    }
  },
}


\begin{document}
\foreach \n in {0,0,0,0,4,...,100,100,100,100}{
  \begin{tikzpicture}%[line cap=round]
    \draw[myOwnStyle=\n]
    (0,0) ++(90:10mm) arc(90:-270:10mm) (0,0) node {\n\,\%};

    \draw[myOwnStyle=\n,myOwnStyle phase=25]
    (3,0) ++(90:10mm) arc(90:-270:10mm) (3,0) node {\n\,\%};

    \draw[myOwnStyle revert=\n]
    (0,-3) ++(90:10mm) arc(90:-270:10mm) (0,-3) node {\n\,\%};

    \draw[myOwnStyle revert=\n,myOwnStyle phase=25]
    (3,-3) ++(90:10mm) arc(90:-270:10mm) (3,-3) node {\n\,\%};

    \draw[myOwnStyle=\n]
    (-1,-7) -- ++(2,0) -- ++(0,2) -- ++(-2,0) -- cycle (0,-6) node {\n\,\%};

    \draw[myOwnStyle=\n,myOwnStyle phase=12.5]
    (2,-7) -- ++(2,0) -- ++(0,2) -- ++(-2,0) -- cycle (3,-6) node {\n\,\%};

    \begin{pgfonlayer}{background}
      \node[fit=(current bounding box),inner sep=2mm,fill=white]{};
    \end{pgfonlayer}
  \end{tikzpicture} 
}
\end{document}

答案2

对于圆形的具体情况,您可以用 翻转 来xscale=-1改变方向,用 旋转 来改变起点,例如\draw[myOwnStyle=\n,rotate=30,xscale=-1]。当然,这不适用于各种形状。

\documentclass[tikz,border=2mm]{standalone}

\usepackage{tikz}
\usetikzlibrary{decorations}

% taken from http://tex.stackexchange.com/questions/247742
% user http://tex.stackexchange.com/users/23215/mark-wibrow
\makeatletter
\tikzset{%
  get path length/.code={%
    \tikz@addoption{%
      \pgfgetpath\tikz@tmppath%
      \pgfprocessround\tikz@tmppath\tikz@tmppath%
      \pgf@decorate@parsesoftpath\tikz@tmppath\tikz@discard%
      \global\let#1=\pgf@decorate@totalpathlength%
    }%
  }
}
\makeatother

% taken from http://tex.stackexchange.com/questions/347336
% user http://tex.stackexchange.com/users/23215/mark-wibrow
\tikzset{myOwnStyle/.style={
  get path length=\pathlength,
  draw=black,
  line width=1mm,
  postaction={
    draw=red,
    line width=2mm,
    dash pattern= on \pathlength/100*#1 off \pathlength/100*(100-#1),
  }
}}


\begin{document}

\foreach \n in {0,...,100}{
\begin{tikzpicture}
    \draw[myOwnStyle=\n,rotate=30,xscale=-1] (0,0) circle [radius = 10mm] node {\n\,\%};  
\end{tikzpicture} 
}

\end{document}

相关内容