Program 4: Convex Hull
Due: April 8, 11:59 pm
CS 16, Sem. II 2008
Read this Whole Handout. And, of course, complete all the readings we ask you to do. We're not being mean. It'll help you understand and complete this program quickly, and it'll cut down on the time spent waiting in line for a TA.
You must read the Convex Hull Help Session slides before coming to TA hours.
The following material will also be helpful in understanding various aspects of the assignment:
You will write an implementation of interface support.convexhull.ConvexHull. This class will be your implementation of an incremental Graham Scan algorithm. When a point is added to the set, your algorithm will determine whether or not it belongs in the convex hull, and add it if necessary. To get full credit you must implement a Graham Scan rather than some other algorithm.
Note that the Static Graham Scan method is extra credit, you are not required to implement it. The static Graham Scan takes in a set of points and finds the convex hull for that set all at once, rather than determining the convex hull as points are added. See a TA on hours if you have questions about this, or visit the wikipedia page for more information.
Your class is required to implement the interface we defined exactly. You cannot change the method signature of any method in the stencil code. You can, however, add as many helper methods as you would like. Helper methods are encouraged!
As in the previous programs, the TAs will provide a visualizer for you. It is a hacked up concoction, but it has no known bugs.
The visualizer will not run until you implement the hull() method.
This method is found in the MyHull.java file that was
installed using the steps outlined above. See the "Data Structures"
section below for more information on visualization.
CircularTree that
we provide for you in the support.convexhull package. Please refrain
from programming your own data structures! Everything will be simpler,
easier, and quicker to code if you use what we have provided for you, and
you'll be much happier in the end.
Note that, unlike in Queue and Heap, you are not designing a generic
data structure for Convex Hull. Rather, you are instantiating the CircularTree
class that is itself generic, so it is your responsibility to define
the concrete classes the tree will use for its key K and value V.
In order for the visualizer to properly display your hull, it is
required that the key be of type Angle and
the value or element stored of type HullPoint.
NDS4 Javadocs are available at http://net3.datastructures.net/doc4/index.html
Given this, you need a standard for interpreting the results. You should
have your orientation test return ints so that you can use the
easily-readable built-in operators (<, >, ==, and so on).
The class support.convexhull.HullPoint maintains coordinate information
about the point. HullPoints are created and passed into your insert method each
time a point is added to the canvas of the visualizer. Your insert method must determine
if these points are on the hull or not. The HullPoint class provides the coordinates
that your orientation tester should be using when performing tests on points. See the
definitions below.
You will be storing points in the CircularTree using the Angle between the new point and the anchor point as the Key. These Angles will keep them organized in counterclockwise order. See the help session slides for further clarification. Initially, the anchor point will be the first point you add to the Hull. This will change once you have added three points. When there are three points in the Hull (ie. not three collinear points), the new anchor point should be calculated by finding the midpoint of the three points currently in the Hull. You will then need to update the Angles of the points already in the Hull and reinsert them into the tree based on these new Keys.
To make a new Angle, you will instantiate a new instance of the Angle class, passing it the difference of the x-coordinates and the difference of the y-coordinates between the new point and the anchor point. See the help session slides for more information.
Remember that the upper left-hand corner is point (0,0). So points that are lower on the screen will have a higher y-value than the points above them, unlike the standard coordinate system the Angle class is based on (while the x-coordinates will be the same as the standard system). Think about how this will affect the computation of the Angles.
A window will show up with a column of buttons and a graphical background. Left click on the background to add points (make sure you don't move the mouse, or it won't register the click). The visualizer will connect the points with lines to draw the current convex hull. The hull will be drawn with red lines.
When a point is added, it is classified and colored as follows:
You can also add a point by specifying its x and y coordinates in the boxes on the left side of the window. This is especially useful for checking special cases.
Right clicking anywhere on the background will display the x and y coordinates in the boxes on the right.
"Load File" is a slightly different beast. When you select "Load File," a dialog box will appear allowing you to select a file. The file consists of a pairs of x and y coordinates representing the points. For example:
100 100 100 50 234 453 74 222
This will create 4 points, with the coordinates {100,100}, {100,50}.... you get the idea. There should be only one pair of points per line, and there must be one pair of points on every line. That is, no blank lines are allowed in the file. Creating files is highly encouraged as it makes checking for special cases repeatedly very simple.
If at any time, you click on "Clear" it will remove all points currently in the drawing and allow you to start from scratch.
Run the handin script cs016_handin convexhull .
As always, save your README file in the same directory was your code and it will be handed in along with your file.
See the Convex Hull documentation linked on the assignments page.
Come to the helpsession and do the reading before you even think about starting to code. There are quite a few resources available for this project – the textbook (and the textbook supplements), lecture slides, helpsession, Javadocs, and (last but not least, with the emphasis on last) your friendly TA staff – so use them! You may be sick of hearing this from CS15, but please do start early. Good luck!