我在 TikZ 中找不到允许绘制一条通过两个指定点的线(可以控制偏移)的本机命令。到目前为止,我正在使用扩展calc
来执行我需要的操作,但可能还有更好的方法:
\documentclass[12pt]{book}
\usepackage[utf8]{inputenc}
\usepackage[T1]{fontenc}
\usepackage{tikz}
\usetikzlibrary{calc}
\begin{document}
\begin{tikzpicture}
\coordinate (V1) at (0.5,2.25); % initial point 1
\coordinate (V2) at (1.5,3.25); % initial point 2
\coordinate (V3) at ($1.2*(V1)-0.2*(V2)$); % offset 1
\coordinate (V4) at ($-0.2*(V1)+1.2*(V2)$); % offset 2
\draw (V3)--(V4); % line with offset
\end{tikzpicture}
\end{document}
答案1
它在 中定义tkz-base
。您需要给出在右侧和左侧添加的线的百分比。它对于获取 2*AB、3*AB 等很有用。
\documentclass[12pt]{scrartcl}
\usepackage{tikz}
\usetikzlibrary{calc}
\begin{document}
\tikzset{%
add/.style args={#1 and #2}{
to path={%
($(\tikztostart)!-#1!(\tikztotarget)$)--($(\tikztotarget)!-#2!(\tikztostart)$)%
\tikztonodes},add/.default={.2 and .2}}
}
\begin{tikzpicture}
\coordinate (A) at (0,0);
\coordinate (B) at (4,2);
\node at (A) {$\bullet$};
\node at (B) {$\bullet$};
\draw [add= 1 and .5, red, ultra thick] (A) to (B);
\end{tikzpicture}
\end{document}
应用
\begin{tikzpicture}
\coordinate (A) at (0,0);
\coordinate (B) at (1,0);
\foreach \t in {-0.25,0,...,4}
{\draw [add= {\t} and {\t},
red,
ultra thick] ([yshift=1.5*\t cm]A) to ([yshift=1.5*\t cm]B) ;}
\end{tikzpicture}
答案2
如果您想要做的只是在两个方向上延长线,则可以使用shorten >=<length>
并shorten <=<length>
提供负长度:
\draw [shorten >= -0.25cm, shorten <=-0.30cm] (V1)--(V2);
为了能够看到这一点,我定义了新的坐标(V1')
和,(V2')
它们与原始坐标水平偏移。黑线是您从(V1)
到(V2)
(即(V3)--(V4)
)手动延伸的线,红线是从(V1') -- (V2')
使用规范延伸的线shorten
。每个坐标的位置都用蓝色圆圈表示。
代码:
\documentclass[12pt]{book}
\usepackage[utf8]{inputenc}
\usepackage[T1]{fontenc}
\usepackage{tikz}
\usetikzlibrary{calc}
\begin{document}
\begin{tikzpicture}
\coordinate (V1) at (0.5,2.25); % initial point 1
\coordinate (V2) at (1.5,3.25); % initial point 2
\coordinate (V3) at ($1.2*(V1)-0.2*(V2)$); % offset 1
\coordinate (V4) at ($-0.2*(V1)+1.2*(V2)$); % offset 2
\draw [black, thick] (V3)--(V4); % line with offset (manually extended)
%% Shift the coordinate to be able to see the result
\coordinate (V1') at ($(V1) + (0.50cm,0)$);
\coordinate (V2') at ($(V2) + (0.50cm,0)$);
%% Draw line but extend it in either direction (extended via shorten)
\draw [red, thick, shorten >= -0.25cm, shorten <=-0.30cm] (V1')--(V2');
%% Show the location of the coordinates -- useful for debugging
\draw [thin, gray,-latex] (V1) -- (V1');
\draw [thin, gray,-latex] (V2) -- (V2') ;
\foreach \name/\placement in {V1/left, V2/left, V3/left, V4/left, V1'/right, V2'/right} {%
\node [fill=blue!50,shape=circle,inner sep=1pt] at (\name) {};
\node [gray, \placement] at (\name) {\tiny $\name$};
}
\end{tikzpicture}
\end{document}
答案3
[pos =]
在路径中使用的另一种解决方案。
\begin{tikzpicture}
\coordinate (A) at (0,0);
\coordinate (B) at (4,2);
\path (A) -- (B) coordinate[pos=-1](dd) coordinate[pos=1.5](ff);
\draw (dd) -- (A)node{$\bullet$}-- (B) node {$\bullet$}--(ff);
\end{tikzpicture}