5

How can I draw a tikzpicture like the one below? I'm having trouble drawing both the rectangle (display) and rounded rectangle (earpiece speaker) holes. My idea is to make them completely transparent so that the background color is seen through them.

enter image description here

This is my code:

\documentclass{article}
\usepackage{pgf,tikz}
\tikzset{mystyle/.style={shape=rectangle,rounded corners,fill=white}}
\begin{document}
 \begin{tikzpicture}[scale=1]
 \fill[blue](-5,-5)rectangle(5,5); % background
 \filldraw[fill=white,rounded corners=0.5cm](-2,-4)rectangle(2,4) % phone case
    [sharp corners](-1.75,-3)rectangle(1.75,3) % display
    (0,-3.5)circle(0.25cm) % button
   [rounded corners=0.125cm](-0.5,3.325)rectangle(0.5,3.625); %earpiece speaker
 % Picture labels
 \draw[ultra thick](0,0)--(-30:4)node[mystyle]{Not blue}[ultra thick]--(0,3.5);
 \draw[ultra thick](0,-3.5)--(210:4)node[mystyle]{Blue};
 \end{tikzpicture}
\end{document}

And this is the result:

enter image description here

  • You can use even odd rule as shown by @marmot, but you can also invert the circle orientation by {[xscale=-1]circle(0.25cm)}. Here xscale works because the coordinate is(0,...), in general it is more tricky. – Kpym Apr 19 '18 at 6:16
6

Use even odd rule.

\documentclass{article}
\usepackage{pgf,tikz}
\tikzset{mystyle/.style={shape=rectangle,rounded corners,fill=white}}
\begin{document}
 \begin{tikzpicture}[scale=1]
 \fill[blue](-5,-5)rectangle(5,5); % background
 \filldraw[fill=white,rounded corners=0.5cm,even odd rule](-2,-4)rectangle(2,4) % phone case
    [sharp corners](-1.75,-3)rectangle(1.75,3) % display
    (0,-3.5)circle(0.25cm) % button
   [rounded corners=0.125cm](-0.5,3.325)rectangle(0.5,3.625); %earpiece speaker
 % Picture labels
 \draw[ultra thick](0,0)--(-30:4)node[mystyle]{Now blue}[ultra thick]--(0,3.5);
 \draw[ultra thick](0,-3.5)--(210:4)node[mystyle]{Blue};
 \end{tikzpicture}
\end{document}

enter image description here

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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