[Tex/LaTex] Shading inside a triangle when the coordinates of one vertex are calculated by TikZ

tikz-pgf

The following code instructs TikZ to draw a right triangle and to put a grid on the Cartesian plane. I would like to make two modifications to it.

I want the inside of the triangle to be "colored" white. I looked at Chapter 15, Section 7 of the manual at the web site http://texdoc.net/texmf-dist/doc/generic/pgf/pgfmanual.pdf. It shows the code to shade a circle if its center and radius are specified, and it shows the code to shade a rectangle if two of its vertices are specified. In the following code, one vertex, R, of the triangle is calculated by TikZ using the code \coordinate (R) at ($(P)!1cm*sqrt(5)!-90:(Q)$);. How do I "color" the inside of the triangle white without manually calculating the coordinates of R? (There is a right-angle mark inside the triangle; it should not be "covered.")

The triangle is drawn over the vertex at R. This looks odd. How do I have TikZ draw the triangle under the vertices?

\documentclass[10pt]{amsart}
\usepackage{tikz}
\usetikzlibrary{calc}


\begin{document}

\begin{tikzpicture}

\draw[yellow, line width=0.1pt] (-1.75,-3.25) grid[xstep=0.5, ystep=0.5]  (2.75,1.75);
\draw[draw=gray!30,latex-latex] (0,1.75) +(0,0.25cm) node[above right] {$y$} -- (0,-3.25) -- +(0,-0.25cm);
\draw[draw=gray!30,latex-latex] (-1.75,0) +(-0.25cm,0) -- (2.75,0) -- +(0.25cm,0) node[below right] {$x$};

\node[outer sep=0pt,circle, fill,inner sep=1.5pt,label={[fill=white]left:$P$}] (P) at (-1,-1) {};
\node[outer sep=0pt,circle, fill,inner sep=1.5pt, label={[fill=white]right:$Q$}] (Q) at (2,1) {};

\draw[green!20!white] (P) -- (Q);

\coordinate (R) at ($(P)!1cm*sqrt(5)!-90:(Q)$);
\node[outer sep=0pt,circle, fill,inner sep=1.5pt, label={[fill=white]right:$R$}] at(R) {};
\draw[green!20!white] (Q) -- (P) -- (R) -- (Q) -- cycle;

\coordinate (a) at ($ (P)!5mm! -45:(Q) $);
\draw[green!20!white] (a) -- ($(P)!(a)!(Q)$);
\draw[green!20!white] (a) -- ($(P)!(a)!(R)$);

\end{tikzpicture}

\end{document}

Best Answer

Add fill=white, like this: \draw[green!20!white, fill=white] (Q) -- (P) -- (R) -- (Q) -- cycle;

In order for the dots to appear on the front, I have taken the liberty of rewriting your code. You could have used Layers1, but the code would be more complicated than this and I don't think it's worth it if you need it just this time.

I added some new things like tikzset, so you can use a single word to define node properties (and you don't need to have those long node option lists), furthermore, you only need to fix one to fix them all, without having to change each one.

I defined the coordinates and used those coordinates first to write the nodes P, Q, and R, and then I used the same coordinates to create little black circle nodes.

figure 1

\documentclass[margin=10pt]{standalone}
\usepackage{amsmath}
\usepackage{tikz}
\usetikzlibrary{calc}

\tikzset{
    points/.style={outer sep=0pt, circle, inner sep=1.5pt, fill=white},
    dotnode/.style={circle, fill=black, inner sep=0pt,minimum size=4pt},
}


\begin{document}

\begin{tikzpicture}

    \coordinate (P) at (-1,-1);
    \coordinate (Q) at (2,1);
    \coordinate (R) at ($(P)!1cm*sqrt(5)!-90:(Q)$);
    \coordinate (a) at ($ (P)!5mm! -45:(Q) $);

\draw[yellow, line width=0.1pt] (-1.75,-3.25) grid[xstep=0.5, ystep=0.5]  (2.75,1.75);
\draw[draw=gray!30,latex-latex] (0,1.75) +(0,0.25cm) node[above right] {$y$} -- (0,-3.25) -- +(0,-0.25cm);
\draw[draw=gray!30,latex-latex] (-1.75,0) +(-0.25cm,0) -- (2.75,0) -- +(0.25cm,0) node[below right] {$x$};

\node[points, anchor=east] at (-1,-1) {$P$};a
\node[points, anchor=west] at (2,1) {$Q$};
\node[points, anchor=west] at (R) {$R$};

\draw[green!20!white] (P) -- (Q);
\draw[green!20!white, fill=white] (Q) -- (P) -- (R) -- (Q) -- cycle;

\draw[green!20!white] (a) -- ($(P)!(a)!(Q)$);
\draw[green!20!white] (a) -- ($(P)!(a)!(R)$);

\node[dotnode] at (P) {};
\node[dotnode] at (Q) {};
\node[dotnode] at (R) {};

\end{tikzpicture}

\end{document}

1: See Section 90: Layered Graphics on the Pgfmanual, page 820.