@@ -17,6 +17,8 @@ public sealed class ComplexPolygon : IPath, IPathInternals, IInternalPathOwner
1717 private List < InternalPath > ? internalPaths ;
1818 private float length ;
1919 private RectangleF ? bounds ;
20+ private IPath ? closedPath ;
21+ private LinearGeometry ? linearGeometry ;
2022
2123 /// <summary>
2224 /// Initializes a new instance of the <see cref="ComplexPolygon"/> class.
@@ -97,6 +99,98 @@ public IEnumerable<ISimplePath> Flatten()
9799 return paths ;
98100 }
99101
102+ /// <inheritdoc />
103+ public LinearGeometry ToLinearGeometry ( )
104+ {
105+ if ( this . linearGeometry is not null )
106+ {
107+ return this . linearGeometry ;
108+ }
109+
110+ int pointCount = 0 ;
111+ int contourCount = 0 ;
112+ int segmentCount = 0 ;
113+
114+ bool hasBounds = false ;
115+ float minX = float . MaxValue ;
116+ float minY = float . MaxValue ;
117+ float maxX = float . MinValue ;
118+ float maxY = float . MinValue ;
119+
120+ foreach ( IPath path in this . paths )
121+ {
122+ LinearGeometry geometry = path . ToLinearGeometry ( ) ;
123+
124+ if ( geometry . Info . PointCount == 0 )
125+ {
126+ continue ;
127+ }
128+
129+ RectangleF childBounds = geometry . Info . Bounds ;
130+ minX = MathF . Min ( minX , childBounds . Left ) ;
131+ minY = MathF . Min ( minY , childBounds . Top ) ;
132+ maxX = MathF . Max ( maxX , childBounds . Right ) ;
133+ maxY = MathF . Max ( maxY , childBounds . Bottom ) ;
134+ hasBounds = true ;
135+
136+ pointCount += geometry . Info . PointCount ;
137+ contourCount += geometry . Info . ContourCount ;
138+ segmentCount += geometry . Info . SegmentCount ;
139+ }
140+
141+ PointF [ ] points = new PointF [ pointCount ] ;
142+ LinearContour [ ] contours = new LinearContour [ contourCount ] ;
143+ int pointStart = 0 ;
144+ int contourStart = 0 ;
145+ int segmentStart = 0 ;
146+
147+ foreach ( IPath path in this . paths )
148+ {
149+ LinearGeometry geometry = path . ToLinearGeometry ( ) ;
150+ if ( geometry . Info . PointCount == 0 )
151+ {
152+ continue ;
153+ }
154+
155+ for ( int i = 0 ; i < geometry . Points . Count ; i ++ )
156+ {
157+ points [ pointStart + i ] = geometry . Points [ i ] ;
158+ }
159+
160+ for ( int i = 0 ; i < geometry . Contours . Count ; i ++ )
161+ {
162+ LinearContour contour = geometry . Contours [ i ] ;
163+ contours [ contourStart + i ] = new LinearContour
164+ {
165+ PointStart = pointStart + contour . PointStart ,
166+ PointCount = contour . PointCount ,
167+ SegmentStart = segmentStart + contour . SegmentStart ,
168+ SegmentCount = contour . SegmentCount ,
169+ IsClosed = contour . IsClosed
170+ } ;
171+ }
172+
173+ pointStart += geometry . Info . PointCount ;
174+ contourStart += geometry . Info . ContourCount ;
175+ segmentStart += geometry . Info . SegmentCount ;
176+ }
177+
178+ RectangleF bounds = hasBounds ? RectangleF . FromLTRB ( minX , minY , maxX , maxY ) : RectangleF . Empty ;
179+
180+ this . linearGeometry = new LinearGeometry (
181+ new LinearGeometryInfo
182+ {
183+ Bounds = bounds ,
184+ ContourCount = contours . Length ,
185+ PointCount = points . Length ,
186+ SegmentCount = segmentCount
187+ } ,
188+ contours ,
189+ points ) ;
190+
191+ return this . linearGeometry ;
192+ }
193+
100194 /// <inheritdoc/>
101195 public IPath AsClosedPath ( )
102196 {
@@ -105,13 +199,19 @@ public IPath AsClosedPath()
105199 return this ;
106200 }
107201
202+ if ( this . closedPath is not null )
203+ {
204+ return this . closedPath ;
205+ }
206+
108207 IPath [ ] paths = new IPath [ this . paths . Length ] ;
109208 for ( int i = 0 ; i < this . paths . Length ; i ++ )
110209 {
111210 paths [ i ] = this . paths [ i ] . AsClosedPath ( ) ;
112211 }
113212
114- return new ComplexPolygon ( paths ) ;
213+ this . closedPath = new ComplexPolygon ( paths ) ;
214+ return this . closedPath ;
115215 }
116216
117217 /// <inheritdoc/>
0 commit comments