我正在尝试计算几个坐标的质心,但先相加然后除以的逻辑方法会导致错误。
\documentclass[a4paper,14pt]{scrartcl}
\usepackage{tikz}
\usepackage{pgfplots,pgfplotstable}
\usetikzlibrary{calc}
\usepackage{calc}
\pgfplotsset{compat=newest}
\usepackage[active,tightpage]{preview}
\setlength\PreviewBorder{2pt}
\begin{document}
\begin{preview}
\begin{tikzpicture}
\coordinate (m1) at (0,0);
\filldraw [blue] (m1) circle (5);
\draw [blue] (m1) circle (75);
\draw [blue] (m1) circle (200);
\coordinate (f1) at ($(m1) + (0,10)$);
\filldraw [red] (f1) circle (3);
\coordinate (m2) at (45:150);
\filldraw[blue] (m2) circle (5);
\draw[blue] (m2) circle (15);
\coordinate (m3) at ($(m2) +(10:30)$);
\filldraw[blue] (m3) circle (5);
\coordinate (m4) at ($(m1) +(200:65)$);
\filldraw[blue] (m4) circle (5);
\coordinate (com) at ($ ((m1)+(m2)+(m3)+(m4)+(f1))/5 $);
\end{tikzpicture}
\end{preview}
\end{document}
我在网上找不到类似的例子。将五个坐标相加就可以了。谢谢帮助!
答案1
第一种选择:您可以使用乘法代替除法(权重之和必须为一):
\coordinate (com) at ($.2*(m1)+.2*(m2)+.2*(m3)+.2*(m4)+.2*(f1)$);
或者
\pgfmathsetmacro\weight{1/5}
\coordinate (com) at ($\weight*(m1)+\weight*(m2)+\weight*(m3)+\weight*(m4)+\weight*(f1) $);
第二种选择:您可以将barycentric cs
(重心坐标系)与权重一起使用:
\coordinate (com) at (barycentric cs:m1=.2,m2=.2,m3=.2,m4=.2,f1=.2);
或者
\coordinate (com) at (barycentric cs:m1=1,m2=1,m3=1,m4=1,f1=1);
完整示例:
\documentclass{standalone}
\usepackage{tikz}
\usetikzlibrary{calc}
\begin{document}
\begin{tikzpicture}
\coordinate (m1) at (0,0);
\filldraw [blue] (m1) circle (5);
\draw [blue] (m1) circle (75);
\draw [blue] (m1) circle (200);
\coordinate (f1) at ($(m1) + (0,10)$);
\filldraw [red] (f1) circle (3);
\coordinate (m2) at (45:150);
\filldraw[blue] (m2) circle (5);
\draw[blue] (m2) circle (15);
\coordinate (m3) at ($(m2) +(10:30)$);
\filldraw[blue] (m3) circle (5);
\coordinate (m4) at ($(m1) +(200:65)$);
\filldraw[blue] (m4) circle (5);
\coordinate (com) at ($.2*(m1)+.2*(m2)+.2*(m3)+.2*(m4)+.2*(f1) $);
\filldraw[green] (com) circle (5);
\coordinate (com1) at (barycentric cs:m1=.2,m2=.2,m3=.2,m4=.2,f1=.2);
\filldraw[black] (com1) circle (3);
\end{tikzpicture}
\end{document}
答案2
您无法对节点名称执行算术(除法)。您必须分别处理它们的 x 和 y 部分,这需要使用构造let ... in
。
使用barycentric
坐标系就容易多了,如下所示:
\coordinate (com) at (barycentric cs:m1=0.5,m2=0.5,m3=0.5,m4=0.5,f1=0.5);
\filldraw[yellow] (com) circle (5);
并得到:
为了完整性,这是一个使用上述let ... in
语法的解决方案:
\path let
\p1 = (m1),
\p2 = (m2),
\p3 = (m3),
\p4 = (m4),
\p5 = (f1),
\n1 = {(\x1+\x2+\x3+\x4+\x5)/5},
\n2 = {(\y1+\y2+\y3+\y4+\y5)/5}
in coordinate (com) at (\n1, \n2);