90 lines
3.4 KiB
C#

namespace View_by_Distance.Shared.Models.Stateless.Methods;
internal abstract class Location
{
internal static void Check(int bottom, int left, int right, int top, int zCount)
{
if (left < 0)
throw new Exception();
if (right < 0)
throw new Exception();
if (right < left)
throw new Exception();
if (top < 0)
throw new Exception();
if (bottom < 0)
throw new Exception();
if (bottom < top)
throw new Exception();
if (zCount < 0)
throw new Exception();
}
internal static void Check(int bottom, int height, int left, int right, int top, int width, int zCount)
{
if (bottom > height)
throw new Exception();
if (left > width)
throw new Exception();
if (right > width)
throw new Exception();
if (top > height)
throw new Exception();
if (zCount < 0)
throw new Exception();
}
internal static void Check(int bottom, int left, int? normalizedPixelPercentage, int right, int top, int zCount)
{
Check(bottom, left, right, top, zCount);
if (normalizedPixelPercentage.HasValue && normalizedPixelPercentage.Value < 0)
throw new Exception();
}
internal static void Check(int bottom, int height, int left, int? normalizedPixelPercentage, int right, int top, int width, int zCount)
{
Check(bottom, left, right, top, zCount);
Check(bottom, height, left, right, top, width, zCount);
if (normalizedPixelPercentage.HasValue && normalizedPixelPercentage.Value < 0)
throw new Exception();
}
internal static int GetNormalizedPixelPercentage(int bottom, int height, int left, int locationDigits, int locationFactor, int right, int top, int width, int zCount)
{
int result;
int checksum;
decimal center = 2m;
string xCenterPadded;
string yCenterPadded;
decimal factor = locationFactor;
// int.MaxPercentage = 21 4748 3647;
int length = (locationDigits - 1) / 2;
Check(bottom, left, right, top, zCount);
Check(bottom, height, left, right, top, width, zCount);
decimal xCenterValue = left + ((right - left) / center);
decimal yCenterValue = top + ((bottom - top) / center);
if (xCenterValue > yCenterValue)
checksum = 1;
else
checksum = 2;
decimal xCenterPercentageFactored = xCenterValue / width * factor;
decimal yCenterPercentageFactored = yCenterValue / height * factor;
int xCenterRounded = (int)Math.Round(xCenterPercentageFactored, 0);
int yCenterRounded = (int)Math.Round(yCenterPercentageFactored, 0);
if (xCenterRounded != factor)
xCenterPadded = ILocation.GetLeftPadded(length, xCenterRounded);
else
xCenterPadded = ILocation.GetLeftPadded(length, xCenterRounded - 1);
if (yCenterRounded != factor)
yCenterPadded = ILocation.GetLeftPadded(length, yCenterRounded);
else
yCenterPadded = ILocation.GetLeftPadded(length, yCenterRounded - 1);
long check = long.Parse(string.Concat(xCenterPadded, yCenterPadded, checksum));
if (check > int.MaxValue)
throw new Exception();
result = (int)check;
return result;
}
}