如果我只想将线向一个方向延长 1cm,而不是向另一个方向延长,该怎么办?我在一年前的另一篇文章中发现了这个问题。
\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{calc}
\begin{document}
\begin{tikzpicture}[dot/.style={circle,inner sep=1pt,fill,label={#1},name=#1},
extended line/.style={shorten >=-#1,shorten <=-#1},
extended line/.default=1cm]
\node [dot=A] at (0,0) {};
\node [dot=B] at (3,1) {};
\node [dot=P] at (1.9,-1.6) {};
\draw [extended line=0.5cm] (A) -- (B);
\draw [extended line] ($(A)!(P)!(B)$) -- (P);
\fill [red] ($(A)!(P)!(B)$) circle [radius=2pt];
\end{tikzpicture}
\end{document}
答案1
extended line
由于样式有 ,您的图形在两端延伸shorten <=-#1
。如果您删除它,则线条将不会在另一端延伸。但我建议您添加另一种样式。
one end extended/.style={shorten >=-#1},
one end extended/.default=1cm,
在您的选择中。
这是完整的代码和用法。
%\documentclass{article}
\documentclass[preview,border=5]{standalone}
\usepackage{tikz}
\usetikzlibrary{calc}
\begin{document}
\begin{tikzpicture}[dot/.style={circle,inner sep=1pt,fill,label={#1},name=#1},
extended line/.style={shorten >=-#1,shorten <=-#1},
extended line/.default=1cm,
one end extended/.style={shorten >=-#1},
one end extended/.default=1cm,
]
\node [dot=A] at (0,0) {};
\node [dot=B] at (3,1) {};
\node [dot=P] at (1.9,-1.6) {};
\draw [extended line=0.5cm] (A) -- (B);
\coordinate (P') at ($(A)!(P)!(B)$);
\draw [one end extended] (P') -- (P);
\fill [red] (P') circle [radius=2pt];
\end{tikzpicture}
\end{document}
编辑
我按照评论中的建议添加了P'
坐标名称,($(A)!(P)!(B)$)
以加快编译时间。
答案2
tkz-euclide
使用或都可以实现这一点tkz-base
,但这里的解决方案只有tikz
\documentclass{scrartcl}
\usepackage{tikz}
\usetikzlibrary{calc}
\tikzset{%
add/.style args={#1 and #2}{to path={%
($(\tikztostart)!-#1!(\tikztotarget)$)--($(\tikztotarget)!-#2!(\tikztostart)$)%
\tikztonodes}}
}
\begin{document}
\begin{tikzpicture}[dot/.style={circle,inner sep=1pt,fill,name=#1}]
\node [dot=A,label={below:$A$}] at (0,0) {};
\node [dot=B,label={below:$B$}] at (3,1) {};
\node [dot=P,label={right:$P$}] at (1.9,-1.6) {};
\draw [add=.5 and .5] (A) to (B);
\coordinate (P') at ($(A)!(P)!(B)$);
\draw [add=0 and .5] (P') to (P);
\fill [red] (P') circle [radius=2pt];
\end{tikzpicture}
\end{document}
答案3
在 PSTricks 中,我们可以使用nodesepA
(仅用于A
末端)、nodesepB
(仅用于B
末端)或nodesep
(用于末端A
和B
末端)来缩短或延长线末端。
\documentclass[pstricks]{standalone}
\usepackage{pst-eucl}
\begin{document}
\begin{pspicture}(8,6)
\pstGeonode(2,3){A}(6,5){B}(6,2){P}
\pstProjection[PointName=none]{A}{B}{P}
\psset{nodesep=-1}
\pstLineAB{A}{B}
\pstLineAB[nodesepB=0]{P}{P'}
\end{pspicture}
\end{document}
答案4
另一种选择是使用($ (A) !-1cm! (B) $)
语法:
\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{calc}
\begin{document}
\begin{tikzpicture}
\coordinate (A) at (0, 0);
\coordinate (B) at (3, 1);
\coordinate (P) at (1.9, -1.6);
\node at (A) [circle, color = black, fill, inner sep = 1 pt] {};
\node at (B) [circle, color = black, fill, inner sep = 1 pt] {};
\node at (P) [circle, color = black, fill, inner sep = 1 pt] {};
\node at (A) [anchor = south] {A};
\node at (B) [anchor = south] {B};
\node at (P) [anchor = west] {P};
\draw ($ (A) !-1cm! (B) $) -- ($ (B) !-1cm! (A) $);
\draw ($(A)!(P)!(B)$) -- ($(P) !-1cm! ($(A)!(P)!(B)$)$);
\fill [red] ($(A)!(P)!(B)$) circle [radius = 2pt];
\end{tikzpicture}
\end{document}
结果如下: