115 lines
4.1 KiB
C#
115 lines
4.1 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();
|
|
Check(bottom, left, right, top, zCount);
|
|
}
|
|
|
|
internal static string GetLeftPadded(int locationDigits, string value)
|
|
{
|
|
string result;
|
|
if (value.Length == locationDigits)
|
|
result = value;
|
|
else if (value.Length > locationDigits)
|
|
result = value[..locationDigits];
|
|
else
|
|
result = value.PadLeft(locationDigits, '0');
|
|
return result;
|
|
}
|
|
|
|
internal static int GetNormalizedPixelPercentage(int bottom, int height, int left, int locationDigits, int locationFactor, int right, int top, int width, int zCount)
|
|
{
|
|
int result;
|
|
Check(bottom, height, left, right, top, width, zCount);
|
|
int checksum;
|
|
decimal center = 2m;
|
|
string xCenterPadded;
|
|
string yCenterPadded;
|
|
decimal factor = locationFactor;
|
|
// int.MaxPercentage = 21 4748 3647;
|
|
int length = (locationDigits - 1) / 2;
|
|
decimal xCenterValue = (left + right) / center;
|
|
decimal yCenterValue = (top + bottom) / center;
|
|
if (xCenterValue < left || xCenterValue > right)
|
|
throw new Exception();
|
|
if (yCenterValue < top || yCenterValue > bottom)
|
|
throw new Exception();
|
|
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;
|
|
}
|
|
|
|
internal static (int?, int?) GetXY(int locationDigits, int locationFactor, int width, int height, string normalizedPixelPercentage)
|
|
{
|
|
int? x;
|
|
int? y;
|
|
int center = 2;
|
|
decimal factor = locationFactor;
|
|
int each = (locationDigits - 1) / center;
|
|
string segmentA = normalizedPixelPercentage[..each];
|
|
string segmentB = normalizedPixelPercentage[each..^1];
|
|
if (!int.TryParse(segmentA, out int xNormalized) || !int.TryParse(segmentB, out int yNormalized))
|
|
{
|
|
x = null;
|
|
y = null;
|
|
}
|
|
else
|
|
{
|
|
decimal xValue = xNormalized / factor * width;
|
|
decimal yValue = yNormalized / factor * height;
|
|
x = (int)Math.Round(xValue, 0);
|
|
y = (int)Math.Round(yValue, 0);
|
|
}
|
|
return new(x, y);
|
|
}
|
|
|
|
} |