5

I need to draw to dashed line which cross at the center of a circle using the tikz package. I would like to obtain a pefect cross at the center of the circle so I wonder if there is a method to force the dashed lines to intersect at the center forming a peferct cross.

\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}

enter image description here

1 Answer 1

9

To get a cross in the middle, we need to dial an appropriate dash pattern. We want to achieve that there are 2n+1 dashes and 2n gaps. Call the ratio between gap and dash lengths r. Then the length a of the dash has to fulfill

2n r a + (2n+1) a = l ,

where l is the length of the path. So

a = l/(2n r + 2n +1)  .

In your example, l is 10cm (but we could let TikZ measure the with a decoration using \pgfdecoratedpathlength, if needed). Here is an example for n=15 and r=0.8, which you can adjust.

\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}

enter image description here

You can define some sort of complete dashes, which are conceptually similar to Jake's complete sines. Then you can say something like

\draw [line width=1.5pt,complete dashes={a=6mm}] (0,5) -- (10,5);

where a is the target dash length. In addition, you can specify the above-mentioned ratio r. Note, however, that if you use this on paths of different lengths, the dash length will only coincide precisely in rare cases,but it will look pretty much the same as long there are many dashes.

\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}

enter image description here

BTW, I thought I have seen something like this before but I could not find a precise match.

4
  • +1: You are working in math science, aren't you? Mar 1, 2020 at 23:44
  • 1
    @Dr.ManuelKuehner No. I am just a cat. ;-)
    – user194703
    Mar 1, 2020 at 23:45
  • Very cool method andnice explanation. I've just a further question: does this method work also if the two lines have a different length?
    – FlyBob
    Mar 1, 2020 at 23:56
  • @FlyBob Yes, you need to compute the dash pattern for each of them separately, then. One can also define a style that measures the path length and make it more automatic. I added such a style.
    – user194703
    Mar 2, 2020 at 1:39

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .