4

I want to draw the moon phases and started with this code

\documentclass{article}
\usepackage{tikz}
\usepackage{pgfplots}
\newcommand*\circled[1]{\tikz[baseline=(char.base)]{\node[shape=circle,draw,inner sep=2pt,fill=white] (char) {#1};}}
\begin{document}
    \begin{tikzpicture}
        \draw (0cm,0cm) circle(4cm);
        \foreach \x in {0,45,...,360} {
            \filldraw[fill=white] (\x:4cm) circle(0.4cm);
            \filldraw[fill=white] (\x:5cm) circle(0.4cm);
            \draw[fill=black!70] (\x:4cm)-- +(0,0.4cm) arc (90:-90:0.4cm) -- cycle;
            }
        \node[inner sep=0pt] at (0,0)   {\includegraphics[height=5cm]{aarde2}};
        \foreach \x/\xtext in {
            0/\circled{5},
            45/\circled{6},
            90/\circled{7},
            135/\circled{8},
            180/\circled{1},
            225/\circled{2},
            270/\circled{3},
            315/\circled{4}}
            \draw (\x:3.3cm) node {\tiny \xtext};
\end{tikzpicture}
\end{document}

resulting in this:

enter image description here

(picture of the earth is not uploaded)

My question is: how can I draw squares around the white circles. As the circles have a radius of 8mm, the squares should have a side of 1cm.

(the purpose of the white circles is to color to realistic view of the moon phase if you stand on that position.

Torbjørn T.
  • 206,688
  • 1
    \draw (\x:5cm) +(-.5cm,-.5cm) rectangle +(.5cm,.5cm); Also, you really don't need \circled here. – John Kormylo Sep 03 '16 at 15:21
  • Great! But why do I not need \circled? – Arne Timperman Sep 03 '16 at 15:29
  • 1
    @ArneTimperman You're kind loading another Tikzpicture with that command... Just add a [count=\n, evaluate=\n as \angle using int(135+45*\n)] in the foreach statement (between \xtext ... in), then inside of it you can write \node[circle, inner sep=1mm] at (\angle:3cm) {\n};. This should work. – Alenanno Sep 03 '16 at 15:33
  • Are you sure that the drawn moon phases are correct? (From a Physicists' point they aren't ) -- where is the position of the Sun here? –  Sep 03 '16 at 15:56
  • @ChristianHupfer the moon phases are not drawn yet. The sun is on the left and it is always the left side of the moon the catches the sun. The right side is dark. The boxes with the circles is for the kids to mark the part they will view on that place on earth. (I still have the color half of the earth dark ;-) ) – Arne Timperman Sep 03 '16 at 16:57

2 Answers2

5

Replacing \circled:

\documentclass{article}
\usepackage{tikz}
\usepackage{pgfplots}
\begin{document}
    \begin{tikzpicture}
        \draw (0cm,0cm) circle(4cm);
        \foreach \x in {0,45,...,360} {
            \filldraw[fill=white] (\x:4cm) circle(0.4cm);
            \filldraw[fill=white] (\x:5cm) circle(0.4cm);
            \draw (\x:5cm) +(-.5cm,-.5cm) rectangle +(.5cm,.5cm);
            \draw[fill=black!70] (\x:4cm)-- +(0,0.4cm) arc (90:-90:0.4cm) -- cycle;
            }
        \node[inner sep=0pt] at (0,0)   {\includegraphics[height=5cm]{aarde2}};
        \foreach \x/\xtext in {
            0/5,
            45/6,
            90/7,
            135/8,
            180/1,
            225/2,
            270/3,
            315/4}
            \draw (\x:3.3cm) node[shape=circle,draw,inner sep=2pt,fill=white] {\tiny \xtext};
\end{tikzpicture}
\end{document}
John Kormylo
  • 79,712
  • 3
  • 50
  • 120
5

It's the wrong way around, but anyway...

\documentclass[tikz,border=5]{standalone}
\colorlet{color 0}{white}
\colorlet{color 1}{black}
\begin{document}
\begin{tikzpicture}
\filldraw [fill=color 0] circle [radius=1.5];
\filldraw [fill=color 1] (90:1.5) arc (90:270:1.5) -- cycle;  
\draw (0, 0) circle [radius=4];
\foreach \phase [count=\i,evaluate={\a=\i*45-45; \j=int(mod(\i-1, 4));
  \ca=int(\i>4); \cb=int(\i<5);}]
    in {New moon, Waxing crescent, First quarter, Waxing gibbous,
        Full moon, Waning gibbous, Last quarter, Waning crescent}{
  \filldraw [fill=color 0] (\a:4) circle [radius=0.5];
  \filldraw [fill=color 1] (\a:4) ++(90:0.5) arc (90:270:0.5) -- cycle;
  \begin{scope}[shift=(\a:6), scale=0.75]
    \fill [gray!50] (-1.125, -1.125) rectangle (1.125, 1.125);
    \fill [fill=color \cb] circle [radius=1];
    \ifcase\j
    \or
      \fill [color \ca] (90:1) arc (45:-45:sqrt 2) arc (-90:90:1) -- cycle;
    \or
      \fill [color \ca] (90:1) arc (90:-90:1) -- cycle;
    \or
      \fill [color \ca] (90:1) arc (135:225:sqrt 2) arc (-90:90:1) -- cycle;
    \fi
    \node [anchor=(\i>4)*180-90, align=center, text width=5em]
      at ({(\i>4)*180+90}:1.125) {\phase};
  \end{scope}
  \node [circle, draw] at (\a:3) {\i};
}
\end{tikzpicture} 
\end{document}

enter image description here

Mark Wibrow
  • 70,437