11

I am trying to draw the following picture but I have some difficulties. I code the following:

\documentclass{article}
\usepackage{xcolor,pict2e}% to allow any radius
\usepackage{tikz}
\begin{document}
\begin{tikzpicture}
\draw[very thick,<->] (-6,0) -- (6,0)node[anchor=north west] {$x$};
\draw (0,0)[red,very thick,dashed] circle (1cm and 1cm);
\draw (0,0) [red,very thick,dashed] circle (2.cm and 2.cm);
 \draw[thick,-] (0.7,0.7) -- (1.4,1.5);
  \draw[thick,-] (-0.7,0.7) -- (-1.4,1.5);
   \draw[thick,-] (0.7,-0.7) -- (1.4,-1.5);
 \draw[thick,-] (-0.7,-0.7) -- (-1.4,-1.5);
\end{tikzpicture}
\end{document}

enter image description here

1
  • 2
    What is pict2e supposed to do?
    – cfr
    Apr 20, 2015 at 23:30

4 Answers 4

10

Time to use pic

\documentclass{article}
%\usepackage{xcolor,pict2e}% to allow any radius
\usepackage{tikz}
\tikzset{pics/myarc/.style args={inner #1 and outer #2}{
  code={
    \draw[very thick,dashed, #1] (1,0) arc (0:90:1cm);
    \draw[very thick] (0,1) -- (0,2);
    \draw[very thick,dashed, #2] (0,2) arc (90:0:2cm);
    \draw[very thick] (2,0) -- (1,0);
  }
}}
\begin{document}
\begin{tikzpicture}
\draw[very thick,<->] (-6,0) -- (6,0)node[anchor=north west] {$x$};
\pic [draw,rotate=-45]      {myarc={inner red and outer blue}};
\pic [draw, rotate=45]      {myarc={inner olive and outer magenta}};
\pic [draw, rotate=135]      {myarc={inner blue and outer teal}};
\pic [draw, rotate=225]      {myarc={inner cyan and outer orange}};
\end{tikzpicture}
\end{document}

enter image description here

With filling:

\documentclass{article}
%\usepackage{xcolor,pict2e}% to allow any radius
\usepackage{tikz}
\tikzset{pics/myarc/.style args={inner #1 and outer #2 with fill #3}{
  code={
    \draw[very thick,dashed, #1] (1,0) arc (0:90:1cm);
    \draw[very thick] (0,1) -- (0,2);
    \draw[very thick,dashed, #2] (0,2) arc (90:0:2cm);
    \draw[very thick] (2,0) -- (1,0);
    \fill[#3] (1,0) arc (0:90:1cm) -- (0,2) arc (90:0:2cm) -- cycle;
  }
}}
\begin{document}
\begin{tikzpicture}
\draw[very thick,<->] (-6,0) -- (6,0)node[anchor=north west] {$x$};
\pic [draw,rotate=-45]      {myarc={inner red and outer blue with fill red!20}};
\pic [draw, rotate=45]      {myarc={inner olive and outer magenta with fill green!20}};
\pic [draw, rotate=135]      {myarc={inner blue and outer teal with fill olive!30}};
\pic [draw, rotate=225]      {myarc={inner cyan and outer orange with fill brown!20}};
\end{tikzpicture}
\end{document}

enter image description here

Now the confusion as the code and picture in OP are not the same. What is needed actually?

\documentclass{article}
%\usepackage{xcolor,pict2e}% to allow any radius
\usepackage{tikz}
\tikzset{pics/myarc/.style args={#1}{
  code={
    \draw[very thick,#1] (2,0) arc(0:90:2cm);
  }
}}
\begin{document}
\begin{tikzpicture}
\draw[very thick,<->] (-6,0) -- (6,0)node[anchor=north west] {$x$};
\foreach \rot/\col in {-45/green,45/blue,135/red,225/blue}{
\pic [draw,rotate=\rot]      {myarc={\col}};
}
\end{tikzpicture}
\end{document}

enter image description here

1
  • A \foreach loop to draw those \pics will be neat. Left as an exercise though.
    – user11232
    Apr 20, 2015 at 23:14
9

This is not very elegant, but I think it gives the result you are after:

\documentclass{standalone}
\usepackage{xcolor}
\usepackage{tikz}

\begin{document}
\begin{tikzpicture}
\newcommand\rad{2} % circle radius
\draw[very thick,<->] (-3,0) -- (3,0)node[anchor=north west] {$x$};
% red
\draw[red,thick] (-\rad,0) arc (180:135:\rad);
\draw[red,thick] (-\rad,0) arc (-180:-135:\rad);
% green
\draw[green!50!black,thick] (\rad,0) arc (0:45:\rad);
\draw[green!50!black,thick] (\rad,0) arc (0:-45:\rad);
% blue top
\draw[blue,thick] (0,\rad) arc (90:45:\rad);
\draw[blue,thick] (0,\rad) arc (90:135:\rad);
% blue bottom
\draw[blue,thick] (0,-\rad) arc (-90:-45:\rad);
\draw[blue,thick] (0,-\rad) arc (270:225:\rad);
\end{tikzpicture}

\end{document}

Tikz circle

3
  • Too amazing,,, Many thanks guys for your help.
    – Kim
    Apr 20, 2015 at 23:22
  • 1
    You could use a \foreach to reduce code duplication.
    – jub0bs
    Apr 21, 2015 at 12:27
  • Indeed. I really should learn to use loops in tikz :)
    – onewhaleid
    Apr 21, 2015 at 23:03
6

It's not possible to change line color in the middle of a line, so four arcs are necessary.

\documentclass{article}
\usepackage{tikz}
\begin{document}
\begin{tikzpicture}
\draw[very thick,<->] (-6,0) -- (6,0)node[anchor=north west] {$x$};

\foreach \i/\j in {-45/green, 45/purple, 135/red, 225/blue}
    \draw[very thick,\j] (\i:2cm) arc [start angle=\i,radius=2cm, delta angle=90];
\end{tikzpicture}
\end{document}

enter image description here

4

And in Metapost, using the subpath operation and the fact that a fullcircle path has 8 "points" on it.

enter image description here

prologues := 3;
outputtemplate := "%j%c.eps";
beginfig(1);
draw (left--right) scaled 1in withcolor .8 white;
path c; 
c = fullcircle scaled 1in;
draw subpath (1,3) of c withcolor blue+red;
draw subpath (3,5) of c withcolor red;
draw subpath (5,7) of c withcolor blue;
draw subpath (-1,1) of c withcolor green;
for t=0 upto 7: label(decimal t, point t of c scaled 1.2); endfor
endfig;
end.
1
  • 1
    In such cases, you may be interested by the freelabel macro of Metafun. input mp-tool.mpivfreelabel(decimal t, point t of c, origin); The offset can not be upset by a change of global scale then, it only depends on the freelabeloffset parameter (default 3 bp). Apr 21, 2015 at 18:25

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .