Listing 232 on page 306 of the textbook
outlines a file named problem-spec.c
which
contains the descriptions of four domains that
are used to illustrate the various capabilities of
Triangle.
The file mesh-demo.c
described in Section 23.7
provides the driver (function main()
)
that invokes those descriptions and triangulates all four domains.
In retrospect, defining four domains in a single file, and triangulating them all at once, is not such a great idea. It will make better sense to split the monolithic demo into four individual demos.
Consider, therefore, replacing problem-spec.c
with four files
that contain domain descriptions, and replacingsquare-spec.c
triangle-with-hole-spec.c
annulus-spec.c
three-holes-spec.c
,
mesh-demo.c
with four individual driverssquare.c
triangle-with-hole.c
annulus.c
three-holes.c
.
That simplifies the coding since the domains may be coded and debugged individually. It also makes it easy to add new domains without disturbing the existing ones.
The book suggests producing graphical representation
of the generated meshes through the supplied files
mesh-to-eps.[ch]
.
These provide a function mesh_to_eps()
that receives a mesh structure and an output file name,
e.g., square.eps
,
it extracts the triangulation
information from the mesh structure and writes a PostScript
script into the file square.eps
that can be fed into a PostScript
viewer for viewing.
With the wide availability of python in these days,
it may be convenient to use python's
matplotlib
plotting utility instead.
That provides you the additional opportunity of writing your
own equivalent of mesh-to-eps.[ch]
, let's call it
mesh-to-py.[ch]
, which
receives a mesh structure and an output file name.
It extracts the triangulation information from the mesh structure and writes a
python script, say square.py
,
that can be fed
into python for viewing.
Here is a minimal sample of a python plotting script:
import matplotlib.pyplot as plt
plt.rcParams['axes.prop_cycle'] = plt.cycler(color=['black'])
plt.gca().set_aspect('equal')
plt.plot([1,2],[4,7])
plt.plot([4,2],[0,4])
plt.show()
The first three lines and the last line are always the same.
Each of the remaining two lines draws a line segment. The syntax
for drawing a line from the point
(x1,y1)
to the point
(x2,y2)
is
plt.plot([x1,x2],[y1,y2])
You may repeat that line as many times as needed. If the script is written
to a file try-me.py
, you may view the graphics by enteringon the Unix command-line.python try-me.py
Thus, write files mesh-to-py.[ch]
that
provide a function, say
void mesh_to_py(struct mesh *mesh, char *outfile);
that receives a mesh structure, walks through the mesh->edges
array, determines the coordinates of the endpoints of each edge, and writes
a python script in
the outfile
.
for plotting the mesh.