-
Notifications
You must be signed in to change notification settings - Fork 33
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'master' of https://github.com/danielchalmers/DesktopClock
- Loading branch information
Showing
5 changed files
with
161 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
using System; | ||
using DesktopClock.Utilities; | ||
|
||
namespace DesktopClock.Tests; | ||
|
||
public class PixelShifterTests | ||
{ | ||
[Theory] | ||
[InlineData(5, 10)] // Evenly divisible. | ||
[InlineData(3, 10)] // Not evenly divisible. | ||
[InlineData(10, 5)] // Amount is larger than total. | ||
public void ShiftX_ShouldNotExceedMaxTotalShift(int shiftAmount, int maxTotalShift) | ||
{ | ||
var shifter = new PixelShifter | ||
{ | ||
PixelsPerShift = shiftAmount, | ||
MaxPixelOffset = maxTotalShift, | ||
}; | ||
|
||
double totalShiftX = 0; | ||
|
||
// Test 100 times because it's random. | ||
for (var i = 0; i < 100; i++) | ||
{ | ||
var shift = shifter.ShiftX(); | ||
totalShiftX += shift; | ||
|
||
Assert.InRange(Math.Abs(totalShiftX), 0, maxTotalShift); | ||
} | ||
} | ||
|
||
[Theory] | ||
[InlineData(5, 10)] // Evenly divisible. | ||
[InlineData(3, 10)] // Not evenly divisible. | ||
[InlineData(10, 5)] // Amount is larger than total. | ||
public void ShiftY_ShouldNotExceedMaxTotalShift(int shiftAmount, int maxTotalShift) | ||
{ | ||
var shifter = new PixelShifter | ||
{ | ||
PixelsPerShift = shiftAmount, | ||
MaxPixelOffset = maxTotalShift, | ||
}; | ||
|
||
double totalShiftY = 0; | ||
|
||
// Test 100 times because it's random. | ||
for (var i = 0; i < 100; i++) | ||
{ | ||
var shift = shifter.ShiftY(); | ||
totalShiftY += shift; | ||
|
||
Assert.InRange(Math.Abs(totalShiftY), 0, maxTotalShift); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
using System; | ||
|
||
namespace DesktopClock.Utilities; | ||
|
||
public class PixelShifter | ||
{ | ||
private readonly Random _random = new(); | ||
private double _totalShiftX; | ||
private double _totalShiftY; | ||
|
||
/// <summary> | ||
/// The number of pixels that will be shifted each time. | ||
/// </summary> | ||
public int PixelsPerShift { get; set; } = 1; | ||
|
||
/// <summary> | ||
/// The maximum amount of drift that can occur in each direction. | ||
/// </summary> | ||
public int MaxPixelOffset { get; set; } = 4; | ||
|
||
/// <summary> | ||
/// Returns an amount to shift horizontally by while staying within the specified bounds. | ||
/// </summary> | ||
public double ShiftX() | ||
{ | ||
double pixelsToMoveBy = GetRandomShift(); | ||
pixelsToMoveBy = GetFinalShiftAmount(_totalShiftX, pixelsToMoveBy, MaxPixelOffset); | ||
_totalShiftX += pixelsToMoveBy; | ||
return pixelsToMoveBy; | ||
} | ||
|
||
/// <summary> | ||
/// Returns an amount to shift vertically by while staying within the specified bounds. | ||
/// </summary> | ||
public double ShiftY() | ||
{ | ||
double pixelsToMoveBy = GetRandomShift(); | ||
pixelsToMoveBy = GetFinalShiftAmount(_totalShiftY, pixelsToMoveBy, MaxPixelOffset); | ||
_totalShiftY += pixelsToMoveBy; | ||
return pixelsToMoveBy; | ||
} | ||
|
||
/// <summary> | ||
/// Returns a random amount to shift by within the specified amount. | ||
/// </summary> | ||
private int GetRandomShift() => _random.Next(-PixelsPerShift, PixelsPerShift + 1); | ||
|
||
/// <summary> | ||
/// Returns a capped amount to shift by. | ||
/// </summary> | ||
/// <param name="current">The current total amount of shift that has occurred.</param> | ||
/// <param name="offset">The proposed amount to shift by this time.</param> | ||
/// <param name="max">The bounds to stay within in respect to the total shift.</param> | ||
private double GetFinalShiftAmount(double current, double offset, double max) | ||
{ | ||
var newTotal = current + offset; | ||
|
||
if (newTotal > max) | ||
{ | ||
return max - current; | ||
} | ||
else if (newTotal < -max) | ||
{ | ||
return -max - current; | ||
} | ||
else | ||
{ | ||
return offset; | ||
} | ||
} | ||
} |