如何计算锚点的相对位置?

如何计算锚点的相对位置?

我担心我误解了 tikz 在定义形状时计算锚点相对位置的一些基本知识。在下面的代码中,我期望从 (4, 2.8) -- (5, 0) 和 (4, 3.2) -- (5, 5) 绘制 2 条线,但两条线都从声明的形状的中心节点开始。我尝试直接使用已保存的锚点和锚点 - 两者都没有效果。为什么?

\documentclass[crop,tikz]{standalone}% 'crop' is the default for v1.0, before it was 'preview'
%\documentclass[crop,tikz,convert={density=300,size=800x800,outext=.png}]{standalone}

\usepackage{circuitikz}
\usetikzlibrary{positioning}
\usetikzlibrary{calc}

\usepackage[T1]{fontenc}
\usepackage[utf8]{inputenc}

\newlength{\originx}
\newlength{\originy}

\pgfdeclareshape{electric engine with connectors}
{
    \anchor{center}{\pgfpointorigin}

    \inheritsavedanchors[from=rectangle] % this is nearly a rectangle
    \inheritanchorborder[from=rectangle]
    \inheritanchor[from=rectangle]{east}

    \savedanchor{\connectorone}{
      \pgfextractx{\originx}{\pgfpointorigin}
      \pgfextracty{\originy}{\pgfpointorigin}  
      \pgfpoint{\originx+1.0}{\originy+0.2}
    }
    \anchor{connectorone}{\connectorone}

    \anchor{connectortwo}{
      \pgfextractx{\originx}{\pgfpointorigin}
      \pgfextracty{\originy}{\pgfpointorigin}  
      \pgfpoint{\originx+1.0}{\originy-0.2}
    }

    \backgroundpath{%
      \draw (0, 0) node[elmech](motor){M};
      \draw (motor.north) -- ++(0, 0.3) -- ++(0.7, 0) |- (1.0, 0.2);
      \draw (motor.south) -- ++(0, -0.3) -- ++(0.7, 0) |- (1.0, -0.2);
    }
}


\begin{document}
  \begin{circuitikz}
    \draw [help lines] (0,0) grid +(5,5);

    \node [shape=electric engine with connectors] (e1) at (3, 3) {};
    \draw (e1.connectorone) -- (5, 5);
    \draw (e1.connectortwo) -- (5, 0);
  \end{circuitikz}
\end{document}

错误的输出

答案1

在这种情况下,如果没有指定单位,pt则使用。因此,当您这样做时,\pgfpoint{\originx+1.0}{\originy-0.2}您会得到位于原点右侧 1pt 处和上方 0.2pt 处的点。如果您明确定义单位(如cm),结果会更好。

在此处输入图片描述

\documentclass[border=5mm]{standalone}% 'crop' is the default for v1.0, before it was 'preview'
%\documentclass[crop,tikz,convert={density=300,size=800x800,outext=.png}]{standalone}

\usepackage{circuitikz}
\usetikzlibrary{positioning}
\usetikzlibrary{calc}

\usepackage[T1]{fontenc}
\usepackage[utf8]{inputenc}

\newlength{\originx}
\newlength{\originy}

\pgfdeclareshape{electric engine with connectors}
{
    \anchor{center}{\pgfpointorigin}

    \inheritsavedanchors[from=rectangle] % this is nearly a rectangle
    \inheritanchorborder[from=rectangle]
    \inheritanchor[from=rectangle]{east}

    \savedanchor{\connectorone}{
      \pgfextractx{\originx}{\pgfpointorigin}
      \pgfextracty{\originy}{\pgfpointorigin}  
      \pgfpoint{\originx+1.0cm}{\originy+0.2cm}
    }
    \anchor{connectorone}{\connectorone}

    \anchor{connectortwo}{
      \pgfextractx{\originx}{\pgfpointorigin}
      \pgfextracty{\originy}{\pgfpointorigin}  
      \pgfpoint{\originx+1.0cm}{\originy-0.2cm}
    }

    \backgroundpath{%
      \draw (0, 0) node[elmech](motor){M};
      \draw (motor.north) -- ++(0, 0.3) -- ++(0.7, 0) |- (1.0, 0.2);
      \draw (motor.south) -- ++(0, -0.3) -- ++(0.7, 0) |- (1.0, -0.2);
    }
}


\begin{document}
  \begin{circuitikz}
    \draw [help lines,opacity=0.2] (0,0) grid +(5,5);

    \node [shape=electric engine with connectors] (e1) at (3, 3) {};
    \draw (e1.connectorone) -- (5, 5);
    \draw (e1.connectortwo) -- (5, 0);
  \end{circuitikz}
\end{document}

相关内容