Skip to content

Commit 4b65c4b

Browse files
gregmeessrms80
authored andcommitted
Add Snapping methods for snapping high or low
1 parent d358f3e commit 4b65c4b

File tree

1 file changed

+26
-0
lines changed

1 file changed

+26
-0
lines changed

core/Snapping.cs

+26
Original file line numberDiff line numberDiff line change
@@ -29,5 +29,31 @@ public static double SnapToNearbyIncrement(double fValue, double fIncrement, dou
2929
return fValue;
3030
}
3131

32+
private static double SnapToIncrementSigned(double fValue, double fIncrement, bool low)
33+
{
34+
if (!MathUtil.IsFinite(fValue))
35+
return 0;
36+
double sign = Math.Sign(fValue);
37+
fValue = Math.Abs(fValue);
38+
int nInc = (int)(fValue / fIncrement);
39+
40+
if (low && sign < 0)
41+
++nInc;
42+
else if (!low && sign > 0)
43+
++nInc;
44+
45+
return sign * (double)nInc * fIncrement;
46+
47+
}
48+
49+
public static double SnapToIncrementLow(double fValue, double fIncrement)
50+
{
51+
return SnapToIncrementSigned(fValue, fIncrement, true);
52+
}
53+
54+
public static double SnapToIncrementHigh(double fValue, double fIncrement)
55+
{
56+
return SnapToIncrementSigned(fValue, fIncrement, false);
57+
}
3258
}
3359
}

0 commit comments

Comments
 (0)