Skip to content

Commit

Permalink
Clipper.Core.cs: make USINGZ as local as possible (minor refactoring) (
Browse files Browse the repository at this point in the history
…#924)

Keeping USINGZ-enable and USINGS-disabled code close together is going to
help in preventing accidental code divergences.
  • Loading branch information
firegurafiku authored Dec 21, 2024
1 parent c1b13f6 commit 7598be9
Showing 1 changed file with 63 additions and 112 deletions.
175 changes: 63 additions & 112 deletions CSharp/Clipper2Lib/Clipper.Core.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,47 +21,66 @@ public struct Point64

#if USINGZ
public long Z;
#endif

public Point64(Point64 pt)
{
X = pt.X;
Y = pt.Y;
#if USINGZ
Z = pt.Z;
#endif
}

public Point64(Point64 pt, double scale)
{
X = (long) Math.Round(pt.X * scale, MidpointRounding.AwayFromZero);
Y = (long) Math.Round(pt.Y * scale, MidpointRounding.AwayFromZero);
#if USINGZ
Z = (long) Math.Round(pt.Z * scale, MidpointRounding.AwayFromZero);
#endif
}

public Point64(long x, long y, long z = 0)
{
public Point64(long x, long y
#if USINGZ
, long z = 0
#endif
) {
X = x;
Y = y;
#if USINGZ
Z = z;
#endif
}

public Point64(double x, double y, double z = 0.0)
{
public Point64(double x, double y
#if USINGZ
, double z = 0.0
#endif
) {
X = (long) Math.Round(x, MidpointRounding.AwayFromZero);
Y = (long) Math.Round(y, MidpointRounding.AwayFromZero);
#if USINGZ
Z = (long) Math.Round(z, MidpointRounding.AwayFromZero);
#endif
}

public Point64(PointD pt)
{
X = (long) Math.Round(pt.x, MidpointRounding.AwayFromZero);
Y = (long) Math.Round(pt.y, MidpointRounding.AwayFromZero);
#if USINGZ
Z = pt.z;
#endif
}

public Point64(PointD pt, double scale)
{
X = (long) Math.Round(pt.x * scale, MidpointRounding.AwayFromZero);
Y = (long) Math.Round(pt.y * scale, MidpointRounding.AwayFromZero);
#if USINGZ
Z = pt.z;
#endif
}

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

public static Point64 operator +(Point64 lhs, Point64 rhs)
{
return new Point64(lhs.X + rhs.X, lhs.Y + rhs.Y, lhs.Z + rhs.Z);
return new Point64(lhs.X + rhs.X, lhs.Y + rhs.Y
#if USINGZ
, lhs.Z + rhs.Z
#endif
);
}

public static Point64 operator -(Point64 lhs, Point64 rhs)
{
return new Point64(lhs.X - rhs.X, lhs.Y - rhs.Y, lhs.Z - rhs.Z);
return new Point64(lhs.X - rhs.X, lhs.Y - rhs.Y
#if USINGZ
, lhs.Z - rhs.Z
#endif
);
}

public readonly override string ToString()
{
return $"{X},{Y},{Z} "; // nb: trailing space
}

// nb: trailing space
#if USINGZ
return $"{X},{Y},{Z} ";
#else
public Point64(Point64 pt)
{
X = pt.X;
Y = pt.Y;
}

public Point64(long x, long y)
{
X = x;
Y = y;
}

public Point64(double x, double y)
{
X = (long) Math.Round(x, MidpointRounding.AwayFromZero);
Y = (long) Math.Round(y, MidpointRounding.AwayFromZero);
}

public Point64(PointD pt)
{
X = (long) Math.Round(pt.x, MidpointRounding.AwayFromZero);
Y = (long) Math.Round(pt.y, MidpointRounding.AwayFromZero);
}

public Point64(Point64 pt, double scale)
{
X = (long) Math.Round(pt.X * scale, MidpointRounding.AwayFromZero);
Y = (long) Math.Round(pt.Y * scale, MidpointRounding.AwayFromZero);
}

public Point64(PointD pt, double scale)
{
X = (long) Math.Round(pt.x * scale, MidpointRounding.AwayFromZero);
Y = (long) Math.Round(pt.y * scale, MidpointRounding.AwayFromZero);
}

public static bool operator ==(Point64 lhs, Point64 rhs)
{
return lhs.X == rhs.X && lhs.Y == rhs.Y;
}

public static bool operator !=(Point64 lhs, Point64 rhs)
{
return lhs.X != rhs.X || lhs.Y != rhs.Y;
}

public static Point64 operator +(Point64 lhs, Point64 rhs)
{
return new Point64(lhs.X + rhs.X, lhs.Y + rhs.Y);
}
return $"{X},{Y} ";
#endif

public static Point64 operator -(Point64 lhs, Point64 rhs)
{
return new Point64(lhs.X - rhs.X, lhs.Y - rhs.Y);
}
public readonly override string ToString()
{
return $"{X},{Y} "; // nb: trailing space
}

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

#if USINGZ
public long z;
#endif

public PointD(PointD pt)
{
x = pt.x;
y = pt.y;
#if USINGZ
z = pt.z;
#endif
}

public PointD(Point64 pt)
{
x = pt.X;
y = pt.Y;
#if USINGZ
z = pt.Z;
#endif
}

public PointD(Point64 pt, double scale)
{
x = pt.X * scale;
y = pt.Y * scale;
#if USINGZ
z = pt.Z;
#endif
}

public PointD(PointD pt, double scale)
{
x = pt.x * scale;
y = pt.y * scale;
#if USINGZ
z = pt.z;
#endif
}

public PointD(long x, long y, long z = 0)
{
public PointD(long x, long y
#if USINGZ
, long z = 0
#endif
) {
this.x = x;
this.y = y;
#if USINGZ
this.z = z;
#endif
}

public PointD(double x, double y, long z = 0)
{
public PointD(double x, double y
#if USINGZ
, long z = 0
#endif
) {
this.x = x;
this.y = y;
#if USINGZ
this.z = z;
#endif
}

public readonly string ToString(int precision = 2)
{
#if USINGZ
return string.Format($"{{0:F{precision}}},{{1:F{precision}}},{{2:D}}", x,y,z);
}

#else
public PointD(PointD pt)
{
x = pt.x;
y = pt.y;
}

public PointD(Point64 pt)
{
x = pt.X;
y = pt.Y;
}

public PointD(PointD pt, double scale)
{
x = pt.x * scale;
y = pt.y * scale;
}

public PointD(Point64 pt, double scale)
{
x = pt.X * scale;
y = pt.Y * scale;
}

public PointD(long x, long y)
{
this.x = x;
this.y = y;
}

public PointD(double x, double y)
{
this.x = x;
this.y = y;
}

public readonly string ToString(int precision = 2)
{
return string.Format($"{{0:F{precision}}},{{1:F{precision}}}", x,y);
#endif
}

#endif
public static bool operator ==(PointD lhs, PointD rhs)
{
return InternalClipper.IsAlmostZero(lhs.x - rhs.x) &&
Expand Down

0 comments on commit 7598be9

Please sign in to comment.