Windows Azure Cloud Storage ermöglicht es Ihnen bereits ab 0,10€ pro GB/Monat die Vorteile der Cloud zu nutzen.
Willkommen bei dotnet-snippets.de! Snippet hinzufügen Login Registrieren
Snippets in der Datenbank: 1549 | Anzahl registrierter User: 1833 | Besucher online: 9
Hauptmenü
Home
Top Ten
Zufälliger Snippet
FAQs
.NET Community
dotnet-forum.de
dotnet-kicks.de
Social

RSS Feeds
Rss Alle Snippets
Rss C#
Rss VB.NET
Rss C++
Rss ASP.NET
Partner
Member of Microsoft Community Leader/Insider Program (CLIP)

CellMatrix


Autor: v.wochnik
Sprache: C#
Bewertung:
4.56 (2 votes)
Anzahl der Aufrufe: 7275
  
Kick it on dotnet-kicks.de  

Beschreibung:

Diese Klasse ermöglicht eine zweidimensionale Matrix eines bestimmten Typs. Die Werte können dann mit dem XY-Koordinatensystem angesprochen werden.

Abgelegt unter: 2D, zweidimensional, Matrix, Koordinatensystem, Koordinate, Mathe, Cell, Typ, Wert, Value, Type.



C#
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
    }
}
Sie haben Fragen zu diesem Snippet oder brauchen Hilfe bei der .NET Entwicklung?
Freundliche und kompetente Entwickler helfen Ihnen gern weiter im Forum für .NET Entwicklung.



Kommentare:
(Zum Schreiben von Kommentaren bitte anmelden.)

Günther Foidl schrieb am:  01.10.2008 00:51:53

Viel Aufwand um die Arrays neu zu "definieren".
T[,] entspricht auch dem. Nur der Zugriff erfolgt über Indizes statt über Koordinaten und das Stürzen wird als Transponieren bezeichnet.


Diese Snippets könnten für Sie interessant sein:
[C++] Abstand von 2 Punkten 2D aus auch 3D berechnen
[C#] Pivot-Tabelle erstellen
[C++] Verwendung von eigenen Matizen in OpenGL
[C#] Summe 1..n berechnen
[C#] Fibonacci-Folge berechnen
[C#] n-te Fibonaccizahl rekursiv berechnen
[C#] Summe 1²...n² berechnen.
[C#] Summe 1³..n³ berechnen.
[C#] größten gemeinsamen Teiler berechnen.
[VB.NET] Multiplikation von übergroßen Zahlen
[C#] Dreiecksberechnung
[VB.NET] PI nach der Bailey-Borwein-Plouffe-Formel berechnen
[VB.NET] Quadratische Gleichung mit der PQ Formel lösen
[VB.NET] Basisrechenfunktionen für einen Kreis
[C++] Exponents
[C#] Quersummenberechnung
[C#] Geodaten in sexagesimal Format umrechnen
[VB.NET] Größten gemeinsamen Teiler berechnen
[VB.NET] Quadratwurzel ohne Sqrt() Funktion ziehen
[C#] Addiere alle ganzen Zahlen von x bis y
[C++] Caesar
[C#] "echte" Teiler Summe berechnen
[C#] Formelevaluierung aus RPN Form
[C#] Prüfung auf narzisstische Zahlen
[C#] Maschinengenauigkeit
[C#] Flächenberechnungen am Kreis,Quadrat,Parallelogramm,Trapez
[C#] Bruch-Klasse
[VB.NET] einfacher rekursiver Mathe Parser
[VB.NET] Permutation nachweisen
[VB.NET] Das Sieb des Eratosthenes
[VB.NET] Primfaktorzerlegung
[VB.NET] Größter gemeinsamer Teiler
[VB.NET] Modulare Exponentation
[VB.NET] Fibonacci-Folge iterativ erzeugen
[VB.NET] Das Sieb von Atkin
[VB.NET] Das Sieb von Atkin (2)
[VB.NET] Werte zweier Variablen tauschen
[VB.NET] Ganzzahlige Wurzel
[VB.NET] Binäre Exponentation
[VB.NET] Größter gemeinsamer Teiler (2)
[VB.NET] Binomialkoeffizient
[VB.NET] Kleinster natürlicher Teiler > 1
[VB.NET] Das Sieb von Atkin (2) - aktuell
[VB.NET] Dezimalzahl in Zahl der Basis b < 37 konvertieren
[VB.NET] Den Typ eines Laufwerks ermitteln
[ASP.net] übergeben von Werten beim Seitenwechsel
[VB.NET] Zahlen als Binär darstellen
[C#] Parameter Value aus Url per Extension-Method ermitteln
[C#] Add Key-Value return old Value
[C#] Registery Nach einem Value durchsuchen
[C#] Typ der Elemente in einem Array herausfinden
[C#] Prüfen, ob Teil des .NET Frameworks
[C#] Eine allgemeine Liste (IEnumerable) in ein Array umwandeln
[C#] Zugriff auf Variablen/Funktionen einer unbekannten Klasse

schlecht sehr gut
1 2 3 4 5 6 7 8 9 10
Nur angemeldete User können Snippets bewerten.