在 TikZ 中扩展锚点

在 TikZ 中扩展锚点

以下是 TikZ 中可用的标准锚点(据我所知):

在此处输入图片描述

\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{positioning}

\tikzset{dot/.style = {
    shape  = circle,
    draw   = black,
    fill = black,
    minimum size = 0.2cm
}}

\tikzset{squarenode/.style = {
        shape  = rectangle,
        draw   = black,
        minimum height = 10cm,
        minimum width  = 10cm
}}

\begin{document}
\begin{tikzpicture}[node distance=2cm]
  \node (a) at (0,0) [squarenode] {};
  \node[label=a.center] at (a.center) [dot] {};
  \node[label=a.north] at (a.north) [dot] {};
  \node[label=a.south] at (a.south) [dot] {};
  \node[label=a.east] at (a.east) [dot] {};
  \node[label=a.west] at (a.west) [dot] {};
  \node[label=a.north east] at (a.north east) [dot] {};
  \node[label=a.north west] at (a.north west) [dot] {};
  \node[label=a.south east] at (a.south east) [dot] {};
  \node[label=a.south west] at (a.south west) [dot] {};
\end{tikzpicture}
\end{document} 

现在,我如何找到以下几点:

  1. a.south 和 a.south west 之间的点(中点和法线点 - 线上的任意点)
  2. 西南与中心之间的点(中点和法线点 - 线上的任意点)

答案1

你可以使用 来获取两个节点之间线上的点coordinate[pos=x],其中x是分数。所以

\path (a.south west) -- (a.south) coordinate[pos=0.5] (a-mid-sw);

(a.south west)将给出位于和之间的中间点(a.south)。如果选择x较小/较大,它将更接近 (a.south west)/ (a-mid-sw),而对于x=0或 ,x=1它将分别与(a.south west)或重合(a.south)。请注意x可以为负数或大于1,在这种情况下,坐标将位于连接点的线之外。

\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{positioning}

\tikzset{dot/.style = {
    shape  = circle,
    draw   = black,
    fill = black,
    minimum size = 0.2cm
}}

\tikzset{squarenode/.style = {
        shape  = rectangle,
        draw   = black,
        minimum height = 10cm,
        minimum width  = 10cm
}}

\begin{document}
\begin{tikzpicture}[node distance=2cm]
  \node (a) at (0,0) [squarenode] {};
  \node[label=a.center] at (a.center) [dot] {};
  \node[label=a.north] at (a.north) [dot] {};
  \node[label=a.south] at (a.south) [dot] {};
  \node[label=a.east] at (a.east) [dot] {};
  \node[label=a.west] at (a.west) [dot] {};
  \node[label=a.north east] at (a.north east) [dot] {};
  \node[label=a.north west] at (a.north west) [dot] {};
  \node[label=a.south east] at (a.south east) [dot] {};
  \node[label=a.south west] at (a.south west) [dot] {};
  \path (a.south west) -- (a.south) coordinate[pos=0.5] (a-mid-sw)
(a.south west) -- (a.center) coordinate[pos=0.3] (a-diag);
  \node[label=a-mid-sw] at (a-mid-sw) [dot] {};
  \node[label=a-diag] at (a-diag) [dot] {};
\end{tikzpicture}
\end{document} 

在此处输入图片描述

calc库还允许您混合坐标。您可以使用 来获取边界上的点a.angle,其中角度必须以度为单位指定。a.90例如, 与 相同a.northa.0与 相同,a.east但一般来说a.45a.north east不必重合。(这里它们重合是因为形状是正方形。)如果您想要有额外的锚点,您可以定义一个新的形状。

答案2

另一种使用库的方法calc(节点标签受到以下启发:marmot 的回答

\documentclass[tikz]{standalone}
\usetikzlibrary{positioning,calc}

\tikzset{dot/.style = {
    shape  = circle,
    draw   = black,
    fill = black,
    minimum size = 0.2cm
}}

\tikzset{squarenode/.style = {
        shape  = rectangle,
        draw   = black,
        minimum height = 10cm,
        minimum width  = 10cm
}}

\begin{document}
\begin{tikzpicture}[node distance=2cm]
  \node (a) at (0,0) [squarenode] {};
  \node[label=a.center] at (a.center) [dot] {};
  \node[label=a.north] at (a.north) [dot] {};
  \node[label=a.south] at (a.south) [dot] {};
  \node[label=a.east] at (a.east) [dot] {};
  \node[label=a.west] at (a.west) [dot] {};
  \node[label=a.north east] at (a.north east) [dot] {};
  \node[label=a.north west] at (a.north west) [dot] {};
  \node[label=a.south east] at (a.south east) [dot] {};
  \node[label=a.south west] at (a.south west) [dot] {};
  \node[label=a-mid-sw] at ($(a.south west)!.5!(a.south)$) [dot] {};
  \node[label=a-diag] at ($(a.south west)!.3!(a.center)$) [dot] {};
\end{tikzpicture}
\end{document} 

在此处输入图片描述

如果你经常使用它,我建议使用宏

\documentclass[tikz]{standalone}
\usetikzlibrary{positioning,calc}

\tikzset{dot/.style = {
    shape  = circle,
    draw   = black,
    fill = black,
    minimum size = 0.2cm
}}

\tikzset{squarenode/.style = {
        shape  = rectangle,
        draw   = black,
        minimum height = 10cm,
        minimum width  = 10cm
}}
\def\findmid#1#2#3{($(#2)!#1!(#3)$)}
\begin{document}
\begin{tikzpicture}[node distance=2cm]
  \node (a) at (0,0) [squarenode] {};
  \node[label=a.center] at (a.center) [dot] {};
  \node[label=a.north] at (a.north) [dot] {};
  \node[label=a.south] at (a.south) [dot] {};
  \node[label=a.east] at (a.east) [dot] {};
  \node[label=a.west] at (a.west) [dot] {};
  \node[label=a.north east] at (a.north east) [dot] {};
  \node[label=a.north west] at (a.north west) [dot] {};
  \node[label=a.south east] at (a.south east) [dot] {};
  \node[label=a.south west] at (a.south west) [dot] {};
  \node[label=a-mid-sw] at \findmid{0.5}{a.south west}{a.south} [dot] {};
  \node[label=a-diag] at \findmid{0.3}{a.south west}{a.center} [dot] {};
\end{tikzpicture}
\end{document} 

(与上面输出相同)

相关内容