Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

How can I draw a square whose side is 6 inscribed in a circle and make the program display the shaded region between the square and the circle

share|improve this question

3 Answers 3

up vote 8 down vote accepted

Here's a simple option; the value for \side controls half the side of the square:

\documentclass{article}
\usepackage{tikz}

\def\side{3}

\begin{document}

\begin{tikzpicture}
\draw[fill=yellow!40] (0,0) circle (\side*1.4142);
\draw[fill=white] (-\side,-\side) rectangle (\side,\side);
\end{tikzpicture}

\end{document}

enter image description here

Another option using the intersections library; this time the value for \radius controls the radius of the circle:

\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{intersections}

\def\radius{2}

\begin{document}

\begin{tikzpicture}
\draw[name path=circle,fill=yellow!40] (0,0) circle (\radius);
\path[name path=line1] (-\radius,-\radius) -- (\radius,\radius);
\path[name path=line2] (-\radius,\radius) -- (\radius,-\radius);
\filldraw[draw=black,fill=white,
  name intersections={of=circle and line1,by={a,b}},
  name intersections={of=circle and line2,by={c,d}}] 
  (a) -- (c) -- (b) -- (d) -- cycle;
\end{tikzpicture}

\end{document}

enter image description here

share|improve this answer
    
thanks; I want to make it small. Any idea? –  user10 Nov 6 '12 at 3:33
    
@user10 sure: you can use a scaling factor: \begin{tikzpicture}[scale=0.5]...\end{tikzpicture}. –  Gonzalo Medina Nov 6 '12 at 3:35
    
@user10 in my updated answer you find another option: you can simply change the value of \radius (initially set to 3). –  Gonzalo Medina Nov 6 '12 at 3:39
    
how to use grey instead of yellow? –  user10 Nov 6 '12 at 3:56
    
@user10 in my code, instead of fill=yellow!40, you can use fill=gray!40 (which means 40% gray and 60% white) or any other shade of the desired color. –  Gonzalo Medina Nov 6 '12 at 4:00

Here is a solution using a nonzero rule fill. The parameters are:

  • the center coordinate,
  • the \radius distance,
  • the \angle of square into the circle.

example

\documentclass{standalone}
\usepackage{tikz}
\begin{document}
\begin{tikzpicture}
  % parameters
  \coordinate (center) at (2,3);
  \def\radius{3cm}
  \def\angle{15}
  %
  \path[fill=lime,draw=black,thick]
  (center) circle (\radius)
  +(\angle:\radius) --
  +(\angle-90:\radius) --
  +(\angle-180:\radius) --
  +(\angle-270:\radius) --
  cycle;
\end{tikzpicture}
\end{document}

And just for fun (credits to Qrrbrbirlbel):

animated example

share|improve this answer
1  
Just for fun. –  Qrrbrbirlbel Nov 6 '12 at 7:29
    
@Qrrbrbirlbel Thanks. I add this picture to my answer... –  Paul Gaborit Nov 6 '12 at 7:33

Some complements :

With the code of Paul, you can use even odd rule if you draw the square with anticlockwise

\documentclass{standalone}
\usepackage{tikz}
\begin{document}
\begin{tikzpicture}
  % parameters
  \coordinate (center) at (2,3);
  \def\radius{3cm}
  \def\angle{15}
  %
  \path[fill=lime,draw=black,thick,even odd rule]
  (center) circle (\radius)
  +(\angle:\radius) --
  +(\angle+90:\radius) --
  +(\angle+180:\radius) --
  +(\angle+270:\radius) --
  cycle;
\end{tikzpicture} 

\end{document} 

If you want to fill the area between two circles, you have the next possibilites

\begin{tikzpicture}
\draw[fill=lime,even odd rule] (0,0) circle (2cm) circle (1cm) ;
\end{tikzpicture}   

\begin{tikzpicture}
\draw[fill=lime,even odd rule] (0,0) circle (2cm) (0:1) arc (0:360:1) ;
\end{tikzpicture} 

\begin{tikzpicture}
\draw[fill=lime] (0,0) circle (2cm) (0:1) arc (0:-360:1) ;
\end{tikzpicture}

enter image description here

About the construction

You can use Tikz with tkz-euclide. You give two points like you want with tikz or tkz-euclide with cartesian coordinates or polar coordinates. Then you define the square and you get the point C and you can define the circle with diameter A and C

\documentclass{scrartcl}
\usepackage{tkz-euclide} 
\usetkzobj{all} 
\begin{document}

\begin{tikzpicture} 
  \tkzDefPoint(2,1){A}
  \tkzDefPoint(5,2){B} 
  \tkzDefSquare(A,B) \tkzGetFirstPoint{C}
  \tkzDrawCircle[diameter,fill=lime](A,C)
    \tkzDrawSquare[fill=white](A,B)   
\end{tikzpicture}

\end{document} 

enter image description here

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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