1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131
| namespace Matrices
{
class Program
{
static void Main(string[] args)
{
int[,] smallSize = new int[5, 6]
{
{1,0,0,0,0,0},
{0,0,0,1,1,0},
{0,1,0,1,0,0},
{0,1,0,0,0,0},
{0,0,0,0,0,0}
};
int[,] mediumSize = new int[10, 12]
{
{1,1,0,0,0,0,0,0,0,0,0,0},
{1,1,0,0,0,0,0,0,0,0,0,0},
{0,0,0,0,0,0,1,1,1,1,0,0},
{0,0,0,0,0,0,1,1,1,1,0,0},
{0,0,1,1,0,0,1,1,0,0,0,0},
{0,0,1,1,0,0,1,1,0,0,0,0},
{0,0,1,1,0,0,0,0,0,0,0,0},
{0,0,1,1,0,0,0,0,0,0,0,0},
{0,0,0,0,0,0,0,0,0,0,0,0},
{0,0,0,0,0,0,0,0,0,0,0,0}
};
int[,] bigSize = new int[15, 18]
{
{1,1,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
{1,1,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
{1,1,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
{0,0,0,0,0,0,0,0,0,1,1,1,1,1,1,0,0,0},
{0,0,0,0,0,0,0,0,0,1,1,1,1,1,1,0,0,0},
{0,0,0,0,0,0,0,0,0,1,1,1,1,1,1,0,0,0},
{0,0,0,1,1,1,0,0,0,1,1,1,0,0,0,0,0,0},
{0,0,0,1,1,1,0,0,0,1,1,1,0,0,0,0,0,0},
{0,0,0,1,1,1,0,0,0,1,1,1,0,0,0,0,0,0},
{0,0,0,1,1,1,0,0,0,0,0,0,0,0,0,0,0,0},
{0,0,0,1,1,1,0,0,0,0,0,0,0,0,0,0,0,0},
{0,0,0,1,1,1,0,0,0,0,0,0,0,0,0,0,0,0},
{0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
{0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
{0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0}
};
int[,] resized = Resize(smallSize, 2);
if (AreEquals(mediumSize, resized)) Console.WriteLine("Resize coef 2 succeeded");
else Console.WriteLine("Resize coef 2 failed");
resized = Resize(smallSize, 3);
if (AreEquals(bigSize,resized)) Console.WriteLine("Resize coef 3 succeeded");
else Console.WriteLine("Resize coef 3 failed");
Console.ReadKey();
}
static int[,] Resize(int[,] source, int coef)
{
if (source.Length == 0) return new int[0, 0];
else
{
int y = source.GetLength(0);
int x = source.Length / y;
int[,] result = new int[(y * coef), (x * coef)];
for (int row = 0; row < y; row++)
{
for (int col = 0; col < x; col++)
{
if (source[row, col] == 1)
{
int coordX = col * coef;
int coordY = row * coef;
FillContiguous(ref result, new Coords(coordX, coordY), coef);
}
}
}
return result;
}
}
static void FillContiguous(ref int[,] dest, Coords coords, int coef)
{
int x = coords.X + coef;
int y = coords.Y + coef;
for (int row = coords.Y; row < y; row++)
{
for (int col = coords.X; col < x; col++)
{
dest[row, col] = 1;
}
}
}
static Boolean AreEquals(int[,] first, int[,] second)
{
int y = first.GetLength(0);
int x = first.Length / y;
for (int row = 0; row < y; row++)
{
for (int col = 0; col < x; col++)
{
if (first[row, col] != second[row, col])
return false;
}
}
return true;
}
}
class Coords
{
public int X { get; private set; }
public int Y { get; private set; }
public Coords(int x, int y)
{
X = x;
Y = y;
}
}
} |
Partager