@@ -11,38 +11,46 @@ public class PixelShifter
11
11
/// <summary>
12
12
/// The number of pixels that will be shifted each time.
13
13
/// </summary>
14
- public int ShiftAmount { get ; set ; } = 2 ;
14
+ public int PixelsPerShift { get ; set ; } = 1 ;
15
15
16
16
/// <summary>
17
17
/// The maximum amount of drift that can occur in each direction.
18
18
/// </summary>
19
- public int MaxTotalShift { get ; set ; } = 4 ;
19
+ public int MaxPixelOffset { get ; set ; } = 4 ;
20
20
21
21
public double ShiftX ( )
22
22
{
23
- var shift = _random . Next ( - ShiftAmount , ShiftAmount + 1 ) ;
24
- var newTotalShiftX = _totalShiftX + shift ;
25
-
26
- if ( Math . Abs ( newTotalShiftX ) <= MaxTotalShift )
27
- {
28
- _totalShiftX = newTotalShiftX ;
29
- return shift ;
30
- }
31
-
32
- return 0 ;
23
+ double pixelsToMoveBy = GetRandomShift ( ) ;
24
+ pixelsToMoveBy = GetFinalShiftAmount ( _totalShiftX , pixelsToMoveBy , MaxPixelOffset ) ;
25
+ _totalShiftX += pixelsToMoveBy ;
26
+ return pixelsToMoveBy ;
33
27
}
34
28
35
29
public double ShiftY ( )
36
30
{
37
- var shift = _random . Next ( - ShiftAmount , ShiftAmount + 1 ) ;
38
- var newTotalShiftY = _totalShiftY + shift ;
31
+ double pixelsToMoveBy = GetRandomShift ( ) ;
32
+ pixelsToMoveBy = GetFinalShiftAmount ( _totalShiftY , pixelsToMoveBy , MaxPixelOffset ) ;
33
+ _totalShiftY += pixelsToMoveBy ;
34
+ return pixelsToMoveBy ;
35
+ }
36
+
37
+ private int GetRandomShift ( ) => _random . Next ( - PixelsPerShift , PixelsPerShift + 1 ) ;
39
38
40
- if ( Math . Abs ( newTotalShiftY ) <= MaxTotalShift )
39
+ private double GetFinalShiftAmount ( double current , double offset , double max )
40
+ {
41
+ var newTotal = current + offset ;
42
+
43
+ if ( newTotal > max )
41
44
{
42
- _totalShiftY = newTotalShiftY ;
43
- return shift ;
45
+ return max - current ;
46
+ }
47
+ else if ( newTotal < - max )
48
+ {
49
+ return - max - current ;
50
+ }
51
+ else
52
+ {
53
+ return offset ;
44
54
}
45
-
46
- return 0 ;
47
55
}
48
56
}
0 commit comments