Home Older Mathematica Items

Scientific Arts

# Building a Simple Ball and Stick Molecule Graphic from Graphics Primitives

Often the visualization of data can be performed most conveniently by making use of Mathematica's graphics programming language directly. One organizes various points, lines, textual elements, 3-dimensional primitives, and so on, in graphics objects such as Graphics, Graphics3D, DensityGraphics, SurfaceGraphics, or ContourGraphics, and then displays them using the function Show. The standard reference for all of this (in addition to "The Mathematica Book") is Tom Wickham-Jones's book, "Mathematica Graphics".

Here we do a rather simple exercise: produce a "ball and stick" graphic of a molecule. The molecule is specified in terms of the atoms that it is composed of, their coordinates in a Cartesian coordinate system, and the bonds between them.

Here is the atomic data. It is a list of lists, each of the form , the latter three quantities specifying the cartesian coordinates of the atom.

And here is the bond data. Each sublist is of the form , where the bond extends from atom1 to atom2 (The integers that specify atom1 and atom2 are just the positions in the list of the atoms in AtomData.)

The list of atom coordinates, AtomCoordinates, and their respective types, AtomTypes, can be easily extracted from AtomData as follows.

Similarly the bond information can be extracted.

The graphics primitives for the atoms will be built up in two steps. For each atom's coordinates we create a Point object with a size specification. These are both placed in a list along with the atom type. The latter is not a graphics directive, but we will use it later, via a replacement rule, to insert a color directive chosen differently for each atom type. We use the function MapIndexed to do all of this at once since the part specification that MapIndexed provides to the function is just the integer that specifies the atom.

 MapIndexed[f, expr] applies f to the elements of expr, giving the part specification of each element as a second argument to f. MapIndexed[f, expr, levspec] applies f to all parts of expr on levels specified by levspec. Usage message for MapIndexed

In a similar way we build up the bonds with line primitives (the argument creates the pairs of starting and ending points for the bond lines).

Now we define some colors to use.

Finally we replace the atom types and the bond types with the color directives. Replacement rules do this easily.

Now we can show the graphic.

You can get a better sense of the three dimensional structure of this molecule by animating it. This is simplified by using the SpinShow function from the standard add-on package Graphics`Animation`. First load the package.

Now generate the animation. To view the animation select the cell and choose the menu item, or use its keyboard equivalent.

``````
``````

 For further information on our services send email to info@scientificarts.com . Contents of this web site Copyright © 1999-2011, Scientific Arts, LLC.
 s