1
1
/**
2
- * This algorithm cuts a ordered 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
3
+ * pieces.
3
4
*
4
5
* <p>Time Complexity: O(nlogn)
5
6
*
8
9
package com .williamfiset .algorithms .geometry ;
9
10
10
11
import java .util .*;
11
- import java .lang .*;
12
12
13
13
public class ConvexPolygonCutWithLineSegment {
14
14
@@ -28,27 +28,28 @@ public String toString() {
28
28
return "(" + x + "," + y + ")" ;
29
29
}
30
30
}
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
- });
31
+
32
+ // sorts the points in CW direction.
33
+ public static List <Pt > sortCW (List <Pt > poly ) {
34
+
35
+ int l = poly .size ();
36
+ double centroidX = 0 ;
37
+ double centroidY = 0 ;
38
+ for (int i = 0 ; i < l ; i ++) {
39
+ centroidX += poly .get (i ).x ;
40
+ centroidY += poly .get (i ).y ;
41
+ }
42
+ centroidX = centroidX / l ;
43
+ centroidY = centroidY / l ;
44
+ Pt center = new Pt (centroidX , centroidY );
45
+
46
+ Collections .sort (
47
+ poly ,
48
+ (a , b ) -> {
49
+ double a1 = (Math .toDegrees (Math .atan2 (a .x - center .x , a .y - center .y )) + 360 ) % 360 ;
50
+ double a2 = (Math .toDegrees (Math .atan2 (b .x - center .x , b .y - center .y )) + 360 ) % 360 ;
51
+ return (int ) (a1 - a2 );
52
+ });
52
53
return poly ;
53
54
}
54
55
@@ -85,38 +86,38 @@ private static int orientation(double ax, double ay, double bx, double by, doubl
85
86
double cross = bx * cy - by * cx ;
86
87
return cross < -EPS ? -1 : cross > EPS ? 1 : 0 ;
87
88
}
88
-
89
- //takes Pt[] as an argument and returns List<Pt>
90
- public static List <Pt > makeList (Pt [] squarePolygon ){
89
+
90
+ // takes Pt[] as an argument and returns List<Pt>
91
+ public static List <Pt > makeList (Pt [] squarePolygon ) {
91
92
List <Pt > list = new ArrayList <Pt >();
92
- for (int i = 0 ; i < squarePolygon .length ; i ++){
93
- list .add (squarePolygon [i ]);
93
+ for (int i = 0 ; i < squarePolygon .length ; i ++) {
94
+ list .add (squarePolygon [i ]);
94
95
}
95
96
return list ;
96
97
}
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 );
98
+
99
+ // takes List<Pt> as an argument and returns Pt[]
100
+ public static Pt [] makeArray (List <Pt > list ) {
101
+ int l = list .size ();
102
+ Pt [] temp = new Pt [l ];
103
+ for (int i = 0 ; i < l ; i ++) {
104
+ temp [i ] = list .get (i );
104
105
}
105
106
return temp ;
106
107
}
107
108
108
109
// Example usage
109
110
public static void main (String [] args ) {
110
-
111
+
111
112
Pt [] squarePolygon = {new Pt (0 , 0 ), new Pt (0 , 4 ), new Pt (4 , 0 ), new Pt (4 , 4 ), new Pt (0 , 2 )};
112
113
Pt p1 = new Pt (-1 , -1 );
113
114
Pt p2 = new Pt (5 , 5 );
114
-
115
+
115
116
int l = squarePolygon .length ;
116
117
List list = makeList (squarePolygon );
117
118
list = sortCW (list );
118
119
squarePolygon = makeArray (list );
119
-
120
+
120
121
Pt [] poly1 = cut (squarePolygon , p1 , p2 );
121
122
Pt [] poly2 = cut (squarePolygon , p2 , p1 );
122
123
@@ -131,7 +132,7 @@ public static void main(String[] args) {
131
132
132
133
System .out .println ("\n Second polygon:" );
133
134
for (Pt pt : poly2 ) System .out .println (pt );
134
- //Prints:
135
+ // Prints:
135
136
// Second polygon:
136
137
// (4.0,4.0)
137
138
// (4.0,0.0)
0 commit comments