Skip to content

Commit 8916ed1

Browse files
committed
C# refactor / code tidy
1 parent 5f34747 commit 8916ed1

9 files changed

Lines changed: 73 additions & 68 deletions

File tree

CSharp/Clipper2Lib.Benchmark/Benchmarks.cs

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -35,11 +35,11 @@ public class Benchmarks
3535
[GlobalSetup]
3636
public void GlobalSetup()
3737
{
38-
Random rand = new Random();
38+
Random rand = new ();
3939

40-
_subj = new Paths64();
41-
_clip = new Paths64();
42-
_solution = new Paths64();
40+
_subj = new ();
41+
_clip = new ();
42+
_solution = new ();
4343

4444
_subj.Add(MakeRandomPath(DisplayWidth, DisplayHeight, EdgeCount, rand));
4545
_clip.Add(MakeRandomPath(DisplayWidth, DisplayHeight, EdgeCount, rand));
@@ -48,7 +48,7 @@ public void GlobalSetup()
4848
[Benchmark]
4949
public void Intersection_N()
5050
{
51-
Clipper64 c = new Clipper64();
51+
Clipper64 c = new ();
5252
c.AddSubject(_subj);
5353
c.AddClip(_clip);
5454
c.Execute(ClipType.Intersection, FillRule.NonZero, _solution);
@@ -67,7 +67,7 @@ public void Union_N()
6767
[Benchmark]
6868
public void Difference_N()
6969
{
70-
Clipper c = new Clipper();
70+
Clipper c = new ();
7171
c.AddSubject(_subj);
7272
c.AddClip(_clip);
7373
c.Execute(ClipType.Difference, FillRule.NonZero, _solution);
@@ -76,7 +76,7 @@ public void Difference_N()
7676
[Benchmark]
7777
public void Xor_N()
7878
{
79-
Clipper c = new Clipper();
79+
Clipper c = new ();
8080
c.AddSubject(_subj);
8181
c.AddClip(_clip);
8282
c.Execute(ClipType.Xor, FillRule.NonZero, _solution);
@@ -85,13 +85,13 @@ public void Xor_N()
8585
private static Point64 MakeRandomPt(int maxWidth, int maxHeight, Random rand)
8686
{
8787
long x = rand.Next(maxWidth);
88-
var y = rand.Next(maxHeight);
88+
long y = rand.Next(maxHeight);
8989
return new Point64(x, y);
9090
}
9191

9292
public static Path64 MakeRandomPath(int width, int height, int count, Random rand)
9393
{
94-
Path64 result = new Path64(count);
94+
Path64 result = new (count);
9595
for (int i = 0; i < count; ++i)
9696
result.Add(MakeRandomPt(width, height, rand));
9797
return result;

CSharp/Clipper2Lib.Examples/ConsoleDemo/Main.cs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -23,15 +23,15 @@ public static void Main()
2323
}
2424
public static void ClipSimpleShapes()
2525
{
26-
Paths64 subject = new Paths64();
27-
Paths64 clip = new Paths64();
26+
Paths64 subject = new ();
27+
Paths64 clip = new ();
2828
FillRule fillrule = FillRule.NonZero;
2929

3030
subject.Add(Clipper.MakePath(new int[] { 100, 50, 10, 79, 65, 2, 65, 98, 10, 21 }));
3131
clip.Add(Clipper.MakePath(new int[] { 98, 63, 4, 68, 77, 8, 52, 100, 19, 12 }));
3232
Paths64 solution = Clipper.Intersect(subject, clip, fillrule);
3333

34-
SimpleSvgWriter svg = new SimpleSvgWriter();
34+
SimpleSvgWriter svg = new ();
3535
SvgUtils.AddSubject(svg, subject);
3636
SvgUtils.AddClip(svg, clip);
3737
SvgUtils.SaveToFile(svg, "..\\..\\..\\clipperA.svg", fillrule, 400, 300, 20);
@@ -52,7 +52,7 @@ public static void ClipTestPolys()
5252
Paths64 clip = LoadPathsFromResource("ConsoleDemo.clip.bin");
5353
Paths64 solution = Clipper.Intersect(subject, clip, fillrule);
5454

55-
SimpleSvgWriter svg = new SimpleSvgWriter();
55+
SimpleSvgWriter svg = new ();
5656
SvgUtils.AddSubject(svg, subject);
5757
SvgUtils.AddClip(svg, clip);
5858
SvgUtils.SaveToFile(svg, "..\\..\\..\\clipperC.svg", fillrule, 800, 600, 20);
@@ -71,13 +71,13 @@ public static Paths64 LoadPathsFromResource(string resourceName)
7171
using Stream stream = Assembly.GetExecutingAssembly().
7272
GetManifestResourceStream(resourceName);
7373
if (stream == null) return new Paths64();
74-
using BinaryReader reader = new BinaryReader(stream);
74+
using BinaryReader reader = new (stream);
7575
int len = reader.ReadInt32();
76-
Paths64 result = new Paths64(len);
76+
Paths64 result = new (len);
7777
for (int i = 0; i < len; i++)
7878
{
7979
int len2 = reader.ReadInt32();
80-
Path64 p = new Path64(len2);
80+
Path64 p = new (len2);
8181
for (int j = 0; j < len2; j++)
8282
{
8383
long X = reader.ReadInt64();

CSharp/Clipper2Lib.Examples/InflateDemo/Main.cs

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -26,9 +26,8 @@ public static void Main()
2626
public static void DoSimpleShapes()
2727
{
2828
//triangle offset - with large miter
29-
Paths64 p = new Paths64();
30-
p.Add(Clipper.MakePath(new int[] { 30, 150, 60, 350, 0, 350 }));
31-
Paths64 pp = new Paths64();
29+
Paths64 p = new() { Clipper.MakePath(new int[] { 30, 150, 60, 350, 0, 350 }) };
30+
Paths64 pp = new ();
3231
pp.AddRange(p);
3332

3433
for (int i = 0; i < 5; ++i)
@@ -44,15 +43,15 @@ public static void DoSimpleShapes()
4443
pp.AddRange(p);
4544
//nb: using the ClipperOffest class directly here to control
4645
//different join types within the same offset operation
47-
ClipperOffset co = new ClipperOffset();
46+
ClipperOffset co = new ();
4847
co.AddPaths(p, JoinType.Miter, EndType.Joined);
4948
p = Clipper.TranslatePaths(p, 120, 100);
5049
pp.AddRange(p);
5150
co.AddPaths(p, JoinType.Round, EndType.Joined);
5251
p = co.Execute(20);
5352
pp.AddRange(p);
5453

55-
SimpleSvgWriter svg = new SimpleSvgWriter();
54+
SimpleSvgWriter svg = new ();
5655
SvgUtils.AddSolution(svg, pp, false);
5756
SvgUtils.SaveToFile(svg, "../../../inflate.svg", FillRule.EvenOdd, 800, 600, 20);
5857
ClipperFileIO.OpenFileWithDefaultApp("../../../inflate.svg");
@@ -62,7 +61,7 @@ public static void DoRabbit()
6261
{
6362
PathsD pd = LoadPathsFromResource("InflateDemo.rabbit.bin");
6463

65-
PathsD solution = new PathsD(pd);
64+
PathsD solution = new (pd);
6665
while (pd.Count > 0)
6766
{
6867
//don't forget to scale the delta offset
@@ -73,7 +72,7 @@ public static void DoRabbit()
7372
solution.AddRange(pd);
7473
}
7574

76-
SimpleSvgWriter svg = new SimpleSvgWriter();
75+
SimpleSvgWriter svg = new ();
7776
SvgUtils.AddSolution(svg, solution, false);
7877
SvgUtils.SaveToFile(svg, "../../../rabbit2.svg", FillRule.EvenOdd, 450, 720, 10);
7978
ClipperFileIO.OpenFileWithDefaultApp("../../../rabbit2.svg");
@@ -84,13 +83,13 @@ public static PathsD LoadPathsFromResource(string resourceName)
8483
using Stream stream = Assembly.GetExecutingAssembly().
8584
GetManifestResourceStream(resourceName);
8685
if (stream == null) return new PathsD();
87-
using BinaryReader reader = new BinaryReader(stream);
86+
using BinaryReader reader = new (stream);
8887
int len = reader.ReadInt32();
89-
PathsD result = new PathsD(len);
88+
PathsD result = new (len);
9089
for (int i = 0; i < len; i++)
9190
{
9291
int len2 = reader.ReadInt32();
93-
PathD p = new PathD(len2);
92+
PathD p = new (len2);
9493
for (int j = 0; j < len2; j++)
9594
{
9695
long X = reader.ReadInt64();

CSharp/Clipper2Lib/Clipper.Core.cs

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
* License : http://www.boost.org/LICENSE_1_0.txt *
99
*******************************************************************************/
1010

11+
#nullable enable
1112
using System;
1213
using System.Collections.Generic;
1314
using System.Runtime.CompilerServices;
@@ -151,9 +152,9 @@ public override string ToString()
151152
}
152153

153154
#endif
154-
public override bool Equals(object obj)
155+
public override bool Equals(object? obj)
155156
{
156-
if (obj is Point64 p)
157+
if (obj != null && obj is Point64 p)
157158
return this == p;
158159
return false;
159160
}
@@ -271,9 +272,9 @@ public override string ToString()
271272
!InternalClipper.IsAlmostZero(lhs.y - rhs.y);
272273
}
273274

274-
public override bool Equals(object obj)
275+
public override bool Equals(object? obj)
275276
{
276-
if (obj is PointD p)
277+
if (obj != null && obj is PointD p)
277278
return this == p;
278279
return false;
279280
}
@@ -342,11 +343,13 @@ public bool Contains(Rect64 rec)
342343

343344
public Path64 AsPath()
344345
{
345-
Path64 result = new Path64(4);
346-
result.Add(new Point64(left, top));
347-
result.Add(new Point64(right, top));
348-
result.Add(new Point64(right, bottom));
349-
result.Add(new Point64(left, bottom));
346+
Path64 result = new Path64(4)
347+
{
348+
new Point64(left, top),
349+
new Point64(right, top),
350+
new Point64(right, bottom),
351+
new Point64(left, bottom)
352+
};
350353
return result;
351354
}
352355

CSharp/Clipper2Lib/Clipper.Engine.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
* License : http://www.boost.org/LICENSE_1_0.txt *
1111
*******************************************************************************/
1212

13+
#nullable enable
1314
using System;
1415
using System.Collections;
1516
using System.Collections.Generic;

CSharp/Clipper2Lib/Clipper.Offset.cs

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -146,7 +146,7 @@ internal static PointD GetUnitNormal(Point64 pt1, Point64 pt2)
146146
}
147147

148148
[MethodImpl(MethodImplOptions.AggressiveInlining)]
149-
private int GetLowestPolygonIdx(Paths64 paths)
149+
private static int GetLowestPolygonIdx(Paths64 paths)
150150
{
151151
Point64 lp = new Point64(0, long.MinValue);
152152
int result = -1;
@@ -161,31 +161,31 @@ private int GetLowestPolygonIdx(Paths64 paths)
161161
}
162162

163163
[MethodImpl(MethodImplOptions.AggressiveInlining)]
164-
private PointD TranslatePoint(PointD pt, double dx, double dy)
164+
private static PointD TranslatePoint(PointD pt, double dx, double dy)
165165
{
166166
return new PointD(pt.x + dx, pt.y + dy);
167167
}
168168

169169
[MethodImpl(MethodImplOptions.AggressiveInlining)]
170-
private PointD ReflectPoint(PointD pt, PointD pivot)
170+
private static PointD ReflectPoint(PointD pt, PointD pivot)
171171
{
172172
return new PointD(pivot.x + (pivot.x - pt.x), pivot.y + (pivot.y - pt.y));
173173
}
174174

175175
[MethodImpl(MethodImplOptions.AggressiveInlining)]
176-
private bool AlmostZero(double value, double epsilon = 0.001)
176+
private static bool AlmostZero(double value, double epsilon = 0.001)
177177
{
178178
return Math.Abs(value) < epsilon;
179179
}
180180

181181
[MethodImpl(MethodImplOptions.AggressiveInlining)]
182-
private double Hypotenuse(double x, double y)
182+
private static double Hypotenuse(double x, double y)
183183
{
184184
return Math.Sqrt(Math.Pow(x, 2) + Math.Pow(y, 2));
185185
}
186186

187187
[MethodImpl(MethodImplOptions.AggressiveInlining)]
188-
private PointD NormalizeVector(PointD vec)
188+
private static PointD NormalizeVector(PointD vec)
189189
{
190190
double h = Hypotenuse(vec.x, vec.y);
191191
if (AlmostZero(h)) return new PointD(0,0);
@@ -195,13 +195,13 @@ private PointD NormalizeVector(PointD vec)
195195

196196

197197
[MethodImpl(MethodImplOptions.AggressiveInlining)]
198-
private PointD GetAvgUnitVector(PointD vec1, PointD vec2)
198+
private static PointD GetAvgUnitVector(PointD vec1, PointD vec2)
199199
{
200200
return NormalizeVector(new PointD(vec1.x + vec2.x, vec1.y + vec2.y));
201201
}
202202

203203
[MethodImpl(MethodImplOptions.AggressiveInlining)]
204-
private PointD IntersectPoint(PointD pt1a, PointD pt1b, PointD pt2a, PointD pt2b)
204+
private static PointD IntersectPoint(PointD pt1a, PointD pt1b, PointD pt2a, PointD pt2b)
205205
{
206206
if (InternalClipper.IsAlmostZero(pt1a.x - pt1b.x)) //vertical
207207
{
@@ -443,7 +443,7 @@ private void OffsetOpenPath(Group group, Path64 path, EndType endType)
443443
}
444444

445445
[MethodImpl(MethodImplOptions.AggressiveInlining)]
446-
private bool IsFullyOpenEndType(EndType et)
446+
private static bool IsFullyOpenEndType(EndType et)
447447
{
448448
return (et != EndType.Polygon) && (et != EndType.Joined);
449449
}

CSharp/Clipper2Lib/Clipper.cs

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -25,12 +25,15 @@ namespace Clipper2Lib
2525
public static class Clipper
2626
{
2727

28-
public static Rect64 MaxInvalidRect64 = new Rect64(
28+
private static Rect64 maxInvalidRect64 = new Rect64(
2929
long.MaxValue, long.MaxValue, long.MinValue, long.MinValue);
3030

31-
public static RectD MaxInvalidRectD = new RectD(
31+
private static RectD maxInvalidRectD = new RectD(
3232
double.MaxValue, -double.MaxValue, -double.MaxValue, -double.MaxValue);
3333

34+
public static Rect64 MaxInvalidRect64 => maxInvalidRect64;
35+
public static RectD MaxInvalidRectD => maxInvalidRectD;
36+
3437
public static Paths64 Intersect(Paths64 subject, Paths64 clip, FillRule fillRule)
3538
{
3639
return BooleanOp(ClipType.Intersection, subject, clip, fillRule);
@@ -206,28 +209,28 @@ public static string Path64ToString(Path64 path)
206209
{
207210
string result = "";
208211
foreach (Point64 pt in path)
209-
result = result + pt.ToString();
212+
result += pt.ToString();
210213
return result + '\n';
211214
}
212215
public static string Paths64ToString(Paths64 paths)
213216
{
214217
string result = "";
215218
foreach (Path64 path in paths)
216-
result = result + Path64ToString(path);
219+
result += Path64ToString(path);
217220
return result;
218221
}
219222
public static string PathDToString(PathD path)
220223
{
221224
string result = "";
222225
foreach (PointD pt in path)
223-
result = result + pt.ToString();
226+
result += pt.ToString();
224227
return result + '\n';
225228
}
226229
public static string PathsDToString(PathsD paths)
227230
{
228231
string result = "";
229232
foreach (PathD path in paths)
230-
result = result + PathDToString(path);
233+
result += PathDToString(path);
231234
return result;
232235
}
233236
public static Path64 OffsetPath(Path64 path, long dx, long dy)
@@ -575,7 +578,7 @@ public static void AddPolyNodeToPathsD(PolyPathD polyPath, PathsD paths)
575578
public static PathsD PolyTreeToPathsD(PolyTreeD polyTree)
576579
{
577580
PathsD result = new PathsD();
578-
foreach (var polyPathBase in polyTree)
581+
foreach (PolyPathBase polyPathBase in polyTree)
579582
{
580583
PolyPathD p = (PolyPathD)polyPathBase;
581584
AddPolyNodeToPathsD(p, result);
@@ -720,7 +723,7 @@ public static Path64 TrimCollinear(Path64 path, bool isOpen = false)
720723
else
721724
{
722725
while (result.Count > 2 && InternalClipper.CrossProduct(
723-
result[result.Count - 1], result[result.Count - 2], result[0]) == 0)
726+
result[^1], result[^2], result[0]) == 0)
724727
result.RemoveAt(result.Count - 1);
725728
if (result.Count < 3)
726729
result.Clear();
@@ -754,8 +757,7 @@ public static Path64 Ellipse(Point64 center,
754757
double si = Math.Sin(2 * Math.PI / steps);
755758
double co = Math.Cos(2 * Math.PI / steps);
756759
double dx = co, dy = si;
757-
Path64 result = new Path64(steps);
758-
result.Add(new Point64(center.X + radiusX, center.Y));
760+
Path64 result = new Path64(steps) { new Point64(center.X + radiusX, center.Y) };
759761
for (int i = 1; i < steps; ++i)
760762
{
761763
result.Add(new Point64(center.X + radiusX * dx, center.Y + radiusY * dy));
@@ -777,8 +779,7 @@ public static PathD Ellipse(PointD center,
777779
double si = Math.Sin(2 * Math.PI / steps);
778780
double co = Math.Cos(2 * Math.PI / steps);
779781
double dx = co, dy = si;
780-
PathD result = new PathD(steps);
781-
result.Add(new PointD(center.x + radiusX, center.y));
782+
PathD result = new PathD(steps) { new PointD(center.x + radiusX, center.y) };
782783
for (int i = 1; i < steps; ++i)
783784
{
784785
result.Add(new PointD(center.x + radiusX * dx, center.y + radiusY * dy));

0 commit comments

Comments
 (0)