给定一个矩形节点,我X
可以访问坐标等。但是,我希望能够在节点顶部绘制直角三角形的两条边。例如,如果节点的左上角和右上角坐标为 ,那么我想绘制。(X.north)
(X.east)
(0,3)
(2,3)
(0,3) -- (1,4) -- (2,3)
换句话说:
\draw (X.north west) -- <what goes here?> -- (X.north east);
如何仅使用节点本身来获取中间点?我能做的最好的事情是
\draw (X.north west) -- ([shift={(X.north)}] X.north)-- (X.north east);
但这似乎有点笨拙。
答案1
这就是calc
图书馆的用途:
\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{calc}
\begin{document}
\begin{tikzpicture}
\node [draw] (X) {X node};
\draw (X.north west) -- ($(X.north west)!cos(45)!45:(X.north east)$) -- (X.north east);
\end{tikzpicture}
\end{document}
语法如下:
($(A)!<fraction>!(B)$)
指从到<fraction>
沿线的点。指连接线的中点,指,指。如果数字大于 1 或小于 0,则路径是推断出来的。(A)
(B)
($(A)!0.5!(B)$)
($(A)!0!(B)$)
(A)
($(A)!1!(B)$)
(B)
句法指的是从到这条线沿着该线旋转一圈后所在的($(A)!<fraction>!<angle>:(B)$)
点。<fraction>
(A)
(B)
(A)
<angle>
对于直角三角形,我们知道如果斜边与邻边的夹角为45°,则邻边的长度为cos(45)*h。
对于其他角度也类似:
\documentclass[border=5mm]{standalone}
\usepackage{tikz}
\usetikzlibrary{calc}
\begin{document}
\begin{tikzpicture}
\node [draw, outer sep=0pt] (X) {X node};
\foreach \angle in {15,30,...,75}{
\draw [red!\angle!blue] (X.north west) -- ($(X.north west)!cos(\angle)!\angle:(X.north east)$) -- (X.north east);
}
\end{tikzpicture}
\end{document}
答案2
利用一个事实:一个矩形三角形的斜边作为圆的直径,而它的另一个顶点在圆上,并使用以下intersections
库:
\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{calc,intersections}
\begin{document}
\begin{tikzpicture}
\node[draw,rectangle,minimum size=2cm] (X) {};
\begin{pgfinterruptboundingbox}
\path[name path=circle] let \p1=(X.north west), \p2=(X.north east) in
(X.north) circle (0.5*\x2-0.5*\x1);
\path[name path=line] (X.north) -- (60:3cm);
\draw[name intersections={of=circle and line,by={a}}] (X.north west) -- (a) -- (X.north east);
\end{pgfinterruptboundingbox}
\end{tikzpicture}
\end{document}
\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{calc,intersections}
\begin{document}
\begin{tikzpicture}
\node[draw,rectangle,minimum size=2cm] (X) {};
\begin{pgfinterruptboundingbox}
\path[name path=circle] let \p1=(X.north west), \p2=(X.north east) in
(X.north) circle (0.5*\x2-0.5*\x1);
\foreach \i/\colo in {40/green,60/red,90/cyan,120/magenta}
{
\path[name path=line\i] (X.north) -- (\i:3cm);
\draw[name intersections={of=circle and line\i,by={a\i}},draw=\colo] (X.north west) -- (a\i) -- (X.north east);
}
\end{pgfinterruptboundingbox}
\end{tikzpicture}
\end{document}