5
\begin{tikzpicture}

\coordinate (1) at (0,0);
\coordinate (2) at (2,0);
\coordinate (3) at (2,2);
\coordinate (4) at (0,2);
\coordinate (5) at (0,0);
\coordinate (6) at ($(1)!.5!(2)$);
\coordinate (7) at ($(2)!.5!(3)$);
\coordinate (8) at ($(3)!.5!(4)$);
\coordinate (9) at ($(4)!.5!(1)$);


\foreach \i in {1,2}
\fill (\i) circle (1pt) node [below] {\tiny \i};
\foreach \i in {3,4}
\fill (\i) circle (1pt) node [above] {\tiny \i};
\foreach \i in {6} 
\fill (\i) circle (1pt) node [below] {\tiny 5};
\foreach \i in {7}
\fill (\i) circle (1pt) node [right] {\tiny 6};
\foreach \i in {8}
\fill (\i) circle (1pt) node [above] {\tiny 7};
\foreach \i in {9}
\fill (\i) circle (1pt) node [left] {\tiny 8};        

\draw (1)--(2)--(3)--(4)--(5);

\end{tikzpicture}

The above code for right side square I want to make the mid point which is giving in left square point(9)? enter image description here

Micky K
  • 1,457
  • You've used \coordinate (9) at ($(4)!.5!(1)$);. Do you understand what that does? (I suspect you wanted \coordinate (9) at ($(1)!.5!(3)$);.) Also, have you noticed that you've placed the 5 coordinate in the origin? And the coordinates 6,7,8,9 is located where the numbers 5,6,7,8, respectively, are placed. – Torbjørn T. May 08 '14 at 05:15

2 Answers2

7

You can compute the mid way of point (9) via (1)!0.5!(3), which denote a point 0.5 from the path form (1) to (3). I have also reduced it to a single \foreach:

enter image description here

Code:

documentclass{article}

\usepackage{tikz} \usetikzlibrary{calc} \begin{document}

\begin{tikzpicture}

\coordinate (1) at (0,0); \coordinate (2) at (2,0); \coordinate (3) at (2,2); \coordinate (4) at (0,2); \coordinate (5) at ($(1)!.5!(2)$); \coordinate (6) at ($(2)!.5!(3)$); \coordinate (7) at ($(3)!.5!(4)$); \coordinate (8) at ($(1)!.5!(4)$); \coordinate (9) at ($(1)!.5!(3)$);

\foreach \i/\Position in {1/below, 2/below, 3/above, 4/above, 5/below, 6/right, 7/above, 8/left, 9/above right} { \fill (\i) circle (1pt) node [\Position] {\tiny \i}; }

\draw (1)--(2)--(3)--(4)-- cycle;

\end{tikzpicture} \end{document}

Peter Grill
  • 223,288
6

You can also use the othogonal coordinates.

\coordinate (9) at (8-|5);   %%% <--- new

(8-|5) means take y-coordinates same as that of node (8) and x coordinates as that of node (5).

Also, the \foreach loop can be refined.

\documentclass[tikz,varwidth]{standalone}%

\usetikzlibrary{calc,positioning}
\begin{document}

\begin{tikzpicture}

\coordinate (1) at (0,0);
\coordinate (2) at (2,0);
\coordinate (3) at (2,2);
\coordinate (4) at (0,2);
\coordinate (5) at ($(1)!.5!(2)$);
\coordinate (6) at ($(2)!.5!(3)$);
\coordinate (7) at ($(3)!.5!(4)$);
\coordinate (8) at ($(4)!.5!(1)$);
\coordinate (9) at (8-|5);   %%% <--- new


\foreach \i/\position in {1/below,2/below,3/above,4/above,5/below,6/right,7/above,8/left,9/above right}
\fill (\i) circle (1pt) node [\position] {\tiny \i};

\draw (1)--(2)--(3)--(4)--cycle;

\end{tikzpicture}
\end{document}

enter image description here

Another simple way is to use a rectangular node.

\documentclass[tikz,varwidth]{standalone}%

\usetikzlibrary{calc,positioning}
\begin{document}

\begin{tikzpicture}

\node[draw,minimum height=2cm,minimum width=2cm] (a) at (1,1) {};

\foreach \i/\a/\position in 
{south west/1/below,south east/2/below,north east/3/above,north west/4/above,
south/5/below,east/6/right,north/7/above,west/8/left,center/9/above right} {%
  \fill (a.\i) circle (1pt) node [\position] {\tiny \a}; 
}
\end{tikzpicture}
\end{document}
  • You could also place the 5 coordinate where the 5 node is, and modify 6,7,8 accordingly, then a single loop is enough. Oh, and use \draw (1)--(2)--(3)--(4)--cycle; of course. – Torbjørn T. May 08 '14 at 05:40
  • @TorbjørnT. You are right. I was very lazy. Corrected now. Thanks. –  May 08 '14 at 05:54