我需要使用该tikz
包绘制一条在圆心处相交的虚线。我想在圆心处获得一个完美的十字,所以我想知道是否有一种方法可以强制虚线在中心相交形成一个完美的十字。
\documentclass[tikz]{standalone}% 'crop' is the default
\begin{document}
\begin{tikzpicture}
\draw [dashed, line width=1.5pt] (0,5) -- (10,5);
\draw [dashed, line width=1.5pt] (5,0) -- (5,10);
\coordinate (c) at (5,5); % center of circle
\def\radius{1} % radius of circle
\def\nbpts{36} % nb of points
\def\radpt{1.0pt} % radius of points
\colorlet{point color}{red} % color of points
\foreach \numpt in {1,...,\nbpts}{\fill[point color] (c) ++ (360/\nbpts*\numpt:\radius) circle(\radpt);}
\end{tikzpicture}
\end{document}
答案1
为了得到中间的十字,我们需要拨一个适当的dash pattern
。我们希望实现有2n+1
虚线和2n
间隙。将间隙和虚线长度之间的比率称为r
。然后虚线的长度a
必须满足
2n r a + (2n+1) a = l ,
其中l
是路径的长度。所以
a = l/(2n r + 2n +1) .
在你的例子中,l
是10cm
(但我们可以让 Ti钾Z 测量带有装饰的(如果需要)。以下是和的\pgfdecoratedpathlength
示例,您可以进行调整。n=15
r=0.8
\documentclass[tikz]{standalone}% 'crop' is the default
\begin{document}
\begin{tikzpicture}
\pgfmathsetmacro{\myr}{0.8}
\pgfmathsetmacro{\myn}{15}
\pgfmathsetmacro{\myl}{10cm}
\pgfmathsetmacro{\myon}{\myl/(2*\myn*\myr+2*\myn+1)}
\pgfmathsetmacro{\myoff}{\myr*\myon}
\typeout{\myon,\myoff}
\draw [line width=1.5pt,dash pattern=on \myon pt off \myoff pt] (0,5) -- (10,5);
\draw [line width=1.5pt,dash pattern=on \myon pt off \myoff pt] (5,0) -- (5,10);
\coordinate (c) at (5,5); % center of circle
\def\radius{1} % radius of circle
\def\nbpts{36} % nb of points
\def\radpt{1.0pt} % radius of points
\colorlet{point color}{red} % color of points
\foreach \numpt in {1,...,\nbpts}{\fill[point color] (c) ++ (360/\nbpts*\numpt:\radius) circle(\radpt);}
\end{tikzpicture}
\end{document}
您可以定义某种完整的破折号,其概念类似于杰克的完整正弦. 然后你可以说类似
\draw [line width=1.5pt,complete dashes={a=6mm}] (0,5) -- (10,5);
其中a
是目标虚线长度。此外,您还可以指定上述比例r
。但请注意,如果您在不同长度的路径上使用它,虚线长度只会在极少数情况下完全一致,但只要有许多虚线,它看起来就会大致相同。
\documentclass[tikz]{standalone}% 'crop' is the default
\usetikzlibrary{decorations.markings}
% earlier posts this post is conceptually similar to include
% https://tex.stackexchange.com/a/214448
% https://tex.stackexchange.com/a/25689
\makeatletter
\tikzset{%
complete dashes/.style={/tikz/bob/settings={#1},
decoration={
markings,
mark=at position 1 with {%
\pgfmathtruncatemacro{\myn}{0.5+2*(\pgfdecoratedpathlength-\pgfkeysvalueof{/tikz/bob/a})/
(2*\pgfkeysvalueof{/tikz/bob/a}*(1+\pgfkeysvalueof{/tikz/bob/r}))}%
\pgfmathsetmacro{\myon}{\pgfdecoratedpathlength/(2*\myn*\pgfkeysvalueof{/tikz/bob/r}+2*\myn+1)}%
\pgfmathsetmacro{\myoff}{\pgfkeysvalueof{/tikz/bob/r}*\myon}%
\global\pgfutil@tempdima=\myon pt%
\global\pgfutil@tempdimb=\myoff pt%
%\typeout{\myon,\myoff}
},
},
preaction=decorate,
draw,dash pattern=on \pgfutil@tempdima off \pgfutil@tempdimb,
},bob/.cd,settings/.code={\tikzset{bob/.cd,#1}},
a/.initial=3mm,r/.initial=0.8
}
\makeatother
\begin{document}
\begin{tikzpicture}
\draw [line width=1.5pt,complete dashes={a=6mm}] (0,5) -- (10,5);
\draw [line width=1.5pt,complete dashes={a=6mm}] (5,1) -- (5,9);
\coordinate (c) at (5,5); % center of circle
\def\radius{1} % radius of circle
\def\nbpts{36} % nb of points
\def\radpt{1.0pt} % radius of points
\colorlet{point color}{red} % color of points
\foreach \numpt in {1,...,\nbpts}{\fill[point color] (c) ++ (360/\nbpts*\numpt:\radius) circle(\radpt);}
\end{tikzpicture}
\end{document}
顺便说一句,我想我以前见过类似的东西,但我找不到完全匹配的。