Introduction to Data Structures and Algorithms
Assignment 2 Part B: Using Collections
Resources and links
What To Hand In
Do not rename these files.
- Part A
- Part B
Remember to submit all of these files. When you have submitted them, check that you can read the files listed on the submission page, and complete the submission process.
MoleculeRenderer (Weight: 1/3)
For this question, you are to complete a program that draws 3D representations
of molecules, drawing the atoms in a molecule as coloured circles on the graphics
pane. To help the user see the 3D shape of the molecule, the program
can let the user rotate around the molecule to view it from different directions.
The program involves Maps, Lists, and Sorting.
The shape of molecules can be very important for understanding their function,
especially for large biological molecules. Using X-ray crystallography and
other techniques, chemists or biochemists can identify the structure of a
molecule by identifying the (x, y, z) coordinates of each atom in the
molecule. However, tables of values are hard to make sense of, and it is better
to be able to visualise the molecule by presenting a graphical rendering of the
atoms. Because molecules are 3D shapes, is hard to make sense of them with
just one viewpoint – a good visualisation will allow a user to view the
molecule from different directions.
program produces a simple visualisation
that draws each atom in a molecule as a coloured circle. To ensure a correct
3D visualisation, the atoms that are further from the viewer are drawn
before atoms that are closer to the viewer, so atoms in front will
cover any atoms behind. The MoleculeRenderer
program also lets the
user step left or right around the molecule in 5 degree steps to view the
molecule from multiple (horizontal) directions.
Complete the MoleculeRenderer
program so that it reads a
specification of the atoms in a molecule and then renders the molecule on the
screen. It should be initially shown from the front, but should then
allow the user to view the molecule from different directions.
There are several molecule files including
- acetone (
- a small part of the hemoglobin protein that carries oxygen in our blood stream (
- a buckyball — an unusual form of Carbon (
molecule6.txt ), and
- curcumin — a critical component of Turmeric (
- trinitrotoluene (
- pregestoerone (
- rose bengal - a dye (
The program has three parts:
- Reading information about elements (including size and colour) from a file into a Map of Element objects, indexed by the element type ("C", "H", "N", etc).
- Reading the description of a molecule from a file (atom types and positions) into a List of Atom objects.
- Drawing the molecule by drawing all the atoms in the molecule in order after sorting the list of atoms in the molecule according to the desired viewing direction, so that atoms that should be further from the viewer come earlier in the ordering, and atoms that should be closer come later in the ordering.
Element Information data.
file contains the size and colour for drawing each kind of
element. Your program needs to read this data into a Map of Element
objects, with the name of the element ("O", "C", "N", etc) as the key of the
map. The Element
class is already written for you.
Each molecule is specified in a file that contains one line for each atom in
the molecule. Each atom is specified by an element type (eg, O for
oxygen, C for carbon, N for nitrogen, etc), and three numbers specifying the x,
y, z coordinates of the atom, relative to the centre of the molecule.
Rendering the Molecule
To draw the atoms on the graphics window the way they would appear from some
direction, the program must first order the atoms so that the atoms further
from the viewer are ordered before the atoms closer to the viewer (so that the further
ones appear to be behind the closer ones). This means that it must reorder the
atoms in different ways, depending on their (x, y, z)
position and the viewing direction.
axis goes from left to right, the y
axis goes from top to
bottom, and the z
axis goes from near to far. This means that
viewing the molecule from the front should order the atoms from large
to small z
. Viewing from a different direction will give a
Each atom should be drawn as a circle with the right size and colour
specified by its element type. The 3D positions of the atoms specified
in the file are relative to the centre of the molecule. The position
of the atom on the screen will depend on where the molecule should be centred on
the screen and on the viewing direction.
Look carefully at the methods in the Atom
class: there are
methods that will help in the sorting and methods to help with drawing.
Complete the MoleculeRender
class so that it
- Reads the data from the element-info file into a Map of Elements
- Reads the data from a molecule file into a List of Atoms.
- Renders the molecule from the front
- Complete the showFromViewDirection method in the MoleculeRender class to render the molecule from different viewing directions.
- Viewing molecules from just horizontal directions doesn't give the user a good enough feel for the 3D shape. Extend the program to allow the user to move their view point up and down (from above the molecule to below it), as well as moving around.
You will need to extend the GUI and modify your showFromViewDirection method.
- Molecules are often rendered with balls and sticks, where the sticks show the bonds between the atoms. We have provided three files (
molecule10-with-bonds.txt) that contains the pairs of atoms in the molecule that have a bond between them. Following the atom types and positions, there are lines with two numbers - the indexes of two atoms that have a bond. For example, the line with "42 1" means that atom number 42 has a bond to atom number 1 (the atoms are numbered from 0 in the order they appear in the file).
Modify the rendering to show the bonds as a fat line between the atoms. You may need to reduce the sizes of all the circles in order to show the bonds nicely.
- Make the atoms themselves look spherical rather than flat.