9

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

user10
  • 315

4 Answers4

8

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

Gonzalo Medina
  • 505,128
5

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

\documentclass[tikz]{standalone}
\begin{document}
\foreach \angle in {0,1,...,89}{
  \begin{tikzpicture}
    \coordinate (center) at (2,3);
    \def\radius{3cm}
    \fill[white] (center) +(-\radius,-\radius) rectangle +(\radius,\radius);
    \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}
Paul Gaborit
  • 70,770
  • 10
  • 176
  • 283
5

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

Alain Matthes
  • 95,075
0

Save the earth by reducing the number of used keystrokes.

\documentclass[pstricks]{standalone}

\begin{document}
\foreach \ang in {0,15,...,345}{%
\begin{pspicture}(-4,-4)(4,4)
    \pscustom[fillstyle=eofill,fillcolor=green]
    {
        \pscircle{3}
        \rotate{\ang}
        \psframe(3;-135)(3;45)
    }
\end{pspicture}}
\end{document}

enter image description here