In this lab exercise we are going to implement an algorithm that can take a set of points in the x,y plane and construct a boundary that just wraps around the points.
Your program will read the coordinates for a list of points from a text file named "points.txt" and run an algorithm to compute a set of points needed to construct a boundary polygon that just wraps around the points in the plane. Your program will save the coordinates of the boundary points to a second file named "boundary.txt".
To help you judge whether or not your results are correct I will also provide a Java program that allows you to see both the original set of points and the boundary that you have constructed. The picture above shows the result we are looking for.
Like many algorithms in computer science, the algorithm we are going to implement in this lab exercise relies on solutions to a set of subproblems. This section of the lab materials will describe a couple of those subproblems and how to solve them.
The first idea the lab is going to make use of is the polar angle between a pair of points. The picture below illustrates this concept.
The picture shows a pair of points, p and q. The difference between the x coordinates of the two points is Δx, and the difference between the y coordinates of the two points is Δy. The polar angle θ in radians of the point q relative to the reference point p is given by the formula
θ = atan2(Δy,Δx)
The atan2(y,x) function in available in Java as the Math.atan2(y,x)
method.
The second subproblem our solution will need to make use of is a procedure whether a set of three points in the plane form a left hand turn or a right hand turn.
Here is a picture of a set of three points p, q and r that form a left hand turn.
Here is a picture of a set of three points p, q and r that form a right hand turn.
Here is code for a Java method that can determine whether the three points form a right hand turn.
public static boolean rightTurn(Point p,Point q, Point r) { double v1x = q.x - p.x; double v1y = q.y - p.y; double v2x = r.x - q.x; double v2y = r.y - q.y; return v1x*v2y-v2x*v1y < 0; }
Here now is the outline of the algorithm we will use to construct a set of boundary points for a collection of points in the plane.
Once this algorithm finishes we will have the list of boundary points we need.
At the top of these instructions you will find a button that links to a NetBeans project that you will use for this lab exercise. The NetBeans project includes a text file points.txt
that you will use to provide your program with a set of points to work with. The project also includes a visualization program that will allow you to visualize your results. Running the project will launch the visualization program.
Since the visualization program expects to read a list of boundary points from a file named boundary.txt
you will not be able to run the visualization program until you have written a program to compute the boundary points and save them to boundary.txt
.
Add a new class to the project for your program. The main method of this new class will implement the algorithm that I outlined above. Running this program should compute and save the list of boundary points to the boundary.txt file. At that point you will be able to run the visualization program to confirm that your boundary is correct.