Skip to content

Commit

Permalink
fix: improve algorithm for size selection resulting
Browse files Browse the repository at this point in the history
  • Loading branch information
Igmat committed Apr 7, 2021
1 parent 1ea53bc commit eb61a04
Showing 1 changed file with 18 additions and 23 deletions.
41 changes: 18 additions & 23 deletions Plugin/Util/ContainerSizes.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,28 +13,23 @@ struct ContainerSize

public static readonly ContainerSize[] PossibleContainerSizes =
(new int[] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20 })
.SelectMany((int i) =>
(new int[] { 1, 2, 3, 4, 5, 6, 7, 8 })
.Select((int j) =>
new ContainerSize() { Height = i, Width = j }))
.Distinct(new ContainerSizeComparer())
.OrderBy((ContainerSize container) => container.Size)
.ToArray();

/// <summary>
/// this comparer used to filter out equal sizes with different Width/Height
/// </summary>
private class ContainerSizeComparer : IEqualityComparer<ContainerSize>
{
public bool Equals(ContainerSize x, ContainerSize y)
{
return x.Size == y.Size;
}

public int GetHashCode(ContainerSize obj)
{
return obj.Size;
}
}
.SelectMany((int i) =>
(new int[] { 1, 2, 3, 4, 5, 6, 7, 8 })
.Select((int j) =>
new ContainerSize() { Height = i, Width = j }))
// group by all available sizes
.GroupBy(container => container.Size)
// select closest to square shape in each group, because it's most usable one
.Select(containers => containers
.OrderBy(container => Math.Abs(container.Height - container.Width))
.First())
.OrderBy(container => container.Size)
// exclude thin and long containers, because they don't look good in UI
.Where(container => !(container.Width < 5 && container.Height - container.Width > 5))
// prefer width over heigh if possible
.Select(container => (container.Height > container.Width && container.Height <= 8)
? new ContainerSize() { Height = container.Width, Width = container.Height }
: container)
.ToArray();
}
}

0 comments on commit eb61a04

Please sign in to comment.