|
用行列式计算三角形面积 = |A_x*(B_y-C_y) + B_x*(C_y-A_y) + C_x*(A_y-B_y)|/2
下面是一个完整的代码:
- \def\area#1#2#3{
- \newdimen\Ax
- \pgfextractx{\Ax}{\pgfpointanchor{#1}{center}}
- \newdimen\Ay
- \pgfextracty{\Ay}{\pgfpointanchor{#1}{center}}
- \newdimen\Bx
- \pgfextractx{\Bx}{\pgfpointanchor{#2}{center}}
- \newdimen\By
- \pgfextracty{\By}{\pgfpointanchor{#2}{center}}
- \newdimen\Cx
- \pgfextractx{\Cx}{\pgfpointanchor{#3}{center}}
- \newdimen\Cy
- \pgfextracty{\Cy}{\pgfpointanchor{#3}{center}}
- \pgfmathsetmacro{\area}{(1pt/1cm)*(1pt/1cm)*abs((\Ax*(\By-\Cy) + \Bx*(\Cy-\Ay) + \Cx*(\Ay-\By)))/2}
-
- % Mark the centroid
- \pgfmathsetmacro{\Gx}{(\Ax+\Bx+\Cx)/3}
- \pgfmathsetmacro{\Gy}{(\Ay+\By+\Cy)/3}
- \fill[red] (\Gx pt, \Gy pt) circle (2pt);
-
- % Output the area value at the centroid
- \node[below right] at (\Gx pt, \Gy pt) {$\area$};
- }
Copy the Code |
|