# Shading between a circle and two lines

I have drawn the unit circle and some tangent lines to the circle. I now wish to shade the area between the two lines bounded by the circle. Here is the tex that I have already written.

\documentclass{article}
\usepackage{tikz}

\begin{document}
\begin{tikzpicture}[domain=0:4]
\draw (0,0) circle (1cm);
\draw ({-2.5},{sqrt(2)*(-sqrt(7)+sqrt(13))*(-2.5)-sqrt(13)+2*sqrt(7)}) -- ({1/sqrt(2)},{sqrt(7)});
\draw (1.25, {-sqrt(2)*(sqrt(7)+sqrt(13))*(1.25)+sqrt(13)+2*sqrt(7)}) -- ({1/sqrt(2)},{sqrt(7)});
\end{tikzpicture}
\end{document}


I have looked at other questions that deal with shading but none have to do with the circle object. Possibly I could use the fact it can be described by sqrt(1-x^2)?

• Hi and welcome to the site. Please don't post code fragments. It's much more helpful to post complete compilable documents that show what you're doing, including which TikZ libraries you are loading etc. I've just been asked to write a minimal example, what is that? – Alan Munn Jul 26 '16 at 22:36
• Ok, I tried to write a minimal example. I hope it will help make the question easier to answer and improve its form. – Will Sherwood Jul 26 '16 at 22:40
• Related: Shading areas in TikZ. – Alan Munn Jul 26 '16 at 22:50
• If you know the polar coordinates of both tangent points (say angles are 5 and 130), you can draw the arc and fill : \fill (5:1) arc (5:130:1) -- (s) -- cycle where (s) is the intersection of both tangents. – Christoph Frings Jul 26 '16 at 22:56

At ridiculous computational cost, you can calculate the points where the circle intersects with the lines and use a clipping together with the even odd filling rule to produce

This uses the intersections library, a scoped \clip and the even odd rule to create an unholy combination.

\documentclass[border=10pt,multi,tikz]{standalone}
\usetikzlibrary{intersections}
\begin{document}
\begin{tikzpicture}[domain=0:4]
\draw [name path=my circle] (0,0) coordinate (o) circle (1cm);
\draw [name path=first line] ({-2.5},{sqrt(2)*(-sqrt(7)+sqrt(13))*(-2.5)-sqrt(13)+2*sqrt(7)}) -- ({1/sqrt(2)},{sqrt(7)}) coordinate (a);
\draw [name path=second line] (a) -- (1.25, {-sqrt(2)*(sqrt(7)+sqrt(13))*(1.25)+sqrt(13)+2*sqrt(7)});
\begin{scope}
\clip [name intersections={of=first line and my circle, name=i}, name intersections={of=second line and my circle, name=j}] (i-1) -- (a) -- (j-1) -- cycle;
\fill [blue!25, even odd rule, blend mode=multiply] (i-1) -- (a) -- (j-1) -- cycle (o) circle (1);
\end{scope}
\end{tikzpicture}
\end{document}


There is surely a less expensive way!

• Do you mean it ought also to be verified somehow? Did you try running it before judging it not that ridiculous? If not, you might want to reconsider and try it. – cfr Jul 27 '16 at 2:39
• You changed your comment while I was typing. What is wrong with the fill? – cfr Jul 27 '16 at 2:40
• It finds too many intersections, I think. – cfr Jul 27 '16 at 2:57
• Interesting, unless they changed the algorithm it shouldn't take that long. – John Kormylo Jul 27 '16 at 2:58
• It found 3 intersections for the first line, but a line and a circle should have at most 2. – John Kormylo Jul 27 '16 at 3:03

For comparison, here is a Metapost solution (I am showing ConTeXt code, but it will work with standalone metapost or LaTeX + gmp):

\starttext

\startMPpage[offset=2mm]
z1 = (-2.5,sqrt(2)*(-sqrt(7)+sqrt(13))*(-2.5)-sqrt(13)+2*sqrt(7))*cm;
z2 = (1/sqrt(2), sqrt(7))*cm;
z3 = (1.25, -sqrt(2)*(sqrt(7)+sqrt(13))*(1.25)+sqrt(13)+2*sqrt(7))*cm;

path p, q;
path l[];

p  := fullcircle scaled 2cm;
l1 := z1 -- z2;
l2 := z2 -- z3;

pair t[];

t1 := p intersectiontimes l1;
t2 := p intersectiontimes l2;

draw p;
draw z1 -- z2;
draw z2 -- z3;

q := subpath (ypart t1, 1) of l1 .. subpath (0, ypart t2) of l2
.. subpath(xpart t2, xpart t1) of p .. cycle;

fill q withcolor 0.5[blue,white];

\stopMPpage
\stoptext


which gives:

Using the closed form solution for intersections:

\documentclass[border=2pt]{standalone}
\usepackage{mathtools}
\usepackage{tikz}

\begin{document}
\begin{minipage}{4in}
The circle is given by
$$x^2 + y^2 = r^2$$
and the line by
$$y = ax + b$$
where
$$a = \frac{y_2-y_1}{x_2-x_1} \quad\textrm{and}\quad b = y_1 - ax_1 = y_2 - ax_2$$
for any two points $(x_1,y_1)$ and $(x_2,y_2)$ on the line.

Substituting for $y$ using (2) into (1) we get
$$x^2 + a^2x^2 + 2abx + b^2 = r^2$$
which has the solution
\begin{align}
x &= \frac{-ab}{a^2+1} \pm \frac{\sqrt{a^2 b^2 - (a^2+1)(b^2-r^2)}}{a^2+1}\notag\\
&= \frac{-ab}{a^2+1} \pm \frac{\sqrt{a^2 r^2 - b^2 + r^2}}{a^2+1} \quad.
\end{align}
For a tangent point one can assume
\begin{equation*}
a^2 r^2 - b^2 + r^2 = 0 \quad.
\end{equation*}

\begin{tikzpicture}
\draw (0,0) coordinate (o) circle (1cm);
\coordinate (a) at ({1/sqrt(2)},{sqrt(7)});
\coordinate (b) at ({-2.5},{sqrt(2)*(-sqrt(7)+sqrt(13))*(-2.5)-sqrt(13)+2*sqrt(7)});
\coordinate (c) at (1.25, {-sqrt(2)*(sqrt(7)+sqrt(13))*(1.25)+sqrt(13)+2*sqrt(7)});
\draw (a) -- (b);
\draw (a) -- (c);
% find intersectons
\pgfpointanchor{a}{center}\pgfgetlastxy{\xa}{\ya}%
\pgfpointanchor{b}{center}\pgfgetlastxy{\xb}{\yb}%
\pgfpointanchor{c}{center}\pgfgetlastxy{\xc}{\yc}%
%find intersection of ab and circle.  Store back into (\xb,\yb)
\pgfmathparse{(\ya-\yb)/(\xa-\xb)}\let\slope=\pgfmathresult
\pgfmathparse{\ya-\slope*\xa}\let\yint=\pgfmathresult
\pgfmathparse{-\slope*\yint/(\slope*\slope+1)}\let\xb=\pgfmathresult
\pgfmathparse{\slope*\xb+\yint}\let\yb=\pgfmathresult
\coordinate (bint) at (\xb pt,\yb pt);
%find intersection of ac and circle.  Store back into (\xc,\yc)
\pgfmathparse{(\ya-\yc)/(\xa-\xc)}\let\slope=\pgfmathresult
\pgfmathparse{\ya-\slope*\xa}\let\yint=\pgfmathresult
\pgfmathparse{-\slope*\yint/(\slope*\slope+1)}\let\xc=\pgfmathresult
\pgfmathparse{\slope*\xc+\yint}\let\yc=\pgfmathresult
\coordinate (cint) at (\xc pt,\yc pt);
\begin{scope}
\clip (bint) -- (a) -- (cint) -- cycle;
\fill [blue!25, even odd rule, blend mode=multiply] (bint) -- (a) -- (cint) -- cycle (o) circle (1);
\end{scope}
\end{tikzpicture}
\end{minipage}
\end{document}