Graphics, Figures & TablesFilling Quadrilaterals

Information and discussion about graphics, figures & tables in LaTeX documents.
Post Reply
jhapk
Posts: 81
Joined: Tue Apr 20, 2010 9:33 pm

Filling Quadrilaterals

Post by jhapk »

Hi,

I want to show a hexahedron and fill the top face with grey colour. But I am not sure how to do this. My present code does everything, but to have a coloured face:

Code: Select all

\documentclass[12pt]{article}
\usepackage{tikz}

\begin{document}
\pagestyle{empty}
\begin{tikzpicture}

  \def\width{7}
  % vertices
  \coordinate (l1) at (0,0);
  \coordinate (l2) at (\width,0.1*\width);
  \coordinate (l3) at (0.4*\width,0.6*\width);
  \coordinate (l4) at (-0.33*\width,0.5*\width);
  \coordinate (u1) at (0.2*\width,0.7*\width);
  \coordinate (u2) at (\width,\width);
  \coordinate (u3) at (0.4*\width,1.1*\width);
  \coordinate (u4) at (-0.4*\width,\width);
 
  %%\draw[step=.5cm,gray,very thin] (-0.5*\width,-0.1*\width) grid (1.2*\width,1.2*\width);

  % hexahedral
  \begin{scope}[thick]
    \draw (l1) -- (l2);
    \draw (l1) -- (l4);
    \draw (l1) -- (u1);
    \draw (l4) -- (u4);
    \draw (l2) -- (u2);
    \draw (u1) -- (u2);
    \draw (u1) -- (u4);
    \draw (u3) -- (u4);
    \draw (u3) -- (u2);
  \end{scope}

  % hidden lines
  \begin{scope}[dashed,thick]
    \draw (l4) -- (l3);
    \draw (l2) -- (l3);
    \draw (l3) -- (u3);
  \end{scope}

  % arrows
  \begin{scope}[->,>=latex]
    \draw (2.5,3.5) circle (0.4ex) [fill];
    \draw (2.5,3) [fill=white] node {cell (i, j, k)};
    \draw (2.5,6.5) -- ++(0,1.5) node [at end, right]{$n_m$};
    \draw (1.5,6.3) [fill=white] node {$A_m$};
    \draw (6,3.5) -- ++(1.75,-0.5);
    \draw (-1.5,4.5) -- ++(-1.5,-0.25);
%%     \draw [dashed] (2,2.5) -- ++(0,-1);
%%     \draw [dashed] (5,4.5) -- ++(0.7,0.7);
%%     \draw [dashed] (0.5,5.5) -- ++(-0.9,0.9);
  \end{scope}

\end{tikzpicture}
\end{document}
Any suggestions? Is there a way to draw arbitrary shaped quadrilaterals?
Last edited by jhapk on Fri Sep 03, 2010 4:28 pm, edited 1 time in total.

Recommended reading 2024:

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

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

gmedina
Posts: 2313
Joined: Wed Jul 11, 2007 11:45 pm

Filling Quadrilaterals

Post by gmedina »

Hi,

use the fill option for the \draw command:

Code: Select all

\documentclass[12pt]{article}
\usepackage{tikz}

\begin{document}
\pagestyle{empty}
\begin{tikzpicture}

  \def\width{7}
  % vertices
  \coordinate (l1) at (0,0);
  \coordinate (l2) at (\width,0.1*\width);
  \coordinate (l3) at (0.4*\width,0.6*\width);
  \coordinate (l4) at (-0.33*\width,0.5*\width);
  \coordinate (u1) at (0.2*\width,0.7*\width);
  \coordinate (u2) at (\width,\width);
  \coordinate (u3) at (0.4*\width,1.1*\width);
  \coordinate (u4) at (-0.4*\width,\width);

  % hexahedral
  \begin{scope}[thick]
    \draw (l1) -- (l2);
    \draw (l1) -- (l4);
    \draw (l1) -- (u1);
    \draw (l4) -- (u4);
    \draw (l2) -- (u2);
    \draw[fill=gray!60] (u1) -- (u2) -- (u3) -- (u4) -- (u1);
  \end{scope}

  % hidden lines
  \begin{scope}[dashed,thick]
    \draw (l4) -- (l3);
    \draw (l2) -- (l3);
    \draw (l3) -- (u3);
  \end{scope}

  % arrows
  \begin{scope}[->,>=latex]
    \draw (2.5,3.5) circle (0.4ex) [fill];
    \draw (2.5,3) [fill=white] node {cell (i, j, k)};
    \draw (2.5,6.5) -- ++(0,1.5) node [at end, right]{$n_m$};
    \draw (1.5,6.3) [fill=white] node {$A_m$};
    \draw (6,3.5) -- ++(1.75,-0.5);
    \draw (-1.5,4.5) -- ++(-1.5,-0.25);
  \end{scope}

\end{tikzpicture}
\end{document}
1,1,2,3,5,8,13,21,34,55,89,144,233,...
jhapk
Posts: 81
Joined: Tue Apr 20, 2010 9:33 pm

Re: Filling Quadrilaterals

Post by jhapk »

Thanks gmedina. That was exactly what I was looking for.
Post Reply