Graphics, Figures & TablesShading a circle

Information and discussion about graphics, figures & tables in LaTeX documents.
Post Reply
coachbennett1981
Posts: 274
Joined: Fri Feb 05, 2010 10:15 pm

Shading a circle

Post by coachbennett1981 »

I am trying to shade the outside circle red and leave the "unit circle" alone. The circle has center )=O(0,0), but it is erased because I filled it white. Is there a better way to shade so I don't lose the center of the circle.




Code: Select all



	\begin{tikzpicture}[scale=2]
		\coordinate [label=left:$O$] (O) at (0,0);
		\coordinate [label=right:$$] (A) at (1,0);
		\coordinate [label=right:$$] (B) at (2,0);
		\node[draw,circle through=(A)] at (O){};
		\node[draw,circle through=(B)] at (O){};
		\filldraw[black](A) circle (1pt);
		\filldraw[black](B) circle (1pt);
		\filldraw[black](O) circle (1pt);
	\draw[fill,color=red](0,0) circle (2cm and 2cm);
	\draw[fill,color=white](0,0) circle (1cm and 1cm);
	\draw (O)--(A)--(B);
	\end{tikzpicture}


Nick
Last edited by coachbennett1981 on Tue Jun 29, 2010 3:05 am, edited 1 time in total.

Recommended reading 2024:

LaTeXguide.org • LaTeX-Cookbook.net • TikZ.org

NEW: TikZ book now 40% off at Amazon.com for a short time.

shadgrind
Posts: 140
Joined: Thu Jul 16, 2009 12:39 am

Shading a circle

Post by shadgrind »

Try this:

Code: Select all

\begin{tikzpicture}[scale=2]
\coordinate [label=left:$O$] (O) at (0,0);
\coordinate [label=above left:$A$] (A) at (1,0);
\coordinate [label=right:$B$] (B) at (2,0);
\filldraw[fill=red] (B) arc (0:360:2) (A) arc (360:0:1);
\filldraw[black](A) circle (1pt);
\filldraw[black](B) circle (1pt);
\filldraw[black](O) circle (1pt);
\draw (O)--(A)--(B);
\end{tikzpicture}
System: TeX Live 2012, Fedora 18 x86_64, GNU Emacs 24.2
torbjorn t.
Posts: 162
Joined: Wed Jun 17, 2009 10:18 pm

Shading a circle

Post by torbjorn t. »

Remember that the elements of the drawing are drawn in the order they appear in the code, so when the line of coding that fills the circles are drawn last, they will cover the parts that have been drawn before. By simply moving them to the top of the code, you get that you want:

Code: Select all

\begin{tikzpicture}[scale=2]
  \draw[fill,color=red](0,0) circle (2cm);
  \draw[fill,color=white](0,0) circle (1cm);
  \coordinate [label=left:$O$] (O) at (0,0);
  \coordinate [label=above right:$A$] (A) at (1,0);
  \coordinate [label=right:$B$] (B) at (2,0);
  \node[draw,circle through=(A)] at (O){};
  \node[draw,circle through=(B)] at (O){};
  \filldraw[black](A) circle (1pt);
  \filldraw[black](B) circle (1pt);
  \filldraw[black](O) circle (1pt);
  \draw (O)--(A)--(B);
\end{tikzpicture}
Note that you do not have to specify the radius two times when drawing circles. You only have to do that if you want an ellipse.


Another way is to use the pgfonlayer-environment (see section 65 in pgfmanual):

Code: Select all

\begin{tikzpicture}[scale=2]
  \coordinate [label=left:$O$] (O) at (0,0);
  \coordinate [label=above right:$A$] (A) at (1,0);
  \coordinate [label=right:$B$] (B) at (2,0);
  \node[draw,circle through=(A)] at (O){};
  \node[draw,circle through=(B)] at (O){};
  \filldraw[black](A) circle (1pt);
  \filldraw[black](B) circle (1pt);
  \filldraw[black](O) circle (1pt);
  \draw (O)--(A)--(B);
  \begin{pgfonlayer}{background}
    \draw[fill,color=red](0,0) circle (2cm);
    \draw[fill,color=white](0,0) circle (1cm);
  \end{pgfonlayer}
\end{tikzpicture}
coachbennett1981
Posts: 274
Joined: Fri Feb 05, 2010 10:15 pm

Re: Shading a circle

Post by coachbennett1981 »

Thank you so much!
Post Reply