我有两点p1
:p2
\node (p1) at (-1,-2) [point, label = {left:$P_1$}]{};
\node (p2) at (3, 4) [point, label = {below:$P_2$}]{};
如何仅使用变量和绘制从点开始p1
并指向某点的单位向量?p2
p1
p2
答案1
您可以使用带单位的 calc 库语法。如果省略单位,则将被理解为完整路径百分比。否则,取决于行进距离的单位。
\documentclass[tikz]{standalone}
\usetikzlibrary{calc}
\begin{document}
\begin{tikzpicture}[point/.style={circle,draw,inner sep=2pt}]
\node[point, label = {left:$P_1$}] (p1) at (-1,-2) {};
\node[point, label = {below:$P_2$}] (p2) at (3, 4) {};
\foreach \x[count=\xi] in {,mm,cm,in}{
\draw[opacity=1/\xi,line width=\xi pt] (p1) -- ($(p1)!1\x!(p2)$);% 1,1mm,1cm,1in respectively
}
\end{tikzpicture}
\end{document}
答案2
p1
这将根据和之间距离的一小部分绘制线条p1
。例如和20%
之间的距离:p1
p2
\documentclass[tikz]{standalone}
\usetikzlibrary{calc}
\begin{document}
\begin{tikzpicture}[point/.style={circle,draw,inner sep=2pt}]
\node[coordinate, label = {left:$P_1$}] (p1) at (-1,-2){};
\node[coordinate, label = {below right:$P_2$}] (p2) at (3, 4){};
\node[point] at (p1){};
\node[point] at (p2){};
\draw let
\p1 = ($ (p2) - (p1) $),
\n1 = {veclen(\x1,\y1)}
in
(p1) -- ($(p1)!0.2*\n1!(p2)$)node[pos=1,right] {\pgfmathparse{0.2*\n1}\pgfmathresult};
\end{tikzpicture}
\end{document}