TikZ 交叉命令未返回预期结果

TikZ 交叉命令未返回预期结果

我尝试将一个节点放置在两条线的交叉点处,但是没有获得预期的结果。

我想让x下图中圈出的部分出现在两条线的交叉点上。我做错了什么?

在此处输入图片描述

母语:

\documentclass{article}
\usepackage{tikz}
\usepackage[active,tightpage]{preview}
\usetikzlibrary{intersections}
\PreviewEnvironment{tikzpicture}
\begin{document}
    \begin{tikzpicture}
        \node (l3) at (-1,-2) {test};
        \node (c1) at (intersection of (0,0)-- ++(300:7) and {l3.north--++(99,0)})[draw, circle]{x};
        \draw (0,0)-- ++(300:3) (l3.north)--++(4,0);
    \end{tikzpicture}
\end{document}

编辑解释一下为什么我需要不同的语法:我正在创建这样的图片:

金字塔

我手工绘制并调整文本节点,之后我需要将水平线绘制到三角形中。我决定懒得使用pgfmath,使用选定的答案,我可以编写以下内容来生成水平线:

\foreach \level in {nodeL1, nodeL2, ...} {      
    \coordinate (c1) at (intersection of 0,0--[shift={(300:7)}]0,0 and \level.north--[xshift=99]\level.north);
    \coordinate (c2) at (intersection of 0,0--[shift={(240:7)}]0,0 and \level.north--[xshift=99]\level.north);      
    \draw (c1) -- (c2);
};

答案1

intersection of坐标语法和坐标系统都不能intersection(直接)处理相对坐标。并且intersections它们都不需要库。

我建议事先声明所需的坐标(包装在环境中pgfinterruptboundingbox)或使用shift相对节点的选项。

语法

(intersection cs: first line={(origin)--(origin')}, second line={(l3)--(l3')})

是相同的

(intersection of origin--origin' and l3--l3')

后者被解析器转换为前者。

代码

\documentclass[tikz,convert=false]{standalone}
\tikzset{nodes={draw,circle,fill,minimum size=+1pt, inner sep=+0pt}}
\begin{document}
\begin{tikzpicture}
    \begin{pgfinterruptboundingbox}
    \path (-1,-2) coordinate (l3)
        ++(99,0)  coordinate (l3')
          (0,0)   coordinate (origin)
        ++(300:7) coordinate (origin')
    ;
    \end{pgfinterruptboundingbox}
    \node (c1) at (intersection of origin--origin' and l3--l3') {};
    \draw (0,0)-- ++(300:3) (l3)--++(4,0);
\end{tikzpicture}
\begin{tikzpicture}
    \path (-1,-2) coordinate (l3)
          (0,0)   coordinate (origin);
    \node (c1) at (intersection of origin--[shift={(300:7)}]origin and l3--[shift={(l3)}]99,0) {};
    \draw (0,0)-- ++(300:3) (l3)--++(4,0);
\end{tikzpicture}
\begin{tikzpicture}
    \path (-1,-2) coordinate (l3)
        ++(4,0)   coordinate (l3')
          (0,0)   coordinate (origin)
        ++(300:3) coordinate (origin')
    ;
    \node (c1) at (intersection of origin--origin' and l3--l3') {};
    \draw (origin)--(origin') (l3)--(l3');
\end{tikzpicture}
\begin{tikzpicture}
    \coordinate (l3) at (-1,-2);
    \node (c1) at (intersection of 0,0--300:7 and l3--[shift={(l3)}]99,0) {};
    \draw (0,0)-- ++(300:3) (l3)--++(4,0);
\end{tikzpicture}
\end{document}

输出

在此处输入图片描述

答案2

首先命名直线的两条路径,然后获取交点。环境pgfinterruptboundingbox确保用于计算的路径不会添加不必要的空间:

\documentclass{article}
\usepackage{tikz}
\usepackage[active,tightpage]{preview}
\usetikzlibrary{intersections}
\PreviewEnvironment{tikzpicture}

\begin{document}
    \begin{tikzpicture}
        \node (l3) at (-1,-2) {test};
\begin{pgfinterruptboundingbox}
        \path[name path=line1] (0,0) -- ++(300:3);
        \path[name path=line2] (l3.north) -- ++(99,0);
        \path[name intersections={of=line1 and line2, by={a}}] node (c1) at (a)[draw, circle]{x};
\end{pgfinterruptboundingbox}
        \draw (0,0)-- ++(300:3) (l3.north)--++(4,0);
    \end{tikzpicture}
\end{document}

在此处输入图片描述

答案3

你使用的语法不intersections正确。下面的代码应该有效

\documentclass{article}
\usepackage{tikz}
\usepackage[active,tightpage]{preview}
\usetikzlibrary{intersections}
\PreviewEnvironment{tikzpicture}
\begin{document}
    \begin{tikzpicture}
        \node (l3) at (-1,-2) {test};
        \draw[name path=A] (0,0)-- ++(300:3); 
        \draw[name path=B] (l3.north)--++(4,0);
        \node[name intersections={of= A and B}] (c1) at (intersection-1)[draw, circle]{x};
    \end{tikzpicture}
\end{document}

在此处输入图片描述

相关内容