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.


    \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)});                              

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
  • 2
    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

shading via clipping, even odd filling and calculations

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

    \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)});
      \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);

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
  • 1
    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
  • 1
    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):


  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];


which gives:

enter image description here


Using the closed form solution for intersections:



The circle is given by
x^2 + y^2 = r^2
and the line by 
y = ax + b
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
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.
For a tangent point one can assume 
a^2 r^2 - b^2 + r^2 = 0 \quad.

    \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
    %find intersection of ab and circle.  Store back into (\xb,\yb)
    \coordinate (bint) at (\xb pt,\yb pt);
    %find intersection of ac and circle.  Store back into (\xc,\yc)
    \coordinate (cint) at (\xc pt,\yc pt);
      \clip (bint) -- (a) -- (cint) -- cycle;
      \fill [blue!25, even odd rule, blend mode=multiply] (bint) -- (a) -- (cint) -- cycle (o) circle (1);

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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