using System.Collections.Generic;
public Image(int width, int height){
public int Height {get;set;}
public int Width {get;set;}
public override bool Equals(object obj){
if(obj != null && obj is Image fact){
return fact.Height == Height && fact.Width == Width;
public override int GetHashCode(){
return Height.GetHashCode() ^ Width.GetHashCode();
public Rectangle(int p1, int p2, int p3, int p4){
public override bool Equals(object obj){
if(obj != null && obj is Rectangle fact){
return fact.P1 == P1 && fact.P2 == P2 && fact.P3 == P3 && fact.P4 == P4;
public override int GetHashCode(){
return P1.GetHashCode() ^ P2.GetHashCode() ^ P3.GetHashCode() ^ P4.GetHashCode();
public override string ToString(){
return $"rect[{P1}, {P2}, {P3}, {P4}]";
public Point(int p1, int p2){
public override bool Equals(object obj){
if(obj != null && obj is Point fact){
return fact.P1 == P1 && fact.P2 == P2;
public override int GetHashCode(){
return P1.GetHashCode() ^ P2.GetHashCode();
public override string ToString(){
return $"point[{P1}, {P2}]";
public BoxInfo(Image image) {
Bounds = new Rectangle(-1, -1, image.Width, image.Height);
Origin = new Point(-1, -1);
public int Offset {get;set;}
public int Dimension {get;set;}
public Rectangle Bounds {get;set;}
public Point Origin {get;set;}
public override bool Equals(object obj){
if(obj != null && obj is BoxInfo fact){
return fact.Offset == Offset && fact.Dimension == Dimension && fact.Bounds.Equals(Bounds) && fact.Origin.Equals(Origin);
public override int GetHashCode(){
return Offset.GetHashCode() ^ Dimension.GetHashCode() ^ Bounds.GetHashCode() ^ Origin.GetHashCode();
public override string ToString(){
return $"offset: {Offset}, Dim: {Dimension}, Bounds: {Bounds}, Origin: {Origin}";
public static class Helper {
public static BoxInfo MakeSquare(Image source){
var dimensionX = source.Width;
var dimensionY = source.Height;
var ratio = (double)source.Width / (double)source.Height;
return LetterBox(source);
return PillarBox(source);
return new BoxInfo(source);
public static BoxInfo MakeSquare2(Image source){
var ratio = (double)source.Width / (double)source.Height;
if (ratio >= 0.95f && ratio <= 1.05f) return new BoxInfo(source);;
private static BoxInfo LetterBox(Image source){
var yOffset = Convert.ToInt32((source.Width - source.Height) / 2f);
Dimension = source.Width,
Bounds = new Rectangle(0, 0, source.Width, source.Width),
Origin = new Point(0, yOffset)
private static BoxInfo PillarBox(Image source){
var xOffset = Convert.ToInt32((source.Height - source.Width) / 2f);
Dimension = source.Height,
Bounds = new Rectangle(0, 0, source.Height, source.Height),
Origin = new Point(xOffset, 0)
private static BoxInfo MagicBox(Image source){
var isLandscape = source.Width > source.Height;
var big = isLandscape ? source.Width : source.Height;
var small = isLandscape ? source.Height : source.Width;
var offset = Convert.ToInt32((big - small) / 2f);
var origin = isLandscape ? new Point(0, offset) : new Point(offset, 0);
Bounds = new Rectangle(0, 0, big, big),
public static void Main()
List<bool> results = new List<bool>();
for(int x = 5; x < 256; x++){
for(int y = 5; y < 256; y++){
var one = Helper.MakeSquare(i);
var two = Helper.MakeSquare2(i);
var result = one.Equals(two);
Console.WriteLine($"one: {one}");
Console.WriteLine($"two: {two}");
Console.WriteLine($"failed! x: {x}, y: {y}");
if(!results.All(r => r == true)){
Console.WriteLine("failure was an option!");
Console.WriteLine($"{n} tests passed");
Console.WriteLine("Done");