如何在 TikZ 中垂直和水平對齊座標

如何在 TikZ 中垂直和水平對齊座標

我有無理座標的點,因為我混合了極座標和直角座標。我想指定一個座標作為一個事物的 x 值和另一個事物的 y 值。

在這個具體範例中,我希望左側的兩條線具有相同的左端點。

\documentclass{standalone}
\usepackage{tikz}
\usetikzlibrary{calc}

\begin{document}

  \begin{tikzpicture}
    \coordinate (left) at (155:2);
    \coordinate (top) at (65:2);
    \coordinate (bottom) at (245:2);
    \coordinate (right) at (335:2);

    \coordinate (leftEdge) at ($ (left) + (-2,0) $);
    \coordinate (bottomEdge) at ($ (bottom) + (0,-2) $);

    \path[draw=black] (left) -- (top) -- (right) -- (bottom) -- cycle;
    \path[draw=blue] (left) -- (leftEdge);
    \path[draw=black] (bottom) -- (bottomEdge);
    \path[draw=red] (bottom) -- ++(-2,0);
  \end{tikzpicture}

\end{document}

我認為有一種方法可以用類似的方法來做到這一點(leftEdge.x,bottom.y),但我沒有成功地讓它發揮作用。

在此輸入影像描述

如何讓紅色邊緣與藍色邊緣一樣遠?

答案1

在您的範例中,您所需要做的就是說\path[draw=red] (bottom) -- (bottom-|leftEdge);.這個很棒的答案為您提供了對該語法的非常有效的討論。

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

\begin{document}

  \begin{tikzpicture}
    \coordinate (left) at (155:2);
    \coordinate (top) at (65:2);
    \coordinate (bottom) at (245:2);
    \coordinate (right) at (335:2);

    \coordinate (leftEdge) at ($ (left) + (-2,0) $);
    \coordinate (bottomEdge) at ($ (bottom) + (0,-2) $);

    \path[draw=black] (left) -- (top) -- (right) -- (bottom) -- cycle;
    \path[draw=blue] (left) -- (leftEdge);
    \path[draw=black] (bottom) -- (bottomEdge);
    \path[draw=red] (bottom) -- (bottom-|leftEdge);
  \end{tikzpicture}

\end{document}

在此輸入影像描述

相關內容