feat : rework with the logic again, use skiasharp instead
This commit is contained in:
parent
dd9f0d9cbe
commit
c390415b6e
3 changed files with 290 additions and 46 deletions
60
Services/Utilities/CoordinateHelper.cs
Normal file
60
Services/Utilities/CoordinateHelper.cs
Normal file
|
|
@ -0,0 +1,60 @@
|
|||
using System.Text.RegularExpressions;
|
||||
|
||||
namespace StitcherApi.Services.Utilities;
|
||||
|
||||
public static class CoordinateHelper
|
||||
{
|
||||
private static readonly Regex CoordRegex = new(@"([A-Z]+)(\d+)", RegexOptions.Compiled);
|
||||
|
||||
public static (int startRow, int endRow, int startCol, int endCol) ParseCanvasRect(
|
||||
string rectStr
|
||||
)
|
||||
{
|
||||
var parts = rectStr.Split(':');
|
||||
if (parts.Length != 2)
|
||||
{
|
||||
throw new ArgumentException("Invalid canvas_rect format. Expected format 'A1:H12'.");
|
||||
}
|
||||
|
||||
var (r1, c1) = ParseSingleCoordinate(parts[0]);
|
||||
var (r2, c2) = ParseSingleCoordinate(parts[1]);
|
||||
|
||||
return (Math.Min(r1, r2), Math.Max(r1, r2), Math.Min(c1, c2), Math.Max(c1, c2));
|
||||
}
|
||||
|
||||
public static string IndexToRow(int index)
|
||||
{
|
||||
index++;
|
||||
var result = "";
|
||||
while (index > 0)
|
||||
{
|
||||
int remainder = (index - 1) % 26;
|
||||
result = (char)('A' + remainder) + result;
|
||||
index = (index - 1) / 26;
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
private static (int row, int col) ParseSingleCoordinate(string coord)
|
||||
{
|
||||
var match = CoordRegex.Match(coord);
|
||||
if (!match.Success)
|
||||
{
|
||||
throw new ArgumentException($"Invalid coordinate format: '{coord}'.");
|
||||
}
|
||||
|
||||
string rowStr = match.Groups[1].Value;
|
||||
int col = int.Parse(match.Groups[2].Value) - 1;
|
||||
return (RowToIndex(rowStr), col);
|
||||
}
|
||||
|
||||
private static int RowToIndex(string rowStr)
|
||||
{
|
||||
int index = 0;
|
||||
foreach (char c in rowStr)
|
||||
{
|
||||
index = index * 26 + (c - 'A' + 1);
|
||||
}
|
||||
return index - 1;
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue