Skip to content

Commit

Permalink
Get size functions for 2D arrays, float/double mapping, fix bug
Browse files Browse the repository at this point in the history
  • Loading branch information
TheArcaneBrony committed Mar 17, 2024
1 parent e94a5b1 commit 409890c
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 3 deletions.
3 changes: 3 additions & 0 deletions ArcaneLibs/Extensions/CollectionExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,4 +26,7 @@ public static async IAsyncEnumerable<T> ToAsyncEnumerable<T>(this IEnumerable<Ta
yield return (task, await task);
}
}

public static int GetWidth<T>(this T[,] array) => array.GetLength(1);
public static int GetHeight<T>(this T[,] array) => array.GetLength(0);
}
19 changes: 16 additions & 3 deletions ArcaneLibs/MathUtil.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,25 @@
namespace ArcaneLibs;

[SuppressMessage("ReSharper", "MemberCanBePrivate.Global", Justification = "Public API")]
public class MathUtil {
public static class MathUtil {
public static int Map(int value, int originalStart, int originalEnd, int newStart, int newEnd) =>
(int)Map((long)value, originalStart, originalEnd, newStart, newEnd);

public static long Map(long value, int originalStart, int originalEnd, int newStart, int newEnd) {
public static long Map(long value, long originalStart, long originalEnd, long newStart, long newEnd) {
var scale = (double)(newEnd - newStart) / (originalEnd - originalStart);
return (int)(newStart + ((value - originalStart) * scale));
return (long)(newStart + ((value - originalStart) * scale));
}

public static float Map(float value, float originalStart, float originalEnd, float newStart, float newEnd) {
var scale = (newEnd - newStart) / (originalEnd - originalStart);
var res = newStart + ((value - originalStart) * scale);
// Console.WriteLine($"MapFloat({value}, {originalStart}, {originalEnd}, {newStart}, {newEnd}) = {res} @ {scale}");
return res;
}
public static double Map(double value, double originalStart, double originalEnd, double newStart, double newEnd) {
var scale = (newEnd - newStart) / (originalEnd - originalStart);
var res = newStart + ((value - originalStart) * scale);
// Console.WriteLine($"MapDouble({value}, {originalStart}, {originalEnd}, {newStart}, {newEnd}) = {res} @ {scale}");
return res;
}
}

0 comments on commit 409890c

Please sign in to comment.