13
\begin{document}
\begin{tikzpicture}
\draw (0, 0) -- (0, 8) -- (8, 8) -- (8, 0) --cycle;
\draw (0, 8) -- (8, 4);
\draw (0, 0) -- (8, 8);
\end{tikzpicture}
\end{document}

enter image description here

Apart from calculating the intersection point and manually drawing each shape, etc., what other options do I have to color any one of the four regions?

blackened
  • 4,181

3 Answers3

19

A perhaps less know possibility is to employ the pgfplots library fillbetween. To show that I do not overwrite different parts, I fill each of them separately in an animation.

\documentclass[tikz,border=3.14mm]{standalone}
\usepackage{pgfplots}
\pgfplotsset{compat=1.16}
\usepgfplotslibrary{fillbetween}
\begin{document}
\foreach \X in {0,...,3}
{\begin{tikzpicture}
\draw[name path=box] (0, 0) -- (0, 8) -- (8, 8) -- (8, 0) --cycle;
\draw[name path=d1] (0, 8) -- (8, 4);
\draw[name path=d2] (0, 0) -- (8, 8);
\ifcase\X
\fill [blue,intersection segments={of=d1 and d2,
                sequence={A0 -- B0[reverse]}}];
\or             
\fill [blue,intersection segments={of=d1 and d2,
                sequence={A0 -- B1}}];
\or             
\fill [blue,intersection segments={of=d1 and d2,
                sequence={A1[reverse] -- B1}}];
\or             
\path [name path=aux,intersection segments={of=d1 and d2,
                sequence={A1[reverse] -- B0[reverse]}}];                
\fill [blue,intersection segments={of=aux and box,
                sequence={A1 -- B2[reverse]}}];         
\fi                     
\end{tikzpicture}}
\end{document}

enter image description here

16

Edit 1: Added a second possibility to color the zones.

Edit 2: Place a node in the center of the regions

One possibility is to calculate the coordinates of the intersection point of the two segments drawn in the square with TikZ; then to color the desired part.

For this purpose, there is an operation that is no longer documented in manual 3.0.1a but is fully functional. Its documentation can be found in the manual 1.18 which is still available (until when?) here TikZ manual 1.18 (p 87, section 10.2.4 Intersection Coordinate Systems).

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

\begin{document}
\begin{tikzpicture}
\draw (0, 0) coordinate(A)node[below left]{A}-- (0, 8)coordinate(B)node[above left]{B} -- (8, 8)coordinate(C)node[above right]{C} -- (8, 0)coordinate(D)node[below right]{D} --cycle;
\draw (B) -- (8, 4)coordinate(E)node[right]{E};
\draw (A) -- (C);
\coordinate (I) at (intersection of 0,8--E and A--C);
\fill[green](A)--(B)--(I)--cycle;
\fill[blue](B)--(C)--(I)--cycle;
\fill[red](C)--(E)--(I)--cycle;
\fill[violet](A)--(D)--(E)--(I)--cycle;
\end{tikzpicture}
\end{document}

fill-region

Edit 2 : Same with Intersections of Arbitrary Paths

The other possibility is to use the intersections library documented in the manual 3.0.1a (p137; 13.3.2 Intersections of Arbitrary Paths) which allows to calculate the coordinates of the intersection points of any two paths (straight or curved).

\documentclass[border=5pt]{standalone}
\usepackage{tikz}
\usetikzlibrary{intersections}
\begin{document}
\begin{tikzpicture}
\draw (0, 0) coordinate(A)node[below left]{A}-- (0, 8)coordinate(B)node[above left]{B} -- (8, 8)coordinate(C)node[above right]{C} -- (8, 0)coordinate(D)node[below right]{D} --cycle;
\draw[name path=ae] (B) -- (8, 4)coordinate(E)node[right]{E};
\draw[name path=ac] (A) -- (C);
\path[name intersections ={of= ae and ac,by=I}];
\fill[green](A)--(B)--(I)--cycle;
\fill[blue](B)--(C)--(I)--cycle;
\fill[red](C)--(E)--(I)--cycle;
\fill[violet](A)--(D)--(E)--(I)--cycle;
\end{tikzpicture}
\end{document}

Edit 3 : Place a node in the center of the regions

To place a node in the center of the zones, it is possible to use barycentric coordinates (p133, section 13.2.2 Barycentric Systems).

I have placed several nodes with different options to give you an overview of the possibilities of TikZ.

barycentre

\documentclass[border=5pt]{standalone}
\usepackage{tikz}
\usetikzlibrary{intersections,shapes.geometric}
\begin{document}
\begin{tikzpicture}
\draw (0, 0) coordinate(A)node[below left]{A}-- (0, 8)coordinate(B)node[above left]{B} -- (8, 8)coordinate(C)node[above right]{C} -- (8, 0)coordinate(D)node[below right]{D} --cycle;
\draw[name path=ae] (B) -- (8, 4)coordinate(E)node[right]{E};
\draw[name path=ac] (A) -- (C);
\path[name intersections ={of= ae and ac,by=I}];
\fill[green](A)--(B)--(I)--cycle;
\node[node font=\bf,ellipse,draw,fill=white,inner sep=1pt] at (barycentric cs:A=1,B=1.2 ,I=1.5) {Zone 1};
\fill[blue](B)--(C)--(I)--cycle;
\node[text=white,node font={\it\Large},draw=white,thick] at (barycentric cs:C=1,B=1 ,I=1) {Zone 2};
\fill[red](C)--(E)--(I)--cycle;
\node[fill=cyan,double=white,double distance=2pt,draw,rounded corners] at (barycentric cs:C=1,E=1 ,I=1) {Zone 3};
\fill[violet](A)--(D)--(E)--(I)--cycle;
\node[fill=white,circle] at (barycentric cs:A=1,D=1.4,E=1 ,I=1) {Zone 4};
\end{tikzpicture}
\end{document}

Translated with www.DeepL.com/Translator

AndréC
  • 24,137
  • Is there an easy way to put text via node in the center of one of those regions? – blackened Oct 14 '18 at 09:22
  • @blackened It is possible to use the barycentre. – AndréC Oct 14 '18 at 09:23
  • @blackened I added nodes in the center of the regions with barycentric coordinates in the response. – AndréC Oct 14 '18 at 12:20
  • I guess a somewhat more elegant way would be to place these nodes at the centers of the incircles, at least for the triangles. –  Oct 14 '18 at 14:15
  • @marmot With tkz-Euclide? Personally, I don't know of any other packages or library built on Tikz that allow you to do it simply. – AndréC Oct 14 '18 at 15:24
  • I'm now with weak internet connection but here is one way. There must be many more. –  Oct 14 '18 at 18:59
  • 1
    Just to save some typping you could replace constructins like \coordinate(D)node[below right]{D} with \coordinate[label=-45:D](D). – Ignasi Oct 15 '18 at 08:43
  • 1
    @AndréC I cannot decide as to which one of these two answers I should pick as the answer. Can someone please help? Ideally, it would be best if they were somehow combined. – blackened Oct 15 '18 at 14:30
  • @blackened After implementing both solutions in your document, accept the solution that suits you best. read: https://tex.stackexchange.com/help/someone-answers – AndréC Oct 15 '18 at 16:58
3

Here is a method to fill after clip (without reference to the intersection point) :

\documentclass[tikz,border=7pt]{standalone}
\begin{document}
  \begin{tikzpicture}
    \path
      (0, 0) coordinate (A)
      (8, 8) coordinate (C)
      (8, 4) coordinate (E);
    \begin{scope} % red zone : fill after clip
      \clip (A) -- (A-|C) -- (C);
      \clip (E)--(C)-- (A|-C);
      \fill[red] (A) rectangle (C);
    \end{scope}
    \begin{scope} % yellow zone : fill after clip
      \clip (A) -- (C) -- (A|-C);
      \clip (A|-C)--(A)-- (A-|C)--(E);
      \fill[yellow] (A) rectangle (C);
    \end{scope}
    \draw (A) rectangle (C) -- (A) (A|-C) -- (E);
  \end{tikzpicture}
\end{document}

enter image description here

Kpym
  • 23,002