![TikZ 中的直线+交点](https://linux22.com/image/344061/TikZ%20%E4%B8%AD%E7%9A%84%E7%9B%B4%E7%BA%BF%2B%E4%BA%A4%E7%82%B9.png)
我有四点
\coordinate (N1) at (-4*pi/3, 0);
\coordinate (N2) at (-2*pi/3, {( 2/3)*pi*sqrt(3)});
\coordinate (N3) at ( 2*pi/3, {( 2/3)*pi*sqrt(3)});
\coordinate (N4) at ( 4*pi/3, 0);
并希望实现以下目标:
N1
通过和N3
(和N2
和N4
分别)画直线(!没有线段)- 计算这两条线的交点并给它命名(例如
IS1
),以便我以后可以使用它。
这应该不难实现,但我刚开始学习 TikZ,大约 1100 页的参考资料让我有点望而却步,而且无法在那里找到它。
答案1
通过线而不是线段,我猜你想将线延伸到坐标之外。以下将线延伸到指定坐标之外 0.5 厘米。
\documentclass[border=5mm]{standalone}
\usepackage{tikz}
\usetikzlibrary{intersections,calc}
\begin{document}
\begin{tikzpicture}
\coordinate (N1) at (-4*pi/3, 0);
\coordinate (N2) at (-2*pi/3, {( 2/3)*pi*sqrt(3)});
\coordinate (N3) at ( 2*pi/3, {( 2/3)*pi*sqrt(3)});
\coordinate (N4) at ( 4*pi/3, 0);
\draw [name path=A] ($(N1)!-0.5cm!(N3)$) -- ($(N3)!-0.5cm!(N1)$);
\draw [name path=B] ($(N2)!-0.5cm!(N4)$) -- ($(N4)!-0.5cm!(N2)$);
\path [name intersections={of=A and B,name=i}];
\fill [red] (i-1) circle[radius=3pt];
\foreach \n in {1,...,4}
\fill [blue] (N\n) circle[radius=2pt];
\end{tikzpicture}
\end{document}
答案2
作为 Torbjørn T. 答案的替代/补充,但代码略短,并且延长行的方式更简单:
\documentclass[border=0mm]{standalone}
\usepackage{tikz}
\usetikzlibrary{intersections,calc}
\begin{document}
\begin{tikzpicture}[
shrtn/.style = {shorten <=-5mm, shorten >=-5mm}
]
\coordinate (N1) at (-4*pi/3, 0);
\coordinate (N2) at (-2*pi/3, {( 2/3)*pi*sqrt(3)});
\coordinate (N3) at ( 2*pi/3, {( 2/3)*pi*sqrt(3)});
\coordinate (N4) at ( 4*pi/3, 0);
%
\draw [red,name path=A,shrtn] (N1) -- (N3);
\draw [red,name path=B,shrtn] (N2) -- (N4);
%
\fill [name intersections={of=A and B, by={a}}, blue]
(a) circle (3pt);
%
\foreach \n in {1,...,4}
\fill [blue] (N\n) circle[radius=2pt];
\node[fit=(N1) (N3) (N4), scale=1.1] {};% <-- determine bounding box
\end{tikzpicture}
\end{document}
编辑:
边界框考虑shrtn
延长线直到给定坐标。完整的线可见(当standalone
有选项时border=0pt
)您有两种可能性:
- 计算线末端的坐标(Torbjørn T. 在他的回答中也这样做)
- 确定具有缩放节点的边界框,适合给定的坐标(参见上面的编辑 MWE)
答案3
这是另一个版本元帖子和luamplib. 用 编译lualatex
。
\RequirePackage{luatex85}
\documentclass[border=5mm]{standalone}
\usepackage{luamplib}
\begin{document}
\mplibtextextlabel{enable}
\begin{mplibcode}
beginfig(1);
numeric u;
u = 3.14159265359 cm; % unit is arbitrary
% implicitly define points z1, z2, z3, and z4
-x1 = x4 = 4/3u;
-x2 = x3 = 2/3u;
y1 = y4 = 0;
y2 = y3 = 2/3u * sqrt(3);
% z5 is the intersection of z1--z3 and z2--z4
z5 = whatever[z1,z3] = whatever[z2,z4];
% idiom to draw line through two points
r = 1+7mm/length(z1-z3); % with 7mm overhang...
draw r[z1,z3] -- r[z3,z1] withcolor .57 red;
draw r[z2,z4] -- r[z4,z2] withcolor .57 red;
% add some dots and labels
dotlabel.ulft("$N_1$", z1);
dotlabel.llft("$N_2$", z2);
dotlabel.lrt ("$N_3$", z3);
dotlabel.urt ("$N_4$", z4);
dotlabel.top ("$I$", z5);
endfig;
\end{mplibcode}
\end{document}