99using System ;
1010using System . Collections ;
1111using System . Collections . Generic ;
12-
1312using System . Linq ;
1413using ClipperLib ;
1514
@@ -35,12 +34,11 @@ public class Polygon
3534 public string name ;
3635
3736 // Windings.
38- [ HideInInspector ] public enum WindingDirection { Unknown , CCW , CW } ;
39- private WindingDirection _windingDirection = WindingDirection . Unknown ;
40- public WindingDirection windingDirection { get { return _windingDirection ; } }
41- public bool isCW { get { return ( _windingDirection == WindingDirection . CW ) ; } }
42- public bool isCCW { get { return ( _windingDirection == WindingDirection . CCW ) ; } }
43-
37+ public bool isCW { get { return ( Mathf . Sign ( _area ) < 0.0f ) ; } }
38+ public bool isCCW { get { return ( Mathf . Sign ( _area ) > 0.0f ) ; } }
39+ public enum Winding { CCW , CW } ;
40+ public Winding winding { get { return ( isCCW ) ? Winding . CCW : Winding . CW ; } }
41+
4442 /// <remarks>
4543 /// For internal use.
4644 /// Edge, Vertex class can read from this directly
@@ -57,12 +55,9 @@ public class Polygon
5755 private Rect _bounds = new Rect ( ) ;
5856 public Rect bounds { get { return _bounds ; } }
5957
60- private float _area ;
58+ private float _area = 0.0f ;
6159 public float area { get { return _area ; } }
6260
63- private float _signedArea ;
64- public float signedArea { get { return _signedArea ; } }
65-
6661 // Yet for single polygons only (see centroid of compound polygons).
6762 private Vector2 _centroid ;
6863 public Vector2 centroid { get { return _centroid ; } }
@@ -75,7 +70,7 @@ public static Polygon PolygonWithPointList(List<Vector2> pointList)
7570
7671 public static Polygon PolygonWithSource ( PolygonSource polygonSource )
7772 {
78- Polygon rootPolygon = Polygon . PolygonWithPointTransforms ( polygonSource . pointTransforms , polygonSource . windingDirection ) ;
73+ Polygon rootPolygon = Polygon . PolygonWithPointTransforms ( polygonSource . pointTransforms ) ;
7974
8075 // Collect sub-olygons if any.
8176 foreach ( Transform eachChildTransform in polygonSource . gameObject . transform )
@@ -112,10 +107,7 @@ public static Polygon PolygonWithPolygon(Polygon polygon)
112107 return rootPolygon ;
113108 }
114109
115- public static Polygon PolygonWithPointTransforms ( Transform [ ] pointTransforms )
116- { return PolygonWithPointTransforms ( pointTransforms , WindingDirection . Unknown ) ; }
117-
118- public static Polygon PolygonWithPointTransforms ( Transform [ ] pointTransforms , WindingDirection windingDirection ) // Uses Transform.localPosition.xy()
110+ public static Polygon PolygonWithPointTransforms ( Transform [ ] pointTransforms ) // Uses Transform.localPosition.xy()
119111 {
120112 // Create points array.
121113 Vector2 [ ] points = new Vector2 [ pointTransforms . Length ] ;
@@ -125,19 +117,13 @@ public static Polygon PolygonWithPointTransforms(Transform[] pointTransforms, Wi
125117 points [ index ] = eachPointTransform . localPosition . xy ( ) ;
126118 }
127119
128- return Polygon . PolygonWithPoints ( points , windingDirection ) ;
120+ return Polygon . PolygonWithPoints ( points ) ;
129121 }
130122
131123 public static Polygon PolygonWithPoints ( Vector2 [ ] points )
132- { return PolygonWithPoints ( points , WindingDirection . Unknown ) ; }
133-
134- public static Polygon PolygonWithPoints ( Vector2 [ ] points , WindingDirection windingDirection )
135- { return PolygonWithPoints ( points , 0.0f , windingDirection ) ; }
124+ { return PolygonWithPoints ( points , 0.0f ) ; }
136125
137126 public static Polygon PolygonWithPoints ( Vector2 [ ] points , float angle )
138- { return PolygonWithPoints ( points , angle , WindingDirection . Unknown ) ; }
139-
140- public static Polygon PolygonWithPoints ( Vector2 [ ] points , float angle , WindingDirection windingDirection )
141127 {
142128 Polygon polygon = new Polygon ( points . Length ) ;
143129
@@ -151,10 +137,7 @@ public static Polygon PolygonWithPoints(Vector2[] points, float angle, WindingDi
151137 }
152138
153139 // Polygon calculations.
154- polygon . CalculateBounds ( ) ;
155- polygon . CalculateArea ( ) ;
156- polygon . CalculateWindingDirectionIfNeeded ( ) ;
157- polygon . CalculateCentroid ( ) ;
140+ polygon . Calculate ( ) ;
158141
159142 // Create members.
160143 polygon . CreateVerticesFromPoints ( ) ;
@@ -189,21 +172,15 @@ public void UpdatePointPositionsWithTransforms(Transform[] pointTransforms) // A
189172 }
190173
191174 // Polygon calculations.
192- CalculateBounds ( ) ;
193- CalculateArea ( ) ;
194- CalculateCentroid ( ) ;
195- _windingDirection = WindingDirection . Unknown ;
196- CalculateWindingDirectionIfNeeded ( ) ;
175+ Calculate ( ) ;
197176 }
198177
199178 public void AddPolygon ( Polygon polygon )
200179 {
201180 polygons . Add ( polygon ) ;
202181
203182 // Polygon calculations.
204- CalculateBounds ( ) ;
205- CalculateArea ( ) ;
206- CalculateCentroid ( ) ;
183+ Calculate ( ) ;
207184 }
208185
209186 #endregion
@@ -330,7 +307,14 @@ public void EnumeratePolygons(Action<Polygon> action)
330307
331308 #region Polygon calculations
332309
333- private void CalculateBounds ( )
310+ void Calculate ( )
311+ {
312+ _CalculateBounds ( ) ;
313+ _CalculateArea ( ) ;
314+ _CalculateCentroid ( ) ;
315+ }
316+
317+ void _CalculateBounds ( )
334318 {
335319 float left = float . MaxValue ; // Out in the right
336320 float right = float . MinValue ; // Out in the left
@@ -354,27 +338,15 @@ private void CalculateBounds()
354338 _bounds . yMax = top ;
355339 }
356340
357- private void CalculateArea ( )
341+ private void _CalculateArea ( )
358342 {
359343
360344 // From https://en.wikipedia.org/wiki/Shoelace_formula
361345 Vector2 [ ] points_ = new Vector2 [ _points . Length + 1 ] ;
362-
363- // Create point list for calculations.
364- if ( windingDirection == WindingDirection . CW )
365- {
366- Vector2 [ ] reversed = new Vector2 [ _points . Length ] ;
367- System . Array . Copy ( _points , reversed , _points . Length ) ;
368- System . Array . Copy ( reversed , points_ , _points . Length ) ;
369- }
370-
371- if ( windingDirection == WindingDirection . CCW ||
372- windingDirection == WindingDirection . Unknown )
373- { System . Array . Copy ( _points , points_ , _points . Length ) ; }
374-
346+ System . Array . Copy ( _points , points_ , _points . Length ) ;
375347 points_ [ _points . Length ] = _points [ 0 ] ;
376348
377- // Calculations .
349+ // Calculate area .
378350 float firstProducts = 0.0f ;
379351 float secondProducts = 0.0f ;
380352 for ( int index = 0 ; index < points_ . Length - 1 ; index ++ )
@@ -385,27 +357,17 @@ private void CalculateArea()
385357 firstProducts += eachPoint . x * eachNextPoint . y ;
386358 secondProducts += eachPoint . y * eachNextPoint . x ;
387359 }
388- float area_ = ( firstProducts - secondProducts ) / 2.0f ;
389-
390- // Set signed area.
391- _signedArea = area_ ;
392-
393- // Set area.
394- _area = Mathf . Abs ( area_ ) ;
360+ float _area = ( firstProducts - secondProducts ) / 2.0f ;
395361
396362 // Add / Subtract sub-polygon areas.
397- float subPolygonAreas = 0.0f ;
398363 foreach ( Polygon eachPolygon in polygons )
399364 {
400365 // Outer or inner polygon (supposing there is no self-intersection).
401- float subPolygonArea = eachPolygon . CalculatedArea ( ) ;
402- subPolygonAreas += ( eachPolygon . windingDirection == windingDirection ) ? subPolygonArea : - subPolygonArea ;
366+ _area += eachPolygon . area ;
403367 }
404-
405- _area = _area + subPolygonAreas ;
406368 }
407369
408- private void CalculateCentroid ( )
370+ private void _CalculateCentroid ( )
409371 {
410372 // Enumerate points.
411373 float Σx = 0.0f ;
@@ -423,26 +385,6 @@ private void CalculateCentroid()
423385 // Assign.
424386 _centroid = new Vector2 ( x , y ) ;
425387 }
426-
427- private float CalculatedArea ( )
428- {
429- CalculateArea ( ) ;
430- return _area ;
431- }
432-
433- private Vector2 CalculatedCentroid ( )
434- {
435- CalculateCentroid ( ) ;
436- return _centroid ;
437- }
438-
439- private void CalculateWindingDirectionIfNeeded ( )
440- {
441- if ( _windingDirection == WindingDirection . Unknown ) // Only if unknown
442- {
443- _windingDirection = ( Mathf . Sign ( _signedArea ) > 0.0f ) ? WindingDirection . CCW : WindingDirection . CW ;
444- }
445- }
446388
447389 private void CreateVerticesFromPoints ( )
448390 {
@@ -473,7 +415,7 @@ private void CreateVerticesFromPoints()
473415 firstVertex . SetPreviousVertex ( eachVertex ) ;
474416 }
475417
476- private void CreateEdgesConnectingPoints ( )
418+ void CreateEdgesConnectingPoints ( )
477419 {
478420 // Enumerate vertices.
479421 Edge eachEdge = null ;
@@ -708,10 +650,9 @@ private Polygon PolygonFromClipperPath(Path path, float scale)
708650
709651 #region Geometry features (Transformations)
710652
711- public void RecalculateWindindDirection ( )
653+ public void Reverse ( )
712654 {
713- _windingDirection = WindingDirection . Unknown ;
714- CalculateWindingDirectionIfNeeded ( ) ;
655+ // May be a feature later on (reverse `_points` using `System.Array.Copy()`.
715656 }
716657
717658 public void AlignCentered ( )
@@ -736,9 +677,7 @@ public void Translate(Vector2 translation)
736677 }
737678
738679 // Polygon calculations.
739- CalculateBounds ( ) ;
740- CalculateArea ( ) ;
741- CalculateCentroid ( ) ;
680+ Calculate ( ) ;
742681
743682 // Update (bounds).
744683 // _bounds.position += translation;
@@ -762,9 +701,7 @@ public void Scale(Vector2 scale)
762701 }
763702
764703 // Polygon calculations.
765- CalculateBounds ( ) ;
766- CalculateArea ( ) ;
767- CalculateCentroid ( ) ;
704+ Calculate ( ) ;
768705 }
769706
770707 #endregion
0 commit comments