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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
|
using System;
using System.Collections.Generic;
using System.Text;
using System.Drawing;
namespace ViperBytes.Collections
{
/// <summary>
/// CellMatrix is a two-dimensional matrix where you can get a type per xy-coordinate system.
/// </summary>
public class CellMatrix<T>
{
#region private fields
/// <summary>
/// Contains the values of type T
/// </summary>
private List<List<T>> data = new List<List<T>>();
/// <summary>
/// Contains width and height of matrix
/// </summary>
private Size size = new Size(0, 0);
/// <summary>
/// Specifyes, wheather values larger then size - 1 will start from zero
/// example: x = width -> x = 0, x = -1 -> x = width - 1
/// </summary>
private bool infinite = false;
#endregion
#region properties
/// <summary>
/// Returns a value
/// </summary>
/// <param name="x">X-coordinate</param>
/// <param name="y">Y-coordinate</param>
/// <returns>Specifyed value</returns>
public T this[int x, int y]
{
get
{
if (Infinite)
{
while (x < 0)
x += this.size.Width;
while (x >= this.size.Width)
x -= this.size.Width;
while (y < 0)
y += this.size.Height;
while (y >= this.size.Height)
y -= this.size.Height;
}
return this.data[y][x];
}
set
{
if (Infinite)
{
while (x < 0)
x += this.size.Width;
while (x >= this.size.Width)
x -= this.size.Width;
while (y < 0)
y += this.size.Height;
while (y >= this.size.Height)
y -= this.size.Height;
}
this.data[y][x] = value;
}
}
/// <summary>
/// Specifyes size of the matrix
/// </summary>
public Size Size
{
get
{
return this.size;
}
set
{
if (this.size != value)
{
this.size = value;
Refresh();
}
}
}
/// <summary>
/// Specifyes, wheather indexes larger then width-1 will start at zero
/// </summary>
public bool Infinite
{
get
{
return this.infinite;
}
set
{
if (this.infinite != value)
{
this.infinite = value;
}
}
}
#endregion
#region Constructor
/// <summary>
/// The constructor
/// </summary>
public CellMatrix()
{ }
/// <summary>
/// The constructor
/// </summary>
/// <param name="width">Width of matrix</param>
/// <param name="height">Height of matrix</param>
public CellMatrix(int width, int height)
{
this.size = new Size(width, height);
Refresh();
}
#endregion
#region public members
/// <summary>
/// This will flip the matrix
/// </summary>
/// <param name="hor">Specifyes, wheather a horizontal flip will happen</param>
/// <param name="ver">Specifyes, wheather a vertical flip will happen</param>
public void Flip(bool hor, bool ver)
{
if ((!hor) && (!ver))
return;
List<List<T>> tmp = new List<List<T>>();
for (int y = 0; y < this.size.Height; y++)
{
int y2 = (ver) ? 0 : tmp.Count;
tmp.Insert(y2, new List<T>());
for (int x = 0; x < this.size.Width; x++)
{
int x2 = (hor) ? 0 : tmp[y2].Count;
tmp[y2].Insert(x2, this.data[y][x]);
}
}
this.data = tmp;
}
#endregion
#region private members
/// <summary>
/// That will adjust the two dimensional list to the size values
/// </summary>
private void Refresh()
{
if (this.data.Count < this.size.Height)
{
int diff = this.size.Height - this.data.Count;
for (int i = 0; i < diff; i++)
this.data.Add(new List<T>());
}
else if (this.data.Count > this.size.Height)
{
int diff = this.data.Count - this.size.Height;
for (int i = 0; i < diff; i++)
this.data.RemoveAt(this.data.Count - 1);
}
for (int y = 0; y < this.size.Height; y++)
{
if (this.data[y].Count < this.size.Width)
{
int diff = this.size.Width - this.data[y].Count;
for (int i = 0; i < diff; i++)
this.data[y].Add((T)new object());
}
else if (this.data[y].Count > this.size.Width)
{
int diff = this.data[y].Count - this.size.Width;
for (int i = 0; i < diff; i++)
this.data[y].RemoveAt(this.data[y].Count - 1);
}
}
}
#endregion
}
}
|