Skip to content

Commit 63da7bc

Browse files
refactor and improve algo
1 parent 530d51d commit 63da7bc

File tree

2 files changed

+31
-23
lines changed

2 files changed

+31
-23
lines changed

DesktopClock.Tests/PixelShifterTests.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,8 @@ public void ShiftX_ShouldNotExceedMaxTotalShift(int shiftAmount, int maxTotalShi
1313
{
1414
var shifter = new PixelShifter
1515
{
16-
ShiftAmount = shiftAmount,
17-
MaxTotalShift = maxTotalShift,
16+
PixelsPerShift = shiftAmount,
17+
MaxPixelOffset = maxTotalShift,
1818
};
1919

2020
double totalShiftX = 0;
@@ -37,8 +37,8 @@ public void ShiftY_ShouldNotExceedMaxTotalShift(int shiftAmount, int maxTotalShi
3737
{
3838
var shifter = new PixelShifter
3939
{
40-
ShiftAmount = shiftAmount,
41-
MaxTotalShift = maxTotalShift,
40+
PixelsPerShift = shiftAmount,
41+
MaxPixelOffset = maxTotalShift,
4242
};
4343

4444
double totalShiftY = 0;

DesktopClock/Utilities/PixelShifter.cs

Lines changed: 27 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -11,38 +11,46 @@ public class PixelShifter
1111
/// <summary>
1212
/// The number of pixels that will be shifted each time.
1313
/// </summary>
14-
public int ShiftAmount { get; set; } = 2;
14+
public int PixelsPerShift { get; set; } = 1;
1515

1616
/// <summary>
1717
/// The maximum amount of drift that can occur in each direction.
1818
/// </summary>
19-
public int MaxTotalShift { get; set; } = 4;
19+
public int MaxPixelOffset { get; set; } = 4;
2020

2121
public double ShiftX()
2222
{
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;
3327
}
3428

3529
public double ShiftY()
3630
{
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);
3938

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)
4144
{
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;
4454
}
45-
46-
return 0;
4755
}
4856
}

0 commit comments

Comments
 (0)