TeX - LaTeX Asked on November 1, 2021
The following code is straightforward (I have left blank lines to distinguish 5 sections):
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}
Defines the coordinates of a right triangle, and the base point of the construction.
Defines each new coordinate as the foot of a height to either segment CA or BA from a previously computed coordinate, starting from the base point.
Draw the triangle.
Draw the heights toward BA and label them.
Draw the heights toward CA.
My question is what is the syntax to automate this process, so that I can do the same, not just 3 steps in, but 5, 10, or variably more steps?
One foreach
loop is enough to draw. The coordinates (Ai)
, (Bi)
, (Ci)
are recusively defined.
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
($(Aj)!(Cj)!(Bj)$) coordinate (Bi)
($(Aj)!(Bi)!(Cj)$) coordinate (Ci)
(Aj) coordinate (Ai);
fill[brown!30] (Cj)--(Bi)--(Ci)--cycle;
draw[blue] (Bi)--(Bj) node[midway,below] {$c_{j}$};
draw[red] (Cj)--(Bi) node[pos=.7,left]{$h_{j}$};
draw[violet] (Bi)--(Ci) node[pos=.7,above left]{$a_{j}$};
}
draw (A0)--(C0);
end{tikzpicture}
end{document}
Answered by Black Mild on November 1, 2021
With foreach
, only two paths to which the points are projected and a start point are needed.
In your case, path 1
is BA
, path 2
is CA
, start
point is C
. From start, C
is projected to path 1
, and then the perpendicular is projected to path 2
, and new perpendicular is projected to path 1
and so on...
documentclass[tikz, border=1cm]{standalone}
usetikzlibrary{calc}
defperpset#1{pgfqkeys{/perpendicular}{#1}}
makeatletter
perpset{
/perpendicular/.search also=/tikz,
path 1/.code args={from (#1) to (#2)}{
defperp@from@a{#1}
defperp@to@a{#2}
},
path 2/.code args={from (#1) to (#2)}{
defperp@from@b{#1}
defperp@to@b{#2}
},
start/.code args={(#1)}{defperp@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}
Answered by ZhiyuanLck on November 1, 2021
Get help from others!
Recent Questions
Recent Answers
© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP