Skip to content

Commit 59067fc

Browse files
authored
Merge pull request williamfiset#98 from RushabhVasani/master
Solved additional points problem
2 parents 0d75a95 + 2e4f252 commit 59067fc

File tree

1 file changed

+57
-9
lines changed

1 file changed

+57
-9
lines changed

com/williamfiset/algorithms/geometry/ConvexPolygonCutWithLineSegment.java

Lines changed: 57 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,14 @@
11
/**
2-
* This algorithm cuts a convex polygon with a line segment and returns the two resulting pieces.
2+
* This algorithm cuts a ordered convex polygon with a line segment and returns the two resulting pieces.
33
*
4-
* <p>Time Complexity: O(n)
4+
* <p>Time Complexity: O(nlogn)
55
*
66
* @author Finn Lidbetter
77
*/
88
package com.williamfiset.algorithms.geometry;
99

1010
import java.util.*;
11+
import java.lang.*;
1112

1213
public class ConvexPolygonCutWithLineSegment {
1314

@@ -27,6 +28,29 @@ public String toString() {
2728
return "(" + x + "," + y + ")";
2829
}
2930
}
31+
32+
33+
//sorts the points in CW direction.
34+
public static List<Pt> sortCW(List<Pt> poly){
35+
36+
int l = poly.size();
37+
double centroidX = 0;
38+
double centroidY = 0;
39+
for(int i = 0; i<l; i++){
40+
centroidX+= poly.get(i).x;
41+
centroidY+= poly.get(i).y;
42+
}
43+
centroidX = centroidX/l;
44+
centroidY = centroidY/l;
45+
Pt center = new Pt(centroidX, centroidY);
46+
47+
Collections.sort(poly, (a, b) -> {
48+
double a1 = (Math.toDegrees(Math.atan2(a.x - center.x, a.y - center.y)) + 360) % 360;
49+
double a2 = (Math.toDegrees(Math.atan2(b.x - center.x, b.y - center.y)) + 360) % 360;
50+
return (int) (a1 - a2);
51+
});
52+
return poly;
53+
}
3054

3155
// Cuts a convex polygon by a specified line and returns one part
3256
// of the polygon (swapping the endpoints p1 and p2 of the line
@@ -61,33 +85,57 @@ private static int orientation(double ax, double ay, double bx, double by, doubl
6185
double cross = bx * cy - by * cx;
6286
return cross < -EPS ? -1 : cross > EPS ? 1 : 0;
6387
}
88+
89+
//takes Pt[] as an argument and returns List<Pt>
90+
public static List<Pt> makeList(Pt[] squarePolygon){
91+
List<Pt> list = new ArrayList<Pt>();
92+
for (int i=0; i<squarePolygon.length; i++){
93+
list.add(squarePolygon[i]);
94+
}
95+
return list;
96+
}
97+
98+
//takes List<Pt> as an argument and returns Pt[]
99+
public static Pt[] makeArray(List<Pt> list){
100+
int l = list.size();
101+
Pt[] temp = new Pt[l];
102+
for (int i=0; i<l; i++){
103+
temp[i] = list.get(i);
104+
}
105+
return temp;
106+
}
64107

65108
// Example usage
66109
public static void main(String[] args) {
67-
68-
Pt[] squarePolygon = {new Pt(0, 0), new Pt(0, 4), new Pt(4, 0), new Pt(4, 4)};
110+
111+
Pt[] squarePolygon = {new Pt(0, 0), new Pt(0, 4), new Pt(4, 0), new Pt(4, 4), new Pt(0, 2)};
69112
Pt p1 = new Pt(-1, -1);
70113
Pt p2 = new Pt(5, 5);
71-
114+
115+
int l = squarePolygon.length;
116+
List list = makeList(squarePolygon);
117+
list = sortCW(list);
118+
squarePolygon = makeArray(list);
119+
72120
Pt[] poly1 = cut(squarePolygon, p1, p2);
73121
Pt[] poly2 = cut(squarePolygon, p2, p1);
74122

75123
System.out.println("First polygon:");
76124
for (Pt pt : poly1) System.out.println(pt);
77125
// Prints:
78126
// First polygon:
127+
// (0.0,4.0)
79128
// (4.0,4.0)
80129
// (0.0,0.0)
81-
// (0.0,4.0)
82-
// (2.0,2.0) <-- Probably should not be here?
130+
// (0.0,2.0)
83131

84132
System.out.println("\nSecond polygon:");
85133
for (Pt pt : poly2) System.out.println(pt);
134+
//Prints:
86135
// Second polygon:
87136
// (4.0,4.0)
88-
// (0.0,0.0)
89-
// (2.0,2.0) <-- Probably should not be here?
90137
// (4.0,0.0)
138+
// (0.0,0.0)
91139

92140
}
93141
}

0 commit comments

Comments
 (0)