Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

I am trying to calculate the center of mass of a few coordinates and the logical way of adding up and then dividing results in an error.

\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}

I couldn't find any examples like this online. Adding up the five coordinates work fine. Thanks for the help!

share|improve this question

2 Answers 2

up vote 8 down vote accepted

1st option: You may use a multiplication instead of a division (the sum of the weights must be one):

\coordinate (com) at ($.2*(m1)+.2*(m2)+.2*(m3)+.2*(m4)+.2*(f1)$);

or

\pgfmathsetmacro\weight{1/5}
\coordinate (com) at ($\weight*(m1)+\weight*(m2)+\weight*(m3)+\weight*(m4)+\weight*(f1) $);

2nd option: You may use barycentric cs (the barycentric coordinate system) with weights:

\coordinate (com) at (barycentric cs:m1=.2,m2=.2,m3=.2,m4=.2,f1=.2);

or

\coordinate (com) at (barycentric cs:m1=1,m2=1,m3=1,m4=1,f1=1);

A complete example:

enter image description here

\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}
share|improve this answer

You cannot perform arithmetic (division) on node names. You have to deal with their x and y part separately, which would require the use of the let ... in construct.

It is much easier to use the barycentric coordinate system, as follows:

\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);

and get:

Result

For completness, this is a solution which uses the aforementioned let ... in syntax:

\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);
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.