1 import java
.io
.BufferedWriter
;
3 import java
.io
.FileWriter
;
4 import java
.io
.IOException
;
5 import java
.text
.SimpleDateFormat
;
6 import java
.util
.ArrayList
;
7 import java
.util
.Collections
;
9 import java
.util
.Random
;
13 * @author Johan Kayser, Romain Chanoir
16 * Runs the benchmarks for the most important operations and generates a file
17 * with time execution results
19 public class BenchPolygon
{
21 public static void main(String
[] args
) throws IOException
{
24 n
= Integer
.parseInt(args
[1]);
53 * addVertex bench: adds a vertex to a polygon with the given number of
56 public static void testAddVertex(int nb
) throws IOException
{
57 ArrayList
<PointDouble
> points
= new ArrayList
<>();
58 ArrayList
<PointDouble
> randomPoints
= new ArrayList
<>();
59 randomPoints
= generatePoints(nb
+ 1);
61 for (int i
= 0; i
< nb
; ++i
) {
62 points
.add(randomPoints
.remove(0));
64 ConvexPolygon test
= new ConvexPolygon(points
);
65 test
.sortVertices(new AntiClockSort(test
.getVertices()));
67 test
.addVertex(randomPoints
.remove(0));
71 * sortVertices bench: sorts the given number of vertices in the ArrayList
74 public static void testSortVertices(int nb
) throws IOException
{
75 ArrayList
<PointDouble
> randomPoints
= new ArrayList
<>();
76 randomPoints
= generatePoints(nb
);
77 Collections
.shuffle(randomPoints
);
79 ConvexPolygon test
= new ConvexPolygon(randomPoints
);
80 test
.sortVertices(new AntiClockSort(test
.getVertices()));
85 * intersects bench: tests the intersection between two polygons with the
86 * given number of vertices
88 public static void testIntersects(int nb
) throws IOException
{
89 ArrayList
<PointDouble
> points1
= new ArrayList
<>();
90 ArrayList
<PointDouble
> points2
= new ArrayList
<>();
91 points1
= generatePoints(nb
);
92 points2
= generatePoints(nb
);
93 ConvexPolygon test1
= new ConvexPolygon(points1
);
94 ConvexPolygon test2
= new ConvexPolygon(points2
);
95 test1
.sortVertices(new AntiClockSort(test1
.getVertices()));
96 test2
.sortVertices(new AntiClockSort(test2
.getVertices()));
98 Boolean rez
= test1
.intersects(test2
);
102 * convexHull bench: gets the convex hull of the given number of points
104 public static void testConvexHull(int nb
) throws IOException
{
105 ArrayList
<PointDouble
> randomPoints
= new ArrayList
<>();
106 randomPoints
= generatePoints(nb
);
107 Collections
.shuffle(randomPoints
);
108 ConvexPolygon test
= new ConvexPolygon(randomPoints
);
110 ConvexPolygon rez
= test
.convexHull(randomPoints
);
114 * isConvex bench: checks if the polygon with the given number of vertices
115 * is convex (we test the worst case -> polygon vertices are ordered)
117 public static void testIsConvex(int nb
) throws IOException
{
118 ArrayList
<PointDouble
> randomPoints
= new ArrayList
<>();
119 randomPoints
= generatePoints(nb
);
120 ConvexPolygon test
= new ConvexPolygon(randomPoints
);
121 test
.sortVertices(new AntiClockSort(test
.getVertices()));
123 Boolean rez
= test
.isConvex();
127 * contain bench: checks if the polygon with the given number of vertices
128 * contains a randomly generated point
130 public static void testContain(int nb
) throws IOException
{
131 ArrayList
<PointDouble
> randomPoints
= new ArrayList
<>();
132 randomPoints
= generatePoints(nb
);
133 ConvexPolygon test
= new ConvexPolygon(randomPoints
);
134 test
.sortVertices(new AntiClockSort(test
.getVertices()));
136 Boolean rez
= test
.contain(new PointDouble(0.0, 0.0));
140 * generates some points making it easier to use convex polygons
142 public static ArrayList
<PointDouble
> generatePoints(int nb
) {
143 ArrayList
<PointDouble
> pts
= new ArrayList
<>();
144 pts
= PointDouble
.getNPointsOnCircle(100.0, nb
);