Re-write
This commit is contained in:
		| @ -1,10 +1,9 @@ | ||||
| using System.Text.Json; | ||||
| using System.Text.Json.Serialization; | ||||
| using View_by_Distance.Shared.Models.Methods; | ||||
|  | ||||
| namespace View_by_Distance.Shared.Models; | ||||
|  | ||||
| public class Location : Properties.ILocation, ILocation, IEquatable<Location> | ||||
| public class Location : Properties.ILocation, IEquatable<Location> | ||||
| { | ||||
|  | ||||
|     protected int _Bottom; | ||||
| @ -13,8 +12,8 @@ public class Location : Properties.ILocation, ILocation, IEquatable<Location> | ||||
|     protected readonly int? _NormalizedPixelPercentage; | ||||
|     protected int _Right; | ||||
|     protected int _Top; | ||||
|     public int Bottom => _Bottom; | ||||
|     public double Confidence => _Confidence; | ||||
|     public int Bottom => _Bottom; | ||||
|     public int Left => _Left; | ||||
|     public int? NormalizedPixelPercentage => _NormalizedPixelPercentage; | ||||
|     public int Right => _Right; | ||||
| @ -23,27 +22,22 @@ public class Location : Properties.ILocation, ILocation, IEquatable<Location> | ||||
|     [JsonConstructor] | ||||
|     public Location(int bottom, double confidence, int left, int? normalizedPixelPercentage, int right, int top) | ||||
|     { | ||||
|         if (normalizedPixelPercentage < 0) | ||||
|             normalizedPixelPercentage = 3; | ||||
|         _Confidence = confidence; | ||||
|         _Bottom = bottom; | ||||
|         _Left = left; | ||||
|         _NormalizedPixelPercentage = normalizedPixelPercentage; | ||||
|         _Right = right; | ||||
|         _Top = top; | ||||
|         Check(bottom, left, normalizedPixelPercentage, right, top, zCount: 1); | ||||
|     } | ||||
|  | ||||
|     public Location(double confidence, int bottom, int left, int right, int top, int width, int height) : | ||||
|         this(bottom, confidence, left, GetNormalizedPixelPercentage(bottom, height, left, right, top, width), right, top) | ||||
|     { } | ||||
|     public Location(double confidence, int height, Location location, int width, int zCount) : | ||||
|         this(location.Bottom, confidence, location.Left, GetNormalizedPixelPercentage(location.Bottom, height, location.Left, location.Right, location.Top, width, zCount), location.Right, location.Top) => | ||||
|         Check(_Bottom, _Left, _NormalizedPixelPercentage, _Right, _Top, zCount); | ||||
|  | ||||
|     public Location(double confidence, Location location, int width, int height) : | ||||
|         this(location.Bottom, confidence, location.Left, GetNormalizedPixelPercentage(location.Bottom, height, location.Left, location.Right, location.Top, width), location.Right, location.Top) | ||||
|     { } | ||||
|  | ||||
|     public Location(int left, int top, int right, int bottom, int width, int height) : | ||||
|         this(bottom, -1.0d, left, GetNormalizedPixelPercentage(bottom, height, left, right, top, width), right, top) | ||||
|     { } | ||||
|     public Location(int bottom, double confidence, int height, int left, int right, int top, int width, int zCount) : | ||||
|         this(bottom, confidence, left, GetNormalizedPixelPercentage(bottom, height, left, right, top, width, zCount), right, top) => | ||||
|         Check(_Bottom, height, _Left, _NormalizedPixelPercentage, _Right, _Top, width, zCount); | ||||
|  | ||||
|     public override bool Equals(object? obj) => Equals(obj as Location); | ||||
|  | ||||
| @ -53,6 +47,53 @@ public class Location : Properties.ILocation, ILocation, IEquatable<Location> | ||||
|         return result; | ||||
|     } | ||||
|  | ||||
|     private 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(); | ||||
|     } | ||||
|  | ||||
|     private 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(); | ||||
|     } | ||||
|  | ||||
|     private 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(); | ||||
|     } | ||||
|  | ||||
|     private 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(); | ||||
|     } | ||||
|  | ||||
|     public override int GetHashCode() | ||||
|     { | ||||
|         int hashCode = -773114317; | ||||
| @ -63,16 +104,21 @@ public class Location : Properties.ILocation, ILocation, IEquatable<Location> | ||||
|         return hashCode; | ||||
|     } | ||||
|  | ||||
|     public static int GetNormalizedPixelPercentage(int bottom, int height, int left, int right, int top, int width) | ||||
|     public static int GetNormalizedPixelPercentage(int bottom, int height, int left, int right, int top, int width, int zCount) | ||||
|     { | ||||
|         int result; | ||||
|         double value; | ||||
|         int decimals = 6; | ||||
|         int factor = 1000000; | ||||
|         double total = width * height; | ||||
|         Check(bottom, left, right, top, zCount); | ||||
|         double xCenter = left + ((right - left) / 2); | ||||
|         double yCenter = top + ((bottom - top) / 2); | ||||
|         value = ((yCenter * width) + xCenter) / (width * height); | ||||
|         double at = ((yCenter - 1) * width) + xCenter; | ||||
|         value = at / total; | ||||
|         if (value < 0) | ||||
|             value = 3; | ||||
|         result = (int)(Math.Round((decimal)value, 4) * Stateless.Methods.ILocation.Factor); | ||||
|         result = (int)(Math.Round(value, decimals) * factor); | ||||
|         return result; | ||||
|     } | ||||
|  | ||||
|  | ||||
		Reference in New Issue
	
	Block a user