5

I have a region on the plane bounded by two hyperbolas xy = +/- C. What is the best/simplest way to shade (not filled) the "star shaped" region enclosed by these two hyperbolas?

Advanced version of this question: I have two rays in the first quadrant, starting from the origin. These two rays cut out a curved wedge in the star shaped region. How I can shade this wedge in a different color from the rest of the region?

Here is a MWE for the star shaped region, plus the two rays:

\documentclass{amsart} \usepackage{tikz} \begin{document} \begin{tikzpicture} \draw [thick, domain=0.5:3] plot (\x, { 1.2/\x)}); \draw [thick, domain=-3:-0.5] plot (\x, { 1.2/\x)}); \draw [thick, domain=0.5:3] plot (\x, {-1.2/\x)}); \draw [thick, domain=-3:-0.5] plot (\x, {-1.2/\x)}); \draw (-3.4,0) -- (3.4,0); \draw (0,-2.6) -- (0,2.6); \draw [thick] (0,0) -- (2.2,2.2); \draw [thick] (0,0) -- (3.1,1.5); \end{tikzpicture} \end{document} 

Thanks for your help!

EDIT: I know how to fill a polygon using

\draw [filled] (coord) -- (coord) -- etc 

but I don't know how to handle the parabolas.

7
  • I think you can weld segments in spath3. I never got the hang of it though. Commented Nov 22 at 19:04
  • Alternatively (which is what I would do), you could calculate everything manually. Commented Nov 22 at 19:05
  • Thanks @Jasper. I can shade a polygon using \draw [filled] (coord) -- (coord) -- etc but I don't know how to handle the parabolas. I've edited my post to reflect this. Commented Nov 22 at 19:49
  • You have a reciprocal equation for the hyperbola, and a linear equation for the line. You can find their intersection by setting those equations equal to each other and solving for x. Basically, represent the line segment not as two points, but as y=mx+b or whatever. Commented Nov 22 at 19:53
  • 1
    You can choose the domain as you wish: \fill [domain=0.5:3] (0,0) -- (0.5,1.2/0.5) -- plot (\x, { 1.2/\x)}) -- cycle; Commented Nov 22 at 19:56

4 Answers 4

9

In luadraw (which produces TikZ) there is a dedicated method for this type of drawing: Ddomain2

\documentclass{standalone}%compile with lualatex only \usepackage[svgnames]{xcolor} \usepackage{luadraw}%https://github.com/pfradin/luadraw/ \usepackage{fouriernc} %https://tex.stackexchange.com/questions/755217/how-to-shade-an-region-bounded-by-curves \begin{document} \begin{luadraw}{name=shade_region} local g = graph:new{} local f = function(x) return 1/x end local h = function(x) return -1/x end g:Ddomain2(f,h,{x={-1,4},draw_options="fill=pink"}) g:Daxes({0,1,1}) g:Linewidth(8) g:Dcartesian(h,{draw_options="red",discont=true}) local a,b = 1.5*Z(1,1.5), Z(3,1) g:Beginclip({a,0,b,"l"}) g:Ddomain1(f,{x={0,4},draw_options="draw=none,fill=green,fill opacity=0.3"}) g:Endclip() g:Dcartesian(f,{draw_options="blue",discont=true}) g:Dpath({a,0,b,"l"}) g:Show() \end{luadraw} \end{document} 

enter image description here

7

Maybe not the easiest way, but you can make use of the fillbetween library provided by the pgfplots package:

\documentclass{amsart} \usepackage{pgfplots} % loads tikz \pgfplotsset{compat=1.18} \usetikzlibrary{fillbetween, backgrounds} \begin{document} \begin{tikzpicture} \draw [name path=a, thick, domain=0.5:3] plot (\x, { 1.2/\x)}); \draw [name path=b, thick, domain=-3:-0.5] plot (\x, { 1.2/\x)}); \draw [name path=c, thick, domain=0.5:3] plot (\x, {-1.2/\x)}); \draw [name path=d, thick, domain=-3:-0.5] plot (\x, {-1.2/\x)}); \draw (-3.4,0) -- (3.4,0); \draw (0,-2.6) -- (0,2.6); \draw [name path=e, thick] (0,0) -- (2.2,2.2); \draw [name path=f, thick] (0,0) -- (3.1,1.5); \path [name path=ac, intersection segments={ of=a and c, sequence={R* -- L*[reverse]} }]; \path [name path=bd, intersection segments={ of=b and d, sequence={R*[reverse] -- L*} }]; \path [name path=ef, intersection segments={ of=e and f, sequence={R*[reverse] -- L*} }]; \begin{scope}[on background layer] \fill [red!50, name path=acbd, intersection segments={ of=ac and bd, sequence={R* -- L*} }] -- cycle; \fill [green!50, intersection segments={ of=acbd and ef, sequence={R2 -- L2} }] -- cycle; \end{scope} \end{tikzpicture} \end{document} 

ouput of above code

3
  • 1
    But I don’t know what you mean with shade instead of fill. A gradient? Or some transparency? Commented Nov 22 at 20:32
  • Thanks. I was thinking about lighter coloring; so turns out e.g. \draw [filled, gray!60] works for me (in tikz). Commented Nov 23 at 17:09
  • @underflow You can also use opacity to make things transparent Commented Nov 23 at 17:10
5

Here's another way to solve this problem.
No additional packages are required, and in my opinion, everything is as simple as possible.

\documentclass{amsart} \usepackage{tikz} \usetikzlibrary{patterns} \begin{document} \begin{center} \begin{tikzpicture} % Parameters \def\C{1.2} \def\xmax{3.4} \def\ymax{2.5} % Minimum |x| such that |C/x| = \ymax \pgfmathsetmacro{\xmin}{\C / \ymax} % ≈ 0.48 % Hatch the star-shaped region — only within the visible window \fill[pattern=north east lines, pattern color=gray!60] plot[domain=\xmin:\xmax, samples=200, smooth] (\x, {\C/\x}) -- plot[domain=\xmax:\xmin, samples=200, smooth] (\x, {-\C/\x}) -- cycle; \fill[pattern=north east lines, pattern color=gray!60] plot[domain=-\xmin:-\xmax, samples=200, smooth] (\x, {\C/\x}) -- plot[domain=-\xmax:-\xmin, samples=200, smooth] (\x, {-\C/\x}) -- cycle; % Fill the central vertical strip between the two branches to close the region near the y-axis \fill[pattern=north east lines, pattern color=gray!60] (\xmin,\ymax) -- (-\xmin,\ymax) -- (-\xmin,-\ymax) -- (\xmin,-\ymax) -- cycle; % Sector in the first quadrant \pgfmathsetmacro{\xA}{sqrt(\C)} % intersection of y = x and xy = C \pgfmathsetmacro{\k}{1.5/3.1} \pgfmathsetmacro{\xB}{sqrt(\C / \k)} \pgfmathsetmacro{\yA}{\xA} \pgfmathsetmacro{\yB}{\k * \xB} % Hatch the sector (only the part within the visible region) \fill[pattern=horizontal lines, pattern color=red!90!black] (0,0) -- (\xB, { \C / \xB}) -- plot[domain=\xB:\xA, samples=100, smooth] (\x, {\C/\x}) -- (\xA, { \C / \xA }) -- cycle; % Hyperbolas (visible portions only) \draw[thick, domain=\xmin:\xmax] plot (\x, {\C/\x}); \draw[thick, domain=\xmin:\xmax] plot (\x, {-\C/\x}); \draw[thick, domain=-\xmax:-\xmin] plot (\x, {\C/\x}); \draw[thick, domain=-\xmax:-\xmin] plot (\x, {-\C/\x}); % Axes \draw[->] (-3.7,0) -- (3.7,0) node[right] {$x$}; \draw[->] (0,-2.8) -- (0,2.8) node[above] {$y$}; % Rays \draw[thick] (0,0) -- (2.2,2.2); \draw[thick] (0,0) -- (3.1,1.5); \end{tikzpicture} \end{center} \end{document} 

enter image description here

2
  • 1
    Very nice...I like also this. Commented Nov 23 at 11:12
  • 2
    @Sebastiano Thank you very much — that’s very kind of you. Commented Nov 23 at 11:37
3

Purely for comparison, here is a version in plain Metapost. (Follow the link for tutorials and reference).

shaded curved star

You need to compile this with lualatex to get the luamplib support.

\documentclass[border=5mm]{standalone} \usepackage{luamplib} \begin{document} \mplibtextextlabel{enable} \begin{mplibcode} beginfig(1); % make the hyperbola paths, starting with a half-hyperbola path h[]; numeric s, t, maxx; s = 1/16; t = 1.2; maxx = 3; h0 = ((t,1) for x = t+s step s until maxx: .. (x,t/x) endfor) scaled 30; % and the others are made by reflecting and then rotating h1 = subpath (length h0, 0) of h0 .. h0 reflectedabout(origin, (1,1)); h2 = h1 rotated 90; h3 = h2 rotated 90; h4 = h3 rotated 90; % the axes trimmed to the size of the hyperbolas path xx, yy; xx = (xpart point 0 of h3, 0) -- (xpart point 0 of h1, 0); yy = xx rotated 90; % two random rays in first quadrant path ray[]; ray1 = origin -- 100 dir 30; ray2 = ray1 rotated 28; % the "star" is just made by joining the hyperbolas path star_shaped_region; star_shaped_region = h1 -- h2 -- h3 -- h4 -- cycle; % now shade the star by drawing and then clipping for i = -200 step 4 until 200: draw (left--right) scaled 600 rotated 42 shifted (i, 0) withpen pencircle scaled 1/4 withcolor 2/3 red; endfor clip currentpicture to star_shaped_region; % the "buildcycle" macro helps to make the curved wedge path curved_wedge; curved_wedge = buildcycle(ray1, h1, ray2); % you need to fill the wedge using the "image" macro, so that the clip % only applies to the drawing inside the image... picture filled_wedge; filled_wedge = image( unfill curved_wedge; % <- leave this out if you prefer cross-hatching for i = -10 step 3 until 200: % smaller step makes lines closer draw (left--right) scaled 600 rotated -32 shifted (i, 0) withpen pencircle scaled 1/4 withcolor 2/3 blue; endfor clip currentpicture to curved_wedge; ); draw filled_wedge; % add the image to the picture % finally draw all the lines... draw ray1; draw ray2; draw h1; draw h2; draw h3; draw h4; draw xx; draw yy; endfig; \end{mplibcode} \end{document} 

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.