
各位 tikzers 大家好。
是否有一個包/黑客來計算折線中封閉的面積,例如像這樣的東西
\draw (p1) to (p2) to (p3) to cycle;
其中點是頂點。多邊形可能不是凸的。
我想可以改進/修改填充演算法以返回面積的值(以使用的單位表示)。但我沒有技術專長,也許已經有了答案。
謝謝。
PS:編輯添加該面積只是沿邊界的積分
$$ \點 x dy $$
所以很容易計算。當然,我不僅僅對四邊形感興趣,而是對具有許多頂點的折線感興趣,因此手工計算變得令人煩惱的長。
答案1
Ti鞋帶配方的應用kz 庫math
。以@Thruston指定的連結中的複雜應用程式為例。
\documentclass[margin=3mm]{standalone}
\usepackage{tikz}
\usetikzlibrary{math}
\usepackage{tkz-euclide}
\tikzmath{
\x1 = 3; \y1 =4;
\x2 = 5; \y2 =11;
\x3 = 12; \y3 =8;
\x4 = 9; \y4 =5;
\x5 = 5; \y5 =6;
\Det = (\x1*\y2) + (\x2*\y3) + (\x3*\y4) + (\x4*\y5) + (\x5*\y1)-
(\x2*\y1) - (\x3*\y2) - (\x4*\y3) - (\x5*\y4) - (\x1*\y5);
\Area = abs (\Det / 2);
}
\begin{document}
\begin{tikzpicture}
\tkzInit[xmax=13,ymax=12,xmin=0,ymin=0]
\tkzGrid
\tkzAxeXY
\draw[fill=gray!30](\x1,\y1)--(\x2,\y2)--(\x3,\y3)--(\x4,\y4)--(\x5,\y5)--cycle;
\tkzText [below](6.5,-1){$A = \Area$} ;
\end{tikzpicture}
\end{document}
答案2
這在用戶領域應該不會太難——當你在等待 TikZ 團隊的時候,這裡有一個簡單的鞋帶演算法簡單地完成梅塔普斯特。編譯mpost
或適應lualatex
with luamplib
。
prologues := 3;
outputtemplate := "%j%c.%{outputformat}";
vardef shoelace_area(expr p) =
for i=1 upto length p:
+ 1/2 xpart point i of p * ypart point i+1 of p
- 1/2 ypart point i of p * xpart point i+1 of p
endfor
enddef;
beginfig(1);
path t; t = for i=0 upto 4: 100 dir 72i -- endfor cycle;
draw origin -- point 1 of t; label.lft(btex $R=100$ etex, 1/2 point 1 of t);
draw t; dotlabel.rt(decimal shoelace_area(t), origin) withcolor 2/3 red;
draw thelabel(btex $\displaystyle A = {5\over4}R^2\sqrt{5+\sqrt5\over2}$ etex, origin) scaled 3/4 shifted 30 down;
draw thelabel(btex $A = 2.37764129 R^2$ etex, origin) scaled 3/4 shifted 54 down;
endfig;
end.
範例圖將其應用於五邊形:
正如我試圖展示的,這會給你大約 6 sig。圖.使用普通 MP 的縮放算術來提高準確性,如果將其應用於比此大得多的任何東西,您將得到算術溢出。但如果你用它進行編譯,mpost -numbersystem double
你會得到準確的答案,並且溢出的可能性會更少: