Convex Hull,。
Project Overview
In this project, you are asked to implement Graham’s scan and Javis’ march to construct the convex hull of an input set of points in the plane, and compare their execution times. For a description of the algorithm, please refer to the lecture notes “convex-hull.pptx”.
We make the following two assumptions:
- a) All the input points have integral coordinates ranging between 50 and 50.
- b) The input points may have duplicates.
In a), integral coordinates are assumed to avoid issues with floating-point arithmetic. The rectangular range [50, 50] [50, 50] is big enough to contain 10,201 points with integral coordinates. Since the input points will be either generated as pseudo-random points or read from an input file, duplicates may appear.
The classes PureStack and ArrayBasedStack should not be modified. Four classes from Project 2 will be modified or reused for this project: Segment, Plot, Point, and PolarAngleComparator,. No need to modify Segment and Plot.
Modify your implementation of the class Point so that the compareTo() method now compares y-coordinate first. The class PolarAngleComparator has an additional instance variable flag used for breaking a tie between two points which have the same polar angle with respect to the reference point (stored in the instance variable referencePoint). When flag == true, the point closer to referencePoint is considered smaller; when flag == false, the further point is considered smaller.
ConvexHull Class
Convex hull construction is implemented by the abstract class ConvexHull, which has two constructors:
1 2
|
public ConvexHull(Point[] pts) throws IllegalArgumentException public ConvexHull(String inputFileName) throws FileNotFoundException, InputMismatchException
|
The first constructor takes an array pts[] of points and copy them over to the array points[]. The array pts[] may consist of random points, or more precisely, points whose coordinates are pseudo-random numbers within the range [50, 50] [50, 50]. After Project 2, you are assumed to be familiar with random point generation. (Just in case not, please read Section 5.)
The second constructor reads points from an input file of integers. Every pair of integers represents the and -coordinates of a point. A FileNotFoundException will be thrown if no file with the inputFileName exists, and an InputMismathException will be thrown if the file consists of an odd number of integers. (There is no need to check if the input file contains unneeded characters like letters since they can be taken care of by the hasNextInt() and nextInt() methods of a Scanner object.)
For example, suppose a file points.txt has the following content (this was the same example file used in the description of Project 2):
0 0 -3 -9 0 -10
8 4 3 3
-6 3
-2 1
10 5
-7 -10
5 -2
7 3 10 5
-7 -10 0 8
-1 -6
-10 0
5 5
There are 34 integers in the file. A constructor call ConvexHull(“points.txt”) will initialize the array points[] to store 17 points below (aligned with five points per row just for display clarity here):
(0, 0) (-6, 3) (7, 3) (-10, 0) (3, 3)
(-3, -9) (-2, 1) (10, 5) (5, 5) (5, -2)
(0, -10) (10, 5) (-7, -10) (-1, -6)
(8, 4) (-7, -10) (0, 8)
Note that the points (-7, -10) and (10, 5) each appear twice in the input, and thus their second appearances are duplicates. The 15 distinct points are plotted in Fig.1 by Mathematica.
There is a non-negligible chance that duplicates occur among the input points, whether they are from the input file or randomly generated from the range [50, 50] [50, 50]. For a fair comparison between Graham’s scan and Javis’ march, both assuming their input points to be distinct, all the duplicates should be eliminated before the convex hull construction. This is done by the constructors via calling the method removeDuplicates().
The method removeDuplicates() performs quicksort on all the points by y-coordinate. After the sorting, equal points will appear next to each other in points[]. The method creates an object of the class QuickSortPoints to carry out quicksort using a provided Comparator object, which invokes the compareTo() method in the class Point. After the sorting, identical points will appear together, and duplicates will be easily removed. Distinct points are then saved to the array pointsNoDuplicate[] with the element at index 0 assigned to lowestPoint.
In the previous example, quicksort produces the following sequence:
(-7, -10) (5, -2) (3, 3) (10, 5)
(-7, -10) (-10, 0) (7, 3) (0, 8)
(0, -10) (0, 0) (8, 4)
(-3, -9) (-2, 1) (5, 5)
(-1, -6) (-6, 3) (10, 5)
The two (-7, -10)s appear together, so do the two (10, 5)s. After removal of duplicates, the remaining points are copied over to the array pointsNoDuplicate[]:
(-7, -10) (-10, 0) (7, 3)
(0, -10) (0, 0) (8, 4)
(-3, -9) (-2, 1) (5, 5)
(-1, -6) (-6, 3) (10, 5)
(5, -2) (3, 3) (0, 8)
The variable lowestPoint is set to (-7, -10).
The array pointsNoDuplicate[] will be the input of a convex hull algorithm. The class
ConvexHull has an abstract method constructHull() which will be implemented by its subclasses to carry out convex hull construction using either Graham’s scan or Jarvis’ march.
1
|
public abstract void constructHull();
|
The vertices of the constructed convex hull will be stored in the array hullVertices[] in counterclockwise order starting with lowestPoint.
Convex Hull Construction
Two algorithms, Graham’s scan and Jarvis’ march, are respectively implemented by the subclasses GrahamScan and JarvisMarch of the abstract class ConvexHull. Both subclasses must handle the special case of one or two points only in the array pointsNoDuplicates[], where the corresponding convex hull is the sole point or the segment connecting the two points.
Graham’s scan
The class GrahamScan sorts all the points in points[] by polar angle with respect to lowestPoint. Point comparison is done using a Comparator object gener⁴⁴†⁃⁴䘨⡳⥴⁐⁴†††㱮⽣㸮ഠਨ㱉㸠††⁴†††⁴⁰⸠㱮⽯㸠ൡ੮㱹㹴⁵⁴⁊⡵⥬
‾䌍䡯†Ɱ⁶䡴⠠⥱⁵ⱳ†㩣㱴⼠㹴൨㰠㵮≴≥㸠㱩⽴㹴൨㰠㹳൯ੲ⁴
†㱭㸠൵ੳ†⁴†㱐㹡൲ੁ⁃⁰⁴㱯㴠≭≥㹤ഠ੩†⁴†⁶†㱰㹧㱲†㵯≭≴㹯ㅲ㰠⽵㹬㱯⽴㹐㱯⽩㹡൳ਠ⁴†㱥⼠㹩൮ੴ†⁎⁴†⁴⁴†㱱㴠≨≨㹥ഠ⁸⁰⁴†⁵㱩㹴㱩†㴠≡≨㹥䙣⡲䑩Ⱐ⁷ⱬ†䍰⡶⥥⡥⤠⥥㭸㱰⽥㸠㱡†⽷㹯㱲⽳㹡൳†⁵†⁴㱥⽳㹯൲ਠ⁴†⁰㱬⽥㹮൴⁴㰠⽯㹨ൡ੭㰦⽲㹳ഠੳ㱣⽡㸠൦੯㱲㹣†⁴⁴‼†ⱥ†⹓
†䌠⡹⤠⡄⥵⁰⁛‵⁴䙨‱†††‵†††…※⁷†⁰⸠⁵䑳†⁴⁓⁴⁵⁶づⱸ††ⱥ†㱣⽫㸠੯㱰㹴䙨†††ㅥⰠ⁴†㕥⁴⁴⠠⁴††⤬㨠㱴⽨㸠੬㱥㹴㱳㹴⡨⡥ⴠ㝡Ⱳⵡㅹ〬⤠Ⱪ⠠どⱮㅥち⥳⥩൮੧⠠⡩のⱤㄬ〠⥡Ⱳ⠠ㅴとⱥ†㕨⥵⥬൬ਠ⡶⡥ㅲぴⱩ㕥⥳Ⱐ⡮〠Ᵽ㡵⥮⥴ੲ⡣⡬はⱣ㡷⥩ⱳ⠠ⵯㅲつⱥ〮⤼⤯൰ਾ⠍⠊ⴼㅨ〳Ⱐつ⤽Ⱒ⁊⡡㝶Ⱪㅲび⥱⥵൯㰭⽍㸢㰾⽊㹩൳ਦ㱲㉱⁵㴠≍䍡ⴳ䌾ⵔ䡨ⵡ䅬≦㸠䍴⁴䌠䡮⁴䅹㱬⽥㉥㹮൴㱤㹩䍳䡳䅣†䝰♥㭡䩥☠㭦⁴⁴‾⸠‼䥴†‾†††⁴•ⱥⰢ‾†⠠⤠†⡡⥮†‾‼‾ ⰾ†† †⁴•‾††‽䔢⁶⁴㩩㱮⽴㸼യੳ㱰㸾഼† ‾㰼㹡⤠䍬⁶ⰾ‼†㰠⽣㹳൳•㱥㹰⥩⁶䙡†⁓䝫 䩳䴼ⰾ‼ ††⁴ † †⸊㰼⼯㹢൬‾ ‼㰯㸍⤼⁰䠾††䡥⡴⥐††㱭⼠㹧൨㱳⽴㹩൮ੴ㰠㉯⁷㵡≲ⵯⵯ䝩≴㹡䝮㱴⽃㉡㹩൮ਬ㰠㹲⁵ⱬ⁹†㔠っⱯ㕶づ†㕵ぬⱬ†㕶づ㱩⼠㹯൮ਠ†ⴠ⁰ ⴠ†㕩ぢⱥ㔠ぷ⁐†……†ㅁ†䥥⁴†䩴⸊Ⱨ‾‾ ‾‼⁹㰠†⼠㸠഼ੴ㱲†㴠∠≴㹴㱥⽲㸠ഠਠ㰠㸠഼ੰ‾‼㱳㹬ൡੳ‽••㰾㸯൳ੰ‾‼† ‾‼ ⁰㱲㴠∠∼㸯൴‾ †††††††††㰠㹴㱤㵤≥∠㸠ㄠ㰠⼠㸠㰠⁰⽲㹥㰾⼼㹮ഠ‽••‾㰼⽳㹮ഠ‽•⁵⁴㱯‾㴠≣∽㸢൫⁹⁷•‾⁰⁶㱴㹳㱰⁐㴼≳≣㹬⁖㵥㱴㵳≰≳㸽㱲⽡㸨⁐⠩⤼㬯㱳⽰㸯㱳⼾㸼㰯⽳㸾഼† ‾‼ ⁰‾ 㰠⼠㸠ഠਠ†††††‼ ⁴㱤⼾㸠ഠਠ††††㰠⼼㸊ഠਠ㰠⼠㹤൹ਾ㰍⼊㸯൴㱢㹥Ⱟ⁰‼㱥†⽧㹩൶㱮⁵㴠≶⁴≥㹸㱴⼠㸠൴੯㰠㹥ഠੰ⁴㰠㸠൨†⁴†㱡㹥൳ੴ†⁰†㰠⁴㵰≥≶㸮ഠ†⁴†⁴⁵⁴㱩㹷㱨㵬≥≮㹴ㅳ㰠⽡㹮㰠⼠㹳㱭⽡㹳൴ਠ⁰†㰠⽴㸠൯੮†⁴⁴††⁴㱥⁵㵴∠∠㹶ഠ੩†⁴†⁸⁴†⁶㱥㹸㰮㵥∠≡㹲⸠䥨⠠㱣㵮∠≨㹯ㅵぬㅤ㰠⽢㹮⥥ⵡ⁴㱥†㵡≬≲㹁㕮で㱬⽥㹡㱲⽴㸬㰠⽳㹥㰩⼮㸾ਊ‼′†‽•⁄⁰㱬⽡㹴൨⁃⁶⁸㱈⽵㸢ാ⁰㱡⽹㹃൯੮㱶⽥㹬഼ਯ㱨⼲㸍ഊ਼㱰㹔†ⵡ㕧ち㕩の†㱮⽧㸠Please refer to Section 4 in the description of Project 2 for a brief introduction to Swing. The outputs by the two convex hull algorithms are displayed in separate windows. For display,