Graphics, Figures & TablesA question about filling a region with colour and pattern

Information and discussion about graphics, figures & tables in LaTeX documents.
Post Reply
kent
Posts: 57
Joined: Thu Oct 20, 2016 3:41 pm

A question about filling a region with colour and pattern

Post by kent »

As seen in the attached pdf file (and Latex input file), I have a region defined by 2 parabola segments and a line (region highlighted in red).

I would like to fill this region using a shape/pattern construct (see Latex file), but I have problem with defining my region's boundary to achieve this.

Any help is highly appreciated!

Best regards,
Kent Holing
NORWAY
Attachments
latexforum2.pdf
(3.55 KiB) Downloaded 292 times
latexforum2.tex
(1.63 KiB) Downloaded 498 times

Recommended reading 2024:

LaTeXguide.org • LaTeX-Cookbook.net • TikZ.org

NEW: TikZ book now 40% off at Amazon.com for a short time.

And: Currently, Packt sells ebooks for $4.99 each if you buy 5 of their over 1000 ebooks. If you choose only a single one, $9.99. How about combining 3 LaTeX books with Python, gnuplot, mathplotlib, Matlab, ChatGPT or other AI books? Epub and PDF. Bundle (3 books, add more for higher discount): https://packt.link/MDH5p

Stefan Kottwitz
Site Admin
Posts: 10329
Joined: Mon Mar 10, 2008 9:44 pm

A question about filling a region with colour and pattern

Post by Stefan Kottwitz »

Hi Kent!

A classic way is using \clip in a scope for defining the boundary and then \filldraw. Not so easy this time, when I quickly tested it, but I may find a way later today.

Stefan
LaTeX.org admin
kent
Posts: 57
Joined: Thu Oct 20, 2016 3:41 pm

A question about filling a region with colour and pattern

Post by kent »

Actually I needed to filldraw a region with a somewhat simpler boundary. Se the new attached PDF file.
The region to be shaded is between the two parabola segments in red and the red line.
Attachments
latexforum2.pdf
(3.55 KiB) Downloaded 372 times
kent
Posts: 57
Joined: Thu Oct 20, 2016 3:41 pm

A question about filling a region with colour and pattern

Post by kent »

I like somebody to revisit this challenge for me. I have tried with no success after I got help from Stefan on a similar problem recently (posted on Nov. 2nd).
Kent
User avatar
Stefan Kottwitz
Site Admin
Posts: 10329
Joined: Mon Mar 10, 2008 9:44 pm

A question about filling a region with colour and pattern

Post by Stefan Kottwitz »

If we cannot easily use a single clip path since it's difficult with parabolas, we could clip several times. Each time, we define a clip path using a parabola, plus a few straight lines around the rest. Clipping all means getting the intersection area that's common to all parts, bounded by a parabola each (plus some straight lines).

This is quickly made :-) using parabolas and some simply chosen points. You can comment out all except one clip to see each area.

Code: Select all

\begin{scope}
  \clip  (O) parabola (P) -- (P2) -- (P1) -- ++(0,-2);
  \clip  (P1) parabola (P2) -- (P) -- (0,-4) -- (O) -- ++(-3,-4);
  \clip  (O) parabola (P1)  -- (P2) -- (P) -- (-4,-4) ;
  \clip  (P2) parabola (P)  -- (0,-5) -- (O) -- ++(0,-4) -- (P1);
  \filldraw [red] (O) circle (20);
\end{scope}
parabola-intersection-fill.png
parabola-intersection-fill.png (10.38 KiB) Viewed 5449 times
Minimal example for testing:

Code: Select all

\documentclass[border=10pt]{standalone}
\usepackage{tikz}
\begin{document}
\begin{tikzpicture}[rotate=-90]
\coordinate (O) at (0,0);  
\coordinate (S) at (-2,2); 
\coordinate (P) at (-4,4);  
\coordinate (P1) at (2,1);  
\coordinate (P2) at (-1,5/2);  
\draw[black,thick] (0,0) parabola (5,25/4);
\draw[black,thick] (0,0) parabola (-5,25/4);
\draw[black,thick] (-2,2) parabola (-5,6.5);
\draw[black,thick] (-2,2) parabola (1,6.5);
\draw[red,ultra thick] (O) parabola (P);
\draw[red,ultra thick] (O) parabola (P1);
\draw[red,ultra thick] (S) parabola (P);
\draw[red,ultra thick] (S) parabola (P2);
\draw[red,ultra thick] (P1) parabola (P2);
\begin{scope}
  \clip  (O) parabola (P) -- (P2) -- (P1) -- ++(0,-2);
  \clip  (P1) parabola (P2) -- (P) -- (0,-4) -- (O) -- ++(-3,-4);
  \clip  (O) parabola (P1)  -- (P2) -- (P) -- (-4,-4) ;
  \clip  (P2) parabola (P)  -- (0,-5) -- (O) -- ++(0,-4) -- (P1);
  \filldraw [red] (O) circle (20);
\end{scope}
\end{tikzpicture}
\end{document}
Stefan
LaTeX.org admin
kent
Posts: 57
Joined: Thu Oct 20, 2016 3:41 pm

A question about filling a region with colour and pattern

Post by kent »

Thanks!
I finally succeeded in getting what my figure should really like, see the attached file. :)
Kent
Attachments
figure.pdf
(7.76 KiB) Downloaded 402 times
User avatar
Stefan Kottwitz
Site Admin
Posts: 10329
Joined: Mon Mar 10, 2008 9:44 pm

A question about filling a region with colour and pattern

Post by Stefan Kottwitz »

Great!

Would you like to add this as example to the TikZ gallery on TeXample.net? If yes, you could post the full code here.

What I see as a good point for learners, is filling an area made by several different curves by intersecting clipping areas.

Stefan
LaTeX.org admin
kent
Posts: 57
Joined: Thu Oct 20, 2016 3:41 pm

A question about filling a region with colour and pattern

Post by kent »

Dear Stefan, if you think the figure has some learning potential, I will consider posting it at the Tikz gallery.

However, looking at some examples I see that people document their examples excellently.

My source code, I am afraid, is a little too obscure for others to read without extensive editing. It was intended only for me to read. (Among other things, I am using several private written macros not directly related to the issue of region shading we want to highlight.)

I am soon finally getting access to upgrade my PGF version to PGF 3.0. After that I will update the code and come back to you on this issue.

Best regards,
Kent
Post Reply