Skip to content

Commit 7598be9

Browse files
authored
Clipper.Core.cs: make USINGZ as local as possible (minor refactoring) (#924)
Keeping USINGZ-enable and USINGS-disabled code close together is going to help in preventing accidental code divergences.
1 parent c1b13f6 commit 7598be9

1 file changed

Lines changed: 63 additions & 112 deletions

File tree

CSharp/Clipper2Lib/Clipper.Core.cs

Lines changed: 63 additions & 112 deletions
Original file line numberDiff line numberDiff line change
@@ -21,47 +21,66 @@ public struct Point64
2121

2222
#if USINGZ
2323
public long Z;
24+
#endif
2425

2526
public Point64(Point64 pt)
2627
{
2728
X = pt.X;
2829
Y = pt.Y;
30+
#if USINGZ
2931
Z = pt.Z;
32+
#endif
3033
}
3134

3235
public Point64(Point64 pt, double scale)
3336
{
3437
X = (long) Math.Round(pt.X * scale, MidpointRounding.AwayFromZero);
3538
Y = (long) Math.Round(pt.Y * scale, MidpointRounding.AwayFromZero);
39+
#if USINGZ
3640
Z = (long) Math.Round(pt.Z * scale, MidpointRounding.AwayFromZero);
41+
#endif
3742
}
3843

39-
public Point64(long x, long y, long z = 0)
40-
{
44+
public Point64(long x, long y
45+
#if USINGZ
46+
, long z = 0
47+
#endif
48+
) {
4149
X = x;
4250
Y = y;
51+
#if USINGZ
4352
Z = z;
53+
#endif
4454
}
4555

46-
public Point64(double x, double y, double z = 0.0)
47-
{
56+
public Point64(double x, double y
57+
#if USINGZ
58+
, double z = 0.0
59+
#endif
60+
) {
4861
X = (long) Math.Round(x, MidpointRounding.AwayFromZero);
4962
Y = (long) Math.Round(y, MidpointRounding.AwayFromZero);
63+
#if USINGZ
5064
Z = (long) Math.Round(z, MidpointRounding.AwayFromZero);
65+
#endif
5166
}
5267

5368
public Point64(PointD pt)
5469
{
5570
X = (long) Math.Round(pt.x, MidpointRounding.AwayFromZero);
5671
Y = (long) Math.Round(pt.y, MidpointRounding.AwayFromZero);
72+
#if USINGZ
5773
Z = pt.z;
74+
#endif
5875
}
5976

6077
public Point64(PointD pt, double scale)
6178
{
6279
X = (long) Math.Round(pt.x * scale, MidpointRounding.AwayFromZero);
6380
Y = (long) Math.Round(pt.y * scale, MidpointRounding.AwayFromZero);
81+
#if USINGZ
6482
Z = pt.z;
83+
#endif
6584
}
6685

6786
public static bool operator ==(Point64 lhs, Point64 rhs)
@@ -76,81 +95,33 @@ public Point64(PointD pt, double scale)
7695

7796
public static Point64 operator +(Point64 lhs, Point64 rhs)
7897
{
79-
return new Point64(lhs.X + rhs.X, lhs.Y + rhs.Y, lhs.Z + rhs.Z);
98+
return new Point64(lhs.X + rhs.X, lhs.Y + rhs.Y
99+
#if USINGZ
100+
, lhs.Z + rhs.Z
101+
#endif
102+
);
80103
}
81104

82105
public static Point64 operator -(Point64 lhs, Point64 rhs)
83106
{
84-
return new Point64(lhs.X - rhs.X, lhs.Y - rhs.Y, lhs.Z - rhs.Z);
107+
return new Point64(lhs.X - rhs.X, lhs.Y - rhs.Y
108+
#if USINGZ
109+
, lhs.Z - rhs.Z
110+
#endif
111+
);
85112
}
86113

87114
public readonly override string ToString()
88115
{
89-
return $"{X},{Y},{Z} "; // nb: trailing space
90-
}
91-
116+
// nb: trailing space
117+
#if USINGZ
118+
return $"{X},{Y},{Z} ";
92119
#else
93-
public Point64(Point64 pt)
94-
{
95-
X = pt.X;
96-
Y = pt.Y;
97-
}
98-
99-
public Point64(long x, long y)
100-
{
101-
X = x;
102-
Y = y;
103-
}
104-
105-
public Point64(double x, double y)
106-
{
107-
X = (long) Math.Round(x, MidpointRounding.AwayFromZero);
108-
Y = (long) Math.Round(y, MidpointRounding.AwayFromZero);
109-
}
110-
111-
public Point64(PointD pt)
112-
{
113-
X = (long) Math.Round(pt.x, MidpointRounding.AwayFromZero);
114-
Y = (long) Math.Round(pt.y, MidpointRounding.AwayFromZero);
115-
}
116-
117-
public Point64(Point64 pt, double scale)
118-
{
119-
X = (long) Math.Round(pt.X * scale, MidpointRounding.AwayFromZero);
120-
Y = (long) Math.Round(pt.Y * scale, MidpointRounding.AwayFromZero);
121-
}
122-
123-
public Point64(PointD pt, double scale)
124-
{
125-
X = (long) Math.Round(pt.x * scale, MidpointRounding.AwayFromZero);
126-
Y = (long) Math.Round(pt.y * scale, MidpointRounding.AwayFromZero);
127-
}
128-
129-
public static bool operator ==(Point64 lhs, Point64 rhs)
130-
{
131-
return lhs.X == rhs.X && lhs.Y == rhs.Y;
132-
}
133-
134-
public static bool operator !=(Point64 lhs, Point64 rhs)
135-
{
136-
return lhs.X != rhs.X || lhs.Y != rhs.Y;
137-
}
138-
139-
public static Point64 operator +(Point64 lhs, Point64 rhs)
140-
{
141-
return new Point64(lhs.X + rhs.X, lhs.Y + rhs.Y);
142-
}
120+
return $"{X},{Y} ";
121+
#endif
143122

144-
public static Point64 operator -(Point64 lhs, Point64 rhs)
145-
{
146-
return new Point64(lhs.X - rhs.X, lhs.Y - rhs.Y);
147-
}
148-
public readonly override string ToString()
149-
{
150-
return $"{X},{Y} "; // nb: trailing space
151123
}
152124

153-
#endif
154125
public readonly override bool Equals(object? obj)
155126
{
156127
if (obj != null && obj is Point64 p)
@@ -172,97 +143,77 @@ public struct PointD
172143

173144
#if USINGZ
174145
public long z;
146+
#endif
175147

176148
public PointD(PointD pt)
177149
{
178150
x = pt.x;
179151
y = pt.y;
152+
#if USINGZ
180153
z = pt.z;
154+
#endif
181155
}
182156

183157
public PointD(Point64 pt)
184158
{
185159
x = pt.X;
186160
y = pt.Y;
161+
#if USINGZ
187162
z = pt.Z;
163+
#endif
188164
}
189165

190166
public PointD(Point64 pt, double scale)
191167
{
192168
x = pt.X * scale;
193169
y = pt.Y * scale;
170+
#if USINGZ
194171
z = pt.Z;
172+
#endif
195173
}
196174

197175
public PointD(PointD pt, double scale)
198176
{
199177
x = pt.x * scale;
200178
y = pt.y * scale;
179+
#if USINGZ
201180
z = pt.z;
181+
#endif
202182
}
203183

204-
public PointD(long x, long y, long z = 0)
205-
{
184+
public PointD(long x, long y
185+
#if USINGZ
186+
, long z = 0
187+
#endif
188+
) {
206189
this.x = x;
207190
this.y = y;
191+
#if USINGZ
208192
this.z = z;
193+
#endif
209194
}
210195

211-
public PointD(double x, double y, long z = 0)
212-
{
196+
public PointD(double x, double y
197+
#if USINGZ
198+
, long z = 0
199+
#endif
200+
) {
213201
this.x = x;
214202
this.y = y;
203+
#if USINGZ
215204
this.z = z;
205+
#endif
216206
}
217207

218208
public readonly string ToString(int precision = 2)
219209
{
210+
#if USINGZ
220211
return string.Format($"{{0:F{precision}}},{{1:F{precision}}},{{2:D}}", x,y,z);
221-
}
222-
223212
#else
224-
public PointD(PointD pt)
225-
{
226-
x = pt.x;
227-
y = pt.y;
228-
}
229-
230-
public PointD(Point64 pt)
231-
{
232-
x = pt.X;
233-
y = pt.Y;
234-
}
235-
236-
public PointD(PointD pt, double scale)
237-
{
238-
x = pt.x * scale;
239-
y = pt.y * scale;
240-
}
241-
242-
public PointD(Point64 pt, double scale)
243-
{
244-
x = pt.X * scale;
245-
y = pt.Y * scale;
246-
}
247-
248-
public PointD(long x, long y)
249-
{
250-
this.x = x;
251-
this.y = y;
252-
}
253-
254-
public PointD(double x, double y)
255-
{
256-
this.x = x;
257-
this.y = y;
258-
}
259-
260-
public readonly string ToString(int precision = 2)
261-
{
262213
return string.Format($"{{0:F{precision}}},{{1:F{precision}}}", x,y);
214+
#endif
263215
}
264216

265-
#endif
266217
public static bool operator ==(PointD lhs, PointD rhs)
267218
{
268219
return InternalClipper.IsAlmostZero(lhs.x - rhs.x) &&

0 commit comments

Comments
 (0)