TransWikia.com

Tikz draw section of circle enclosed by x-axis and horizontal line

TeX - LaTeX Asked by Tordir on July 13, 2021

I’m new to tikz and I want to draw the following shaded area that is enclosed by the line y = 1, the x-axis, and the circle defined by x^2 + y^2 <= 2. The following figure depicts the desired result. How do I do this?

What I want in Tikz

2 Answers

In general I would recommend following the advice given by Tobi, but this is quite simple, so I'll answer anyway. I'll also note that if you haven't looked at the tutorials at the start of the TikZ manual, I'd recommend doing so. The first of those (chapter 2) will teach you everything needed to make a diagram like that.


Drawing the axes shouldn't be a problem I guess, that's just two straight lines.

For the cut off circle, it consists of two arcs and two straight lines. To draw an arc you can use

arc[start angle=<degrees>, end angle=<degrees>, radius=<length>]

which will draw counter-clockwise the part of a circle from start angle to end angle. (You can also do arc[start angle=<degrees>, delta angle=<degrees>, radius=<length>], where the delta angle indicates the arc length in degrees.)

Lets say you start drawing on the right most point of the circle. You know that is at (2,0), so you start with

draw (2,0) ...

You want to add a label below this point, and while it can be added later, it might be just as convenient in this case to add it at the same time. Text is added using node [<options>] {<text>}:

draw (2,0) node[below] {2} ...

(If adding nodes separately, you can do node [<options>] at (<coordinate>) {<text>};.)

From there you want to draw an arc covering 30 degrees (because sin(30) = 0.5), starting at an angle of zero. Following what was said above:

draw (2,0) node[below] {2} 
       arc[start angle=0, end angle=30, radius=2] node[right] {1}...

where I also added a label right of the endpoint of that arc.

The next part is to draw a straight line to where the left arc starts. While you could calculate the x/y-coordinates, either manually or directly in the coordinate, it's more convenient to use polar coordinates, which are specified with (<angle> : <radius>):

  draw (2,0) node[below] {2}
        arc[start angle=0, end angle=30, radius=2] node[right] {1}
        -- (150:2) ...

Now you just draw the second arc, adding a node for the label, and finally a straight line back to start. When drawing closed paths, instead of repeating the first coordinate use cycle:

draw (2,0) node[below] {2}
        arc[start angle=0, end angle=30, radius=2] node[right] {1}
        -- (150:2)
        arc[start angle=150, end angle=180, radius=2] node[below] {2}
        -- cycle;

draw alone gives you just a black line, but you can specify color and fill in the options:

filldraw [
  fill=brown!80!black,
  draw=red,
  very thick
  ] 
  (2,0) node[below] {2}
        arc[start angle=0, end angle=30, radius=2] node[right] {1}
        -- (150:2)
        arc[start angle=150, end angle=180, radius=2] node[below] {2}
        -- cycle;

(Using filldraw instead of draw isn't strictly necessary when specifying both fill=<color> and draw=<color> as in this case.)

enter image description here

documentclass[border=5mm]{standalone}
usepackage{tikz}
begin{document}
begin{tikzpicture}

path [
  fill=brown!80!black,
  draw=red,
  very thick
  ] 
  (2,0) node[below] {2}
        arc[start angle=0, end angle=30, radius=2] node[right] {1}
        -- (150:2)
        arc[start angle=150, end angle=180, radius=2] node[below] {2}
        -- cycle;
  
  
draw [thick] (-3,0) -- (3,0);
draw [thick] (0,-2) -- (0,2);
end{tikzpicture}
end{document}

Answered by Torbjørn T. on July 13, 2021

An answer with tkz-euclide is added

enter image description here

documentclass{article} % or another class
usepackage{xcolor} % before tikz or tkz-euclide if necessary

usepackage{tkz-euclide} % no need to load TikZ

usetikzlibrary{babel} %if there are problems with the active characters
begin{document}

begin{tikzpicture}
    
    %define the origin O -- radius A
    tkzDefPoint(0,0){O}
    tkzDefPoint(1.41,0){A}
    tkzDrawPoints(O,A)
    tkzLabelPoints[below](O,A)

    %draw the semicircle
    tkzDefPointBy[rotation= center O angle 180](A)
    tkzGetPoint{B}
    tkzDrawArc[line width=0.1pt, white](O,A)(B)
    tkzLabelPoints[below](B)
    %draw the line at y=1
    tkzDefPoint(1,0){A'}
    tkzDefPoint(-1,0){B'}
    tkzDefShiftPoint[A'](90:1){A''}
    tkzDefShiftPoint[B'](90:1){B''}
    tkzDrawSegment[red,line width=1pt](A'',B'')    
    %draw the arc perimeter line
    tkzDrawArc[red,line width=1pt](O,A)(A'')
    tkzDrawArc[red,line width=1pt](O,B'')(B)
    %color the fill inside
    fill[red!40] (-1,0) rectangle (1,1);
    tkzFillSector[rotate,color=red!40](O,A)(45)
    tkzFillSector[rotate,color=red!40](O,B)(-45)
    %draw the center axis -horizontal and vertical
    tkzDrawLine(A,B)
    tkzDefLine[orthogonal =through O](B,A)tkzGetPoint{X}
    tkzDrawLine(O,X)
end{tikzpicture}
end{document}

Answered by js bibra on July 13, 2021

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP