使用 TikZ 缩放几何图形的问题

使用 TikZ 缩放几何图形的问题

以下代码显示了使用 时计算点的偏移量scale=1.25。我可以使用 ,transform canvas={scale=1.25}但这scope会导致其他困难,例如需要bounding box手动创建 。这非常烦人,因为在复杂的几何结构中,偏移可能会结合在一起并导致错误的图形。一般来说,只要使用距离计算就会发生这种情况。有没有办法避免这个问题?

\documentclass[landscape]{article} 
\usepackage{tikz,fullpage}
 \usetikzlibrary{calc} 
\begin{document} 
\parindent=0pt

\begin{tikzpicture}
\draw[help lines](0,0) grid (18,1);
 \coordinate(A) at (0,0);
 \coordinate(C) at (10,0);
 \coordinate(B) at (6,0);
 \path (A) -- (B) coordinate[pos=.5](E);
 \path (B) -- (C) coordinate[pos=.5](F);
 \path[coordinate] let 
   \p1 = ($ (B) - (E) $),
   \n1={veclen(\x1,\y1)},
   \p2 = ($ (B) - (F) $),
   \n2={veclen(\x2,\y2)},
    in (barycentric cs:E={-\n2/1cm},F={\n1/1cm}) coordinate (D);
 \foreach \point in {A,B,C,D,E,F}
    \fill [black,opacity=.5] (\point) circle (2pt);
\end{tikzpicture}

\vspace{1cm}
\begin{tikzpicture}[scale=1.25]
  \draw[help lines](0,0) grid (18,1);
  \coordinate(A) at (0,0);
  \coordinate(C) at (10,0);
  \coordinate(B) at (6,0);
  \path (A) -- (B) coordinate[pos=.5](E);
  \path (B) -- (C) coordinate[pos=.5](F);
  \path[coordinate] let 
    \p1 = ($ (B) - (E) $),
    \n1={veclen(\x1,\y1)},
    \p2 = ($ (B) - (F) $),
    \n2={veclen(\x2,\y2)},
     in (barycentric cs:E={-\n2/1cm},F={\n1/1cm}) coordinate (D);
  \foreach \point in {A,B,C,D,E,F}
     \fill [black,opacity=.5] (\point) circle (2pt);
\end{tikzpicture}

\vspace{1cm}
\begin{tikzpicture}
\begin{scope}[transform canvas={scale=1.25}]
  \draw[help lines](0,0) grid (18,1);
  \coordinate(A) at (0,0);
  \coordinate(C) at (10,0);
  \coordinate(B) at (6,0);
  \path (A) -- (B) coordinate[pos=.5](E);
  \path (B) -- (C) coordinate[pos=.5](F);
  \path[coordinate] let 
    \p1 = ($ (B) - (E) $),
    \n1={veclen(\x1,\y1)},
    \p2 = ($ (B) - (F) $),
    \n2={veclen(\x2,\y2)},
     in (barycentric cs:E={-\n2/1cm},F={\n1/1cm}) coordinate (D);
  \foreach \point in {A,B,C,D,E,F}
     \fill [black,opacity=.5] (\point) circle (2pt);
\end{scope}
\useasboundingbox (0,0) rectangle (18,1);
\end{tikzpicture}
\end{document}

在此处输入图片描述

答案1

在此处输入图片描述 解决传播错误问题的一种方法是直接计算重心(使用calc),而不调用画布上的坐标变化。

代码

\documentclass[landscape]{article} 
\usepackage{tikz, fullpage}
 \usetikzlibrary{calc} 
\begin{document} 
\parindent=0pt


Computing $D$ directly as a barycenter of $E$ and $F$\\
\begin{tikzpicture}
  \draw[help lines](0,0) grid (18,1);
  \coordinate(A) at (0,0);
  \coordinate(C) at (10,0);
  \coordinate(B) at (6,0);
  \path (A) -- (B) coordinate[pos=.5](E);
  \path (B) -- (C) coordinate[pos=.5](F);
  \path[coordinate] let 
    \p1 = ($ (B) - (E) $),
    \n1={veclen(\x1,\y1)},
    \p2 = ($ (B) - (F) $),
    \n2={veclen(\x2,\y2)},
  in (${-\n2/1cm}*(E) +{\n1/1cm}*(F)$) coordinate (D);
  \foreach \point in {A,B,C,D,E,F}
  \filldraw[blue!70!black, opacity=.5] (\point) circle (2pt)
  node[below right, opacity=1] {$\point$};
\end{tikzpicture}

\vspace{1cm}
Computing $D$ by invoking the barycentric system through
\texttt{barycenter cs:}\\
\begin{tikzpicture}
  \draw[help lines](0,0) grid (18,1);
  \coordinate(A) at (0,0);
  \coordinate(C) at (10,0);
  \coordinate(B) at (6,0);
  \path (A) -- (B) coordinate[pos=.5](E);
  \path (B) -- (C) coordinate[pos=.5](F);
  \path[coordinate] let 
    \p1 = ($ (B) - (E) $),
    \n1={veclen(\x1,\y1)},
    \p2 = ($ (B) - (F) $),
    \n2={veclen(\x2,\y2)},
  in (barycentric cs:E={-\n2/1cm},F={\n1/1cm}) coordinate (D);
  \foreach \point in {A,B,C,D,E,F}
  \filldraw [black, opacity=.5] (\point) circle (2pt)
  node[below right, opacity=1] {$\point$};
\end{tikzpicture}

\vspace{1.5cm}
Computing $D$ directly as a barycenter of $E$ and $F$ $+$
\texttt{scale=1.25}\\
\begin{tikzpicture}[scale=1.25]
  \draw[help lines](0,0) grid (18,1);
  \coordinate(A) at (0,0);
  \coordinate(C) at (10,0);
  \coordinate(B) at (6,0);
  \path (A) -- (B) coordinate[pos=.5](E);
  \path (B) -- (C) coordinate[pos=.5](F);
  \path[coordinate] let 
    \p1 = ($ (B) - (E) $),
    \n1={veclen(\x1,\y1)},
    \p2 = ($ (B) - (F) $),
    \n2={veclen(\x2,\y2)},
  in (${-\n2/1cm}*(E) +{\n1/1cm}*(F)$) coordinate (D);
  \foreach \point in {A,B,C,D,E,F}
  \filldraw [blue!70!black, opacity=.5] (\point) circle (2pt)
  node[below right, opacity=1] {$\point$};
\end{tikzpicture}

\vspace{1cm}
Computing $D$ by invoking the barycentric system through
\texttt{barycenter cs:} $+$ \texttt{scale=1.25}\\
\begin{tikzpicture}[scale=1.25]
  \draw[help lines](0,0) grid (18,1);
  \coordinate(A) at (0,0);
  \coordinate(C) at (10,0);
  \coordinate(B) at (6,0);
  \path (A) -- (B) coordinate[pos=.5](E);
  \path (B) -- (C) coordinate[pos=.5](F);
  \path[coordinate] let 
    \p1 = ($ (B) - (E) $),
    \n1={veclen(\x1,\y1)},
    \p2 = ($ (B) - (F) $),
    \n2={veclen(\x2,\y2)},
  in (barycentric cs:E={-\n2/1cm},F={\n1/1cm}) coordinate (D);
  \foreach \point in {A,B,C,D,E,F}{
    \filldraw [black,opacity=.5] (\point) circle (2pt)
    node[below right, opacity=1] {$\point$};
  }
\end{tikzpicture}
\end{document}

相关内容