答案1
这真的只是为了好玩,无法与 Alain Matthes 的出色宏和例程相媲美。然而,让 Ti钾Z 在给定线上方/下方绘制正方形,使得该线成为正方形的一条边。
\documentclass[tikz,border=3.14mm]{standalone}
\usetikzlibrary{calc,positioning}
\begin{document}
\tikzset{square/.style={minimum size=#1,draw},
measureme/.style={execute at begin to={
\path let \p1=($ (\tikztostart) - (\tikztotarget) $),\n1={veclen(\x1,\y1)}
in \pgfextra{\xdef#1{\n1}};}}}
\begin{tikzpicture}
\draw[measureme=\mylen](0,0)
to node[midway,sloped,above,square=\mylen,fill=blue!20]{\xdef\mylenC{\mylen}} node[midway,left=3pt]{$c$} (2,4)
to node[midway,sloped,above,square=\mylen,fill=red!20]{\xdef\mylenB{\mylen}} node[midway,right=3pt]{$b$} (2,0)
to node[midway,sloped,below,square=\mylen,fill=purple!20]{\xdef\mylenA{\mylen}} node[midway,below=3pt]{$a$} (0,0);
\begin{scope}[yshift=-5cm]
\node[square=\mylenB,fill=red!20](B) {$b^2$};
\node[left=2pt of B] (plus) {$+$};
\node[left=2pt of plus,square=\mylenA,fill=purple!20](A) {$a^2$};
\node[right=2pt of B] (eq) {$=$};
\node[right=2pt of eq,square=\mylenC,fill=blue!20](C) {$c^2$};
\end{scope}
\end{tikzpicture}
\end{document}
编辑:使节点标签直立,并添加毕达哥拉斯关系。
解释:measureme
风格让 Ti钾Z 测量边的长度(并将结果存储在\mylen
示例中调用的宏中)。正方形样式生成一个正方形,放置在给定边的中间,使其与边重合。
答案2
答案3
\input tikz.tex
\input pgfmath.tex
{\vskip 5mm plus 2mm minus 2mm
\leftskip=0mm plus 1fil
\rightskip=0mm plus 1fil
\parindent=0pt
\parfillskip=0pt
\tikzpicture
\def\xa{0}
\def\ya{0}
\def\xb{0}
\def\yb{3}
\def\xc{-1}
\def\yc{0}
\coordinate (A) at (\xa,\ya);
\coordinate (B) at (\xb,\yb);
\coordinate (C) at (\xc,\yc);
\pgfmathanglebetweenpoints{\pgfpointanchor{A}{center}}{\pgfpointanchor{B}{center}}
\edef\angleab{\pgfmathresult}
\pgfmathanglebetweenpoints{\pgfpointanchor{B}{center}}{\pgfpointanchor{C}{center}}
\edef\anglebc{\pgfmathresult}
\pgfmathanglebetweenpoints{\pgfpointanchor{C}{center}}{\pgfpointanchor{A}{center}}
\edef\angleca{\pgfmathresult}
\pgfmathparse{veclen(abs(\xa-\xb),abs(\ya-\yb))}
\edef\lenab{\pgfmathresult}
\pgfmathparse{veclen(abs(\xb-\xc),abs(\yb-\yc))}
\edef\lenbc{\pgfmathresult}
\pgfmathparse{veclen(abs(\xc-\xa),abs(\yc-\ya))}
\edef\lenca{\pgfmathresult}
\draw[fill=green,opacity=0.5]
(A) -- (B) -- ++ (\angleab-90:\lenab) -- ++ (\angleab-180:\lenab) -- cycle;
\draw[fill=blue,opacity=0.5]
(B) -- (C) -- ++ (\anglebc-90:\lenbc) -- ++ (\anglebc-180:\lenbc) -- cycle;
\draw[fill=red,opacity=0.5]
(C) -- (A) -- ++ (\angleca-90:\lenca) -- ++ (\angleca-180:\lenca) -- cycle;
\endtikzpicture
\vskip 2mm plus 1mm minus 1mm
\line{\hfil{Figure. My Lovely Picture}\hfil}
\par}
\bye