使用 TikZ 交叉口生成的线太短

使用 TikZ 交叉口生成的线太短

我的脚本计算了多条线(绿色)与垂直“墙”(未显示)的交点。这些交点(小红点)没有问题。但是从一个交点到另一个交点的线太短了(蓝色)。我不知道脚本出了什么问题:

\documentclass[a4paper]{scrartcl}
\usepackage{pgf}
\usepackage{tikz}
\usetikzlibrary{intersections,calc}


\begin{document}
    \begin{tikzpicture}
        \def\wallx{6cm}
        \def\scannerx{0cm}
        \def\scannery{0.0cm}
        \def\scannerradius{0.4cm}
        \def\walllength{8cm}

        \coordinate (WALLBEGIN) at ($(\wallx, 0.5*\walllength)$);
        \coordinate (WALLEND) at ($(\wallx, -0.5*\walllength)$);
        \coordinate (SCANNER) at (\scannerx, \scannery);
        \path[name path=wall] (WALLBEGIN) -- (WALLEND);

        %calculate intersections
        \foreach \index in {0,...,4} {
            %calculates the ray from scanner to wall
            \def\raylength{12cm}
            \coordinate (REL) at ({9*(\index-2)+1}:\raylength);
            \coordinate (RAYEND) at ($(SCANNER)+(REL)$);
            \path [name path=ray] (SCANNER) -- (RAYEND);

            %intersection with wall
            \path [name intersections={of=ray and wall,by=X}];
            \node (iwall\index) at (X) {};

            %THIS INTERSECTION IS OK
            \filldraw[fill=red] (X) circle (0.05cm);
            \draw[green] (SCANNER) -- (RAYEND);
        }


        % LINE BETWEEN TWO INTERSECTIONS WHICH IS TOO SHORT
        \draw[blue] (iwall0) -- (iwall1);

        \filldraw[fill=gray!50!white] (SCANNER) circle (\scannerradius);
    \end{tikzpicture}
\end{document}

答案1

您无需添加nodes 来保存交叉点坐标,只需使用 即可。然后交叉点将以、等\path [name intersections={of=ray and wall,by=X\index}];形式提供。X0X1

然后,您可以用带有inner sep=0pt:的圆形节点“覆盖”这些坐标inner sep,空节点的出现导致了“分离”线的问题。它们实际上并没有分离,而是与空节点的不可见边界相连。

一般来说,node如果您只是想保存一个点,那么空的 并不是最好的选择,因为即使是空的node也会占用空间(由 引起inner sep,您可以将其设置为零作为解决方法)。\coordinate(或者,等效地,\node [coordinate]更适合这项任务。

\documentclass[a4paper]{article}
\usepackage{tikz}
\usetikzlibrary{intersections,calc}


\begin{document}
    \begin{tikzpicture}
        \def\wallx{6cm}
        \def\scannerx{0cm}
        \def\scannery{0.0cm}
        \def\scannerradius{0.4cm}
        \def\walllength{8cm}

        \coordinate (WALLBEGIN) at ($(\wallx, 0.5*\walllength)$);
        \coordinate (WALLEND) at ($(\wallx, -0.5*\walllength)$);
        \coordinate (SCANNER) at (\scannerx, \scannery);
        \path[name path=wall] (WALLBEGIN) -- (WALLEND);

        %calculate intersections
        \foreach \index in {0,...,4} {
            %calculates the ray from scanner to wall
            \def\raylength{12cm}
            \coordinate (REL) at ({9*(\index-2)+1}:\raylength);
            \coordinate (RAYEND) at ($(SCANNER)+(REL)$);
            \path [name path=ray] (SCANNER) -- (RAYEND);

            %intersection with wall
            \path [name intersections={of=ray and wall,by=X\index}];

            % use a circular node for drawing the circles
            \node [draw,fill=red,circle, inner sep=0pt, minimum size=4pt] (X\index) at (X\index) {};
            \draw[green] (SCANNER) -- (RAYEND);
        }


        \draw[blue] (X1) -- (X2);

        \filldraw[fill=gray!50!white] (SCANNER) circle (\scannerradius);
    \end{tikzpicture}
\end{document}

相关内容