Here's another way to do it without using intersections but using path clipping. Each path we draw, we also define a clip against this path. This does mean "drawing" the path twice: once to draw and once to clip against (and I don't see a quick way of merging these two since they have to happen at different times). One thing I like about this approach is that what the graphical package is doing is precisely what the mathematics is doing. That is, each inequality specifies a "clipping" of the plane, saying "After this, we're only interested in one side of this line and we throw away everything else.". That's exactly what a clip does.
\documentclass{article}
\pagestyle{empty}
\usepackage[svgnames]{xcolor}
\usepackage{tikz}
\usepackage{mathtools}
\def\nudge{.5}
\tikzset{axis/.style={ultra thick, Red!75!black, -latex, shorten <=-\nudge cm, shorten >=-2*\nudge cm}}
\tikzset{line/.style={thick,Green}}
\begin{document}
\begin{tikzpicture}
\draw[axis] (0,0) -- (4,0) node[right=2* \nudge cm] {\(x_1\)};
\draw[axis] (0,0) -- (0,4) node[above=2*\nudge cm] {\(x_2\)};
\begin{scope}
\clip (-\nudge ,-\nudge) rectangle (4+\nudge,4+\nudge);
\draw[line] (0,1) -- (4,4) coordinate (ineq1);
\draw[line] (0,5.5) -- (4,-.5) coordinate (ineq2);
\draw[line] (0,-5) -- (4,3) coordinate (ineq3);
\begin{scope}
\clip (0,1) -- (4,4) |- (0,0);
\clip (0,5.5) -- (4,-.5) -| (0,0);
\clip (0,-5) -- (4,3) |- (4,4) -| (0,0);
\fill[Red,opacity=.5] (0,0) rectangle (4,4);
\end{scope}
\draw[dashed,line] (0,1) -- (2,2) -- (3,1) -- (2,0);
\clip (0,1) -- (2,2) -- (3,1) -- (2,0) -| (0,1);
\fill[Blue,opacity=.5] (0,0) rectangle (4,4);
\end{scope}
\node[above right] at (ineq1) {\(\mathllap{-}3 x_1 + 4 x_2 = 4\)};
\node[below right] at (ineq2) {\(3 x_1 + 2 x_2 = 11\)};
\node[above right] at (ineq3) {\(2 x_1 - x_2 = 5\)};
\foreach \coord/\adj in {
{(2,2)}/right,
{(0,1)}/left,
{(0,0)}/below left,
{(2,0)}/below left,
{(2.5,0)}/below right,
{(3,1)}/right,
{(2,2.5)}/right%
} {
\fill \coord circle (2pt) node[\adj] {\coord};
}
\end{tikzpicture}
\end{document}
Other aspects of this answer:
- We use clipping to avoid having to work out the formulae for the lines too carefully (and we draw these as simple lines rather than using the plot function).
- We use node-coordinates when we want to label the lines since putting the labels directly on the lines wouldn't work with the clipping.
- Note the use of
\mathllap
to adjust the placement of one of the labels (that's why mathtools
is included).
- Note the use of a negative shortening of the axes.
Here is my solution:
\documentclass[11pt]{article}
\usepackage{tikz}
\usetikzlibrary{shadings}
\begin{document}
\begin{tikzpicture}
\draw[top color=white,bottom color=red] (0,0) -- (1,2) -- (0,3) -- cycle;
\foreach \coordinate/\label/\pos in {{(0,0)/label1/above left},{(1,2)/label2/right},{(0,3)/label3/above right}}
\node[\pos] at \coordinate {\label};
\draw[->] (-5.0,0) -- (5.0,0) node[right] {$x$} coordinate(x axis);
\draw[->] (0,-5.0) -- (0,5.0) node[above] {$y$} coordinate(y axis);
\end{tikzpicture}
\end{document}
It gives:
The key points are basically two:
the foreach
that allows to specify in one shot the coordinate
(i.e. (0,0)) in which the label
will be placed in a given position
(i.e. above left
);
the use of the shadings
library that allows to shade the region; in the example the shading is vertical, but it is possible to insert different type of shadings (see 47 Shadings Library from the pgfmanual version April 25, 2012 / 46
Shadings Library from the pgfmanual version October 25, 2010).
Best Answer
This is explained in the sections “15.4 Filling a Path” and “19 Plots of Functions” in the TikZ manual (numbers refer to the 2.10 version).
A simple example (note that
\x r
interprets\x
as radians; the TikZ'ssin
function takes degrees as input):