Graphics, Figures & TablesShade intersection of two circles

Information and discussion about graphics, figures & tables in LaTeX documents.
Post Reply
stepec
Posts: 18
Joined: Fri Aug 06, 2010 11:36 pm

Shade intersection of two circles

Post by stepec »

I'm using tikz and for self-tutorials I'm trying to reproduce some diagrams. Here I want to shade just the intersection of the two circles. How do I do that?

Code: Select all

\documentclass[12pt,a4paper,oneside, leqno]{report}
%\usepackage{graphicx}
\usepackage{amsmath}
%\usepackage{indentfirst}
\usepackage{setspace}
\usepackage{tikz}

\setlength{\parindent}{0.0in}
\setlength{\parskip}{0.1in}

\begin{document}
\begin{tikzpicture}[thick]
\draw (0,0) circle (2cm);
\draw (3,-1) circle (2.5cm);
\draw (0,0) node {A};
\draw (3.2,-1) node {B};
\end{tikzpicture}

\end{document}
Incidentally, is there any way to "fill" with hatched lines or similar, rather than colour fill?

stepec
Last edited by stepec on Tue Aug 31, 2010 12:42 pm, 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.

localghost
Site Moderator
Posts: 9202
Joined: Fri Feb 02, 2007 12:06 pm

Shade intersection of two circles

Post by localghost »

See the pgf/tikZ manual [Section 14.7 — Clipping and Fading (Soft Clipping), p. 142f] for a concrete example. For filling with hatch lines you will need the »patterns« library (see Section 34 — Pattern Library, p. 296).


Best regards
Thorsten
stepec
Posts: 18
Joined: Fri Aug 06, 2010 11:36 pm

Shade intersection of two circles

Post by stepec »

Thorsten

Thanks: I have the user manual but didn't see this as I didn't think of looking for "clip" (which I don't completely follow). After a bit of messing about I've used the following code. It works (ie both circles are visible), but seems a bit clumsy. Can it be written more efficiently?

Code: Select all

\documentclass[12pt,a4paper,oneside, leqno]{report}
%\usepackage{graphicx}
\usepackage{amsmath}
%\usepackage{indentfirst}
\usepackage{setspace}

\setlength{\parindent}{0.0in}
\setlength{\parskip}{0.1in}



\usepackage{tikz}
\begin{document}

\begin{tikzpicture}[thick]

\draw(0,0) circle (1cm);
\draw(1,0) circle (1cm);
\draw [clip](0,0) circle (1cm);
\fill[red] (1,0) circle (1cm);


\end{tikzpicture}
Thanks in advance for any help

stepec
stepec
Posts: 18
Joined: Fri Aug 06, 2010 11:36 pm

Shade intersection of two circles

Post by stepec »

The following code appears o give me what I am looking for (ie two diagrams with some brief blurb to the right) although I suspect it is heavy-handed and there is probably a better way of handling it. However, I can't see how to get rid of the gigantic vertical space between the diagrams. In fact I can't even see where it comes from. Can anyone help in shrinking it down? (Using a negative vspace doesn't seem to help).

Code: Select all

\documentclass[12pt,a4paper,oneside, leqno]{report}
\usepackage{amsmath}
\usepackage{tikz}

\begin{document}
\begin{figure}
\begin{minipage}[b]{0.5\linewidth}
\begin{tikzpicture} [scale=0.75][thick]
\draw [fill=yellow](0,0) circle (2cm);
\draw [fill=yellow](3,-1) circle (2.5cm);
\draw (0,0) node {A};
\draw (0,0) circle (2cm);
\draw (3.2,-1) node {B};
\end{tikzpicture}
\end{minipage}
\hspace{0.5cm}
\begin{minipage}[b]{0.5\linewidth}
The shaded area represents \newline $A\cup B$
\vspace{1in}
\end{minipage}
\end{figure}
\begin{figure}
\begin{minipage}[b]{0.5\linewidth}
\begin{tikzpicture}[scale=0.75][thick]
\draw (0,0) circle (2cm);
\draw (3,-1) circle (2.5cm);
\draw (0,0) node {A};
\draw (3.2,-1) node {B};
\draw [clip](0,0) circle (2cm);
\fill[yellow] (3,-1) circle (2.5cm);
\end{tikzpicture}
\end{minipage}
\hspace{0.5cm}
\begin{minipage}[b]{0.5\linewidth}
The shaded area represents\\ $A\cap B$
\vspace{1in}
\end{minipage}
\end{figure}

\end{document}
stepec
User avatar
localghost
Site Moderator
Posts: 9202
Joined: Fri Feb 02, 2007 12:06 pm

Shade intersection of two circles

Post by localghost »

If two floats are placed on a separate page (so called float page) there are placed to fill the space consistently. If you don't like that, you can change the layout of such pages [1]. Note that this will be a global setting that effects all float pages.

[1] TeX Frequently Asked Quextions — Vertical layout of float pages
stepec
Posts: 18
Joined: Fri Aug 06, 2010 11:36 pm

Re: Shade intersection of two circles

Post by stepec »

Thanks. I didn't follow everything at the link, but when I adjusted some text the whole thing sorted itself, presumably because there was more than just the two figures on a page.

I still think my coding is probably ugly and inefficient, but at least I've got the desired result!

stepec
Post Reply