How can I put labels at centre of all polygons?

Base on comment of TorbjørnT, this is the code

\documentclass[border=1.5mm,12pt]{standalone}
%\usepackage[utf8]{inputenc}
\usepackage{fouriernc}
\usepackage{tkz-euclide,amsmath}
\usetkzobj{all}
\tikzset{hidden/.style = {thick, dashed}}
\begin{document}
\begin{tikzpicture}
\tkzDefPoint(0,0){O}
\tkzDefPoint(1,0){A}
\tkzDefPoint(2,0){B}
\tkzDefPoint(3,0){E}
\tkzDefPoint(1,-1){C}
\tkzDefPoint(0,-1){M}
\tkzDefPoint(2,-1){N}
\tkzDefPoint(1,-2){F}
\tkzDefPoint(1,1){G}
\tkzDefPoint(2,1){H}
\tkzDefPoint(1,2){I}
\tkzDefPointsBy[rotation=center N angle 60](F){D}
\tkzDrawSegments[thick](O,E O,M F,E A,C C,F)
\tkzDrawPolygon[thick](O,M,N,B)
\tkzDrawPolygon[thick](N,F,D)
\tkzDrawPolygon[thick](G,H,I)
\tkzDrawPolygon[thick](C,F,N)
\tkzDrawPolygon[thick](A,B,H,G)
 \node at (barycentric cs:N=1,F=1,D=1) {$G$};
 \node at (barycentric cs:N=1,B=1,E=1) {$E
 $};
  \node at (barycentric cs:A=1,B=1,N=1,C=1) {$D$};
  \node at (barycentric cs:G=1,H=1,I=1) {$A$};
   \node at (barycentric cs:A=1,B=1,H=1,G=1) {$B$};
   \node at (barycentric cs:O=1,A=1,M=1,C=1) {$C$};
    \node at (barycentric cs:C=1,F=1,N=1) {$G$};
\end{tikzpicture}
\end{document} 

enter image description here


If you want to stick to tkz-euclide, you can use \tkzDefBarycentricPoint to obtain the midpoints of triangles and rectangles. There are other possibilities as well though.

For rectangles you can use \tkzDefMidPoint(A,B), where A and B are two opposing corners.

For triangles there are different "center points", which can be found with \tkzCentroid(A,B,C) (same as \tkzDefBarycentricPoint(A=1,B=1,C=1) I believe), \tkzCircumCenter(A,B,C) (center of circumscribed circle) and \tkzInCenter(A,B,C) (center of inscribed circle). As with the barycentric point, the macro must be followed by \tkzGetPoint{<name of point>}.

You could also mix in pure TikZ syntax as I mention in a comment, and you in your answer. I.e. use \node at (barycentric cs:A=1,B=1,C=1) {a};

Note also that due to the way you draw the polygons, some of the vertices have ugly line joins:

enter image description here

But this can be easily remedied by drawing it more carefully, as in the code below.

\documentclass[border=1.5mm,12pt]{standalone}
\usepackage[utf8]{inputenc}
\usepackage{fouriernc}
\usepackage{tkz-euclide,amsmath}
\usetkzobj{all}
\tikzset{hidden/.style = {thick, dashed}}
\begin{document}
\begin{tikzpicture}
\tkzDefPoint(0,0){o}
\tkzDefPoint(1,0){a}
\tkzDefPoint(2,0){b}
\tkzDefPoint(3,0){e}
\tkzDefPoint(1,-1){c}
\tkzDefPoint(0,-1){m}
\tkzDefPoint(2,-1){n}
\tkzDefPoint(1,-2){f}
\tkzDefPoint(1,1){g}
\tkzDefPoint(2,1){h}
\tkzDefPoint(1,2){i}
\tkzDefPointsBy[rotation=center n angle 60](f){d}

%\tkzLabelPoints[left](o,a,b,c,d,e,f,g,h,i,m,n)
\tkzDrawPolygon[thick](i,h,b,e,n,d,f)
\tkzDrawSegments[thick](n,f g,h)
\tkzDrawPolygon[thick](o,m,n,b)

\tkzDefBarycentricPoint(i=1,h=1,g=1) \tkzGetPoint{A}
\tkzDefBarycentricPoint(g=1,h=1,a=1,b=1) \tkzGetPoint{B}
\tkzDefBarycentricPoint(o=1,a=1,m=1,c=1) \tkzGetPoint{C}
\tkzDefBarycentricPoint(a=1,b=1,c=1,n=1) \tkzGetPoint{D}
\tkzDefBarycentricPoint(b=1,e=1,n=1) \tkzGetPoint{E}
\tkzDefBarycentricPoint(c=1,n=1,f=1) \tkzGetPoint{F}
\tkzDefBarycentricPoint(d=1,n=1,f=1) \tkzGetPoint{G}

\tkzLabelPoints[anchor=center,font=\scriptsize](A,B,C,D,E,F,G)
\end{tikzpicture}
\end{document} 

enter image description here


While you are waiting for a tkz-euclide answer to be written up, here's a way to solve the same problem in Metapost. Plain MP provides a center macro that find the centre of the bounding box of any given closed path, which is usually the wrong place if your path is a triangle. Here I've defined a simple centroid macro that will find the centroid of a triangular path, that gives a better place for a label.

enter image description here

\RequirePackage{luatex85}
\documentclass[border=5mm]{standalone}
\usepackage{luamplib}
\begin{document}
\mplibtextextlabel{enable}
\begin{mplibcode}
vardef centroid primary trig_path =
    save a,b,c,m; 
    pair a,b,c,m; 
    a = point 0 of trig_path;
    b = point 1 of trig_path;
    c = point 2 of trig_path;
    m = whatever [a, 1/2[b,c]] = whatever [b, 1/2[a,c]]; m
enddef;
beginfig(1);
    path A,B,C,D,E,F,G, t[];

    C = unitsquare scaled 100 rotated 15;
    D = C shifted point 1 of C;
    B = C shifted point 3 of D;

    t0 = (point 0 of C -- point 1 of C -- point 3 of C -- cycle);
    t1 = t0 rotated -90;
    A = t0 shifted point 3 of B;
    E = t1 shifted point 2 of D;
    F = t1 shifted point 0 of D;

    t2 = origin -- point 2 of C -- point 2 of C rotated -60 -- cycle;
    G = t2 shifted point 1 of F;

    forsuffixes @=A,B,C,D,E,F,G:
        draw @;
        label(str @, if length @=3: centroid else: center fi @);
    endfor

endfig;
\end{mplibcode}
\end{document}

Compile this with lualatex to get the luamplib support.

Tags:

Tkz Euclide