# [Programming] Simple meshes

Denis Girou Denis.Girou at idris.fr
Mon May 31 00:04:03 CEST 1999

-----------------------------------------------------------------------------
This is the PSTricks mailing list, devoted to discussions about computational
graphics in (La)TeX using the PSTricks package from Timothy van Zandt.
For help using this mailing list, see instructions at the end of message.
-----------------------------------------------------------------------------

Some months ago, Gaelle Jeanfaivre <Gaelle.Jeanfaivre at asci.fr> ask me how
to draw some simple meshes, based on triangles, as the ones frequently used in
fluid mechanic modelisations. Of course, the goal was not to emulate some of
the "real" (and also huge and complex) programs used in this field to build
and draw meshes, but just to be able to draw and personalize some simple ones
for illustration purposes in documents. For that, the only real problem is to
compute the positions of the various barycenters (I use the very powerful fp'
package from Michael Mehlich for that).

I give her a solution at that time, but she soon found a bug which occur
in some cases. I correct it tonight (at least I hope that there are not
obvious other ones...).

\documentclass[a4paper]{article}

\usepackage{pstcol}
\usepackage[nomessages]{fp}     % CTAN/macros/latex/contrib/other/fp

\newcounter{MyCountA}
\newcounter{MyCountB}
\newcounter{MyCountC}

\makeatletter

\newcommand{\MeshNode}[3]{%
% #1 : node number
% #2 : X coordinate
% #3 : Y coordinate
\setcounter{MyCountA}{#1}
\expandafter\def\csname MeshNode\Alph{MyCountA}\endcsname{#2,#3}
\expandafter\def\csname MeshNode\Alph{MyCountA}@X\endcsname{#2}
\expandafter\def\csname MeshNode\Alph{MyCountA}@Y\endcsname{#3}}

\newcommand{\MeshTriangle}[4]{%
% #1 : label to print on the barycenter of this triangle
% #2 : first  node number
% #3 : second node number
% #4 : third  node number
\setcounter{MyCountA}{#2}
\def\MeshNode at A{\expandafter\csname MeshNode\Alph{MyCountA}\endcsname}%
\setcounter{MyCountB}{#3}
\def\MeshNode at B{\expandafter\csname MeshNode\Alph{MyCountB}\endcsname}%
\setcounter{MyCountC}{#4}
\def\MeshNode at C{\expandafter\csname MeshNode\Alph{MyCountC}\endcsname}%
\pspolygon(\MeshNode at A)(\MeshNode at B)(\MeshNode at C)
\FormatNodeRput(\MeshNode at A){\FormatNodeNumber{#2}}
\FormatNodeRput(\MeshNode at B){\FormatNodeNumber{#3}}
\FormatNodeRput(\MeshNode at C){\FormatNodeNumber{#4}}
\PutLabelOnBarycenter{\expandafter\csname MeshNode\Alph{MyCountA}@X\endcsname}%
{\expandafter\csname MeshNode\Alph{MyCountA}@Y\endcsname}%
{\expandafter\csname MeshNode\Alph{MyCountB}@X\endcsname}%
{\expandafter\csname MeshNode\Alph{MyCountB}@Y\endcsname}%
{\expandafter\csname MeshNode\Alph{MyCountC}@X\endcsname}%
{\expandafter\csname MeshNode\Alph{MyCountC}@Y\endcsname}%
{#1}}

\newcommand{\PutLabelOnBarycenter}[7]{%
% #1,#2 : coordinates of the first  node (A) of the triangle
% #3,#4 : coordinates of the second node (B) of the triangle
% #5,#6 : coordinates of the third  node (C) of the triangle
% #7    : label to print on the barycenter
%
% First part (nodes A and B - or B and C if we can't use them)
\FPeval{\@tempa}{(#1 + #3) / 2}
\FPifeq{\@tempa}{#5}
\PutLabelOnBarycenter@{#3}{#4}{#5}{#6}{#1}{#2}{\Coef at A}{\Coef at B}
\else
\PutLabelOnBarycenter@{#1}{#2}{#3}{#4}{#5}{#6}{\Coef at A}{\Coef at B}
\fi
% Second part (nodes A and C - or B and C if we can't use them)
\FPeval{\@tempa}{(#1 + #5) / 2}
\FPifeq{\@tempa}{#3}
\PutLabelOnBarycenter@{#3}{#4}{#5}{#6}{#1}{#2}{\Coef at C}{\Coef at D}
\else
\PutLabelOnBarycenter@{#1}{#2}{#5}{#6}{#3}{#4}{\Coef at C}{\Coef at D}
\fi
% We solve the system of these two linear equations
\FPeval{\Barycenter at X}{(\Coef at B - \Coef at D) / (\Coef at A - \Coef at C)}
\FPeval{\Barycenter at Y}{(\Coef at D) - \Coef at C * \Barycenter at X}
% Now, we can print the label
\rput(\Barycenter at X,\Barycenter at Y){\FormatLabelOnBarycenter{#7}}}

\newcommand{\PutLabelOnBarycenter@}[8]{%
\FPeval{\@tempa}{(#1 + #3) / 2}
\FPeval{\@tempb}{(#2 + #4) / 2}
% Equation: Coef1 * x + y = Coef2
\FPeval{#7}{(\@tempb - #6) / (#5 - \@tempa)}
\FPeval{#8}{#5 * #7 + #6}}

% Format to position the node numbers
\newcommand{\FormatNodeRput}{\rput*}

% Format to print the node numbers
\newcommand{\FormatNodeNumber}[1]{\LARGE\textbf{#1}}

% Format to print the labels on the barycenters
\newcommand{\FormatLabelOnBarycenter}[1]{\Huge\textbf{\textcolor{red}{#1}}}

\makeatother

\SpecialCoor

\pagestyle{empty}

\begin{document}

% Definition of the nodes
\MeshNode{1}{0}{9}
\MeshNode{2}{3.4}{5.8}
\MeshNode{3}{3}{9}
\MeshNode{4}{0}{6}
\MeshNode{5}{0}{3}
\MeshNode{6}{1.5}{2.8}
\MeshNode{7}{0}{0}
\MeshNode{8}{3}{0}
\MeshNode{9}{6}{9}
\MeshNode{10}{6.6}{4.3}
\MeshNode{11}{9}{9}
\MeshNode{12}{9}{6}
\MeshNode{13}{4.1}{2.3}
\MeshNode{14}{6}{0}
\MeshNode{15}{9}{0}
\MeshNode{16}{9}{3}

% The mesh
\newcommand{\MeshDraw}{%
\begin{pspicture}(9,9)
\MeshTriangle{1}{6}{7}{8}
\MeshTriangle{2}{8}{13}{14}
\MeshTriangle{3}{4}{5}{6}
\MeshTriangle{4}{2}{6}{13}
\MeshTriangle{5}{10}{15}{16}
\MeshTriangle{6}{1}{2}{3}
\MeshTriangle{7}{10}{12}{16}
\MeshTriangle{8}{6}{8}{13}
\MeshTriangle{9}{2}{4}{6}
\MeshTriangle{10}{2}{10}{13}
\MeshTriangle{11}{1}{2}{4}
\MeshTriangle{12}{10}{13}{14}
\MeshTriangle{13}{2}{3}{9}
\MeshTriangle{14}{10}{11}{12}
\MeshTriangle{15}{2}{9}{10}
\MeshTriangle{16}{10}{14}{15}
\MeshTriangle{17}{9}{10}{11}
\MeshTriangle{18}{5}{6}{7}
\end{pspicture}}

\MeshDraw

\clearpage
% Redefinition of some mesh layout characteristics
\renewcommand{\FormatNodeRput}{\rput}
\renewcommand{\FormatNodeNumber}[1]{%
\pscirclebox[fillstyle=solid,fillcolor=yellow]{\LARGE\textcolor{red}{#1}}}
\renewcommand{\FormatLabelOnBarycenter}[1]{%
\setcounter{MyCountA}{#1}
\LARGE\textbf{\textcolor{cyan}{\Roman{MyCountA}}}}

\psset{unit=1.5}
\MeshDraw

\end{document}

D.G.

-----------------------------------------------------------------------------
`