下面的代码很简单(我留下了空行来区分 5 个部分):
\begin{tikzpicture}
\coordinate (B) at (0,0);
\coordinate (A) at (12,0);
\coordinate (C) at (3,5);
\coordinate (f0) at ($(B)!(C)!(A)$);
\coordinate (f1) at ($(C)!(f0)!(A)$);
\coordinate (f2) at ($(B)!(f1)!(A)$);
\coordinate (f3) at ($(C)!(f2)!(A)$);
\coordinate (f4) at ($(B)!(f3)!(A)$);
\coordinate (f5) at ($(C)!(f4)!(A)$);
\coordinate (f6) at ($(B)!(f5)!(A)$);
\draw (B) -- (A) -- (C)
node [midway,above] {$\mathbf b$} --
node [midway, left] {$\mathbf a$} cycle;
\filldraw[fill=blue!5!white, draw=black]
(C) -- (f0) node [midway,left] {$h_0$} --
(f1) node [midway,above,left] {$a_0$} --
(f2) node [midway,left] {$h_1$} --
(f3) node [midway,above,left] {$a_1$} --
(f4) node [midway,left] {$h_2$} --
(f5) node [midway,above,left] {$a_2$};
\draw
(B) --
(f0) node [midway,below] {$c_0$} --
(f2) node [midway,below] {$c_1$} --
(f4) node [midway,below] {$c_2$} --
(f6) node [midway,below] {$c_3$};
\end{tikzpicture}
定义直角三角形的坐标,以及基点建筑施工
将每个新坐标定义为线段 CA 或 BA 的高度脚根据先前计算的坐标,从基点开始。
画出三角形。
将高度画向 BA并给它们贴上标签。
向 CA 方向绘制高度。
我的问题是,自动化这个过程的语法是什么,以便我可以做同样的事情,不只是 3 个步骤,而是 5 个、10 个或更多的步骤?
答案1
有了\foreach
,只需要两条点投影到的路径和一个起点。
在你的情况下,path 1
是BA
,path 2
是CA
,start
点是C
。从开始,C
投影到path 1
,然后垂直线投影到path 2
,新的垂直线投影到,path 1
依此类推......
\documentclass[tikz, border=1cm]{standalone}
\usetikzlibrary{calc}
\def\perpset#1{\pgfqkeys{/perpendicular}{#1}}
\makeatletter
\perpset{
/perpendicular/.search also=/tikz,
path 1/.code args={from (#1) to (#2)}{
\def\perp@from@a{#1}
\def\perp@to@a{#2}
},
path 2/.code args={from (#1) to (#2)}{
\def\perp@from@b{#1}
\def\perp@to@b{#2}
},
start/.code args={(#1)}{\def\perp@start{#1}}
}
\newcommand{\perpdraw}[2][1]{
\begin{scope}[/perpendicular/.cd,#2]
\coordinate (perp@start-0) at (\perp@start);
\foreach \x [count=\i from 0] in {1, ..., #1} {
\draw[fill=blue!5] (perp@start-\i)
-- node[left, teal] {$h_\i$} ($(\perp@from@a)!(perp@start-\i)!(\perp@to@a)$) coordinate (perp@end-\i)
-- node[left, red] {$a_\i$} ($(\perp@from@b)!(perp@end-\i)!(\perp@to@b)$) coordinate (perp@start-\x);
}
\end{scope}
}
\makeatother
\begin{document}
\begin{tikzpicture}
\coordinate (B) at (0,0);
\coordinate (A) at (12,0);
\coordinate (C) at (3,5);
\draw (B) -- (A) -- (C)
node [midway,above] {$\mathbf b$} --
node [midway, left] {$\mathbf a$} cycle;
\perpdraw[4]{
path 1=from (B) to (A),
path 2=from (C) to (A),
start=(C),
}
\end{tikzpicture}
\end{document}
答案2
一个\foreach
循环就足以绘制。坐标(Ai)
,(Bi)
,(Ci)
是递归定义的。
\documentclass[tikz,border=5mm]{standalone}
\usetikzlibrary{calc}
\begin{document}
\begin{tikzpicture}[join=round]
\path
(0,0) coordinate (B0)
(12,0) coordinate (A0)
(2,6) coordinate (C0)
;
\draw (B0)--(A0)--(C0)
node[midway,above]{$\mathbf b$} --
node[midway,left]{$\mathbf a$} cycle;
\foreach \i in {1,...,5}{
\pgfmathsetmacro{\j}{int(\i-1)}
\path
($(A\j)!(C\j)!(B\j)$) coordinate (B\i)
($(A\j)!(B\i)!(C\j)$) coordinate (C\i)
(A\j) coordinate (A\i);
\fill[brown!30] (C\j)--(B\i)--(C\i)--cycle;
\draw[blue] (B\i)--(B\j) node[midway,below] {$c_{\j}$};
\draw[red] (C\j)--(B\i) node[pos=.7,left]{$h_{\j}$};
\draw[violet] (B\i)--(C\i) node[pos=.7,above left]{$a_{\j}$};
}
\draw (A0)--(C0);
\end{tikzpicture}
\end{document}