1
1
/**
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.
3
3
*
4
- * <p>Time Complexity: O(n )
4
+ * <p>Time Complexity: O(nlogn )
5
5
*
6
6
* @author Finn Lidbetter
7
7
*/
8
8
package com .williamfiset .algorithms .geometry ;
9
9
10
10
import java .util .*;
11
+ import java .lang .*;
11
12
12
13
public class ConvexPolygonCutWithLineSegment {
13
14
@@ -27,6 +28,29 @@ public String toString() {
27
28
return "(" + x + "," + y + ")" ;
28
29
}
29
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
+ });
52
+ return poly ;
53
+ }
30
54
31
55
// Cuts a convex polygon by a specified line and returns one part
32
56
// 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
61
85
double cross = bx * cy - by * cx ;
62
86
return cross < -EPS ? -1 : cross > EPS ? 1 : 0 ;
63
87
}
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
+ }
64
107
65
108
// Example usage
66
109
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 ) };
69
112
Pt p1 = new Pt (-1 , -1 );
70
113
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
+
72
120
Pt [] poly1 = cut (squarePolygon , p1 , p2 );
73
121
Pt [] poly2 = cut (squarePolygon , p2 , p1 );
74
122
75
123
System .out .println ("First polygon:" );
76
124
for (Pt pt : poly1 ) System .out .println (pt );
77
125
// Prints:
78
126
// First polygon:
127
+ // (0.0,4.0)
79
128
// (4.0,4.0)
80
129
// (0.0,0.0)
81
- // (0.0,4.0)
82
- // (2.0,2.0) <-- Probably should not be here?
130
+ // (0.0,2.0)
83
131
84
132
System .out .println ("\n Second polygon:" );
85
133
for (Pt pt : poly2 ) System .out .println (pt );
134
+ //Prints:
86
135
// Second polygon:
87
136
// (4.0,4.0)
88
- // (0.0,0.0)
89
- // (2.0,2.0) <-- Probably should not be here?
90
137
// (4.0,0.0)
138
+ // (0.0,0.0)
91
139
92
140
}
93
141
}
0 commit comments