Efficient way to find neighboring cells in a 2d array - c#

I have a 2d array of class Tiles. While creating the playfield I have to generate all directly neighboring cells (horizontally, vertically, diagonally). I start by generating the field filling up each cell with a new Tile, than (when that is done) I loop through the 2d array to calculate the neighbors using this piece of loop:
int dnDistance= 1; //Direct Neighbor Distance.
for (int iMapY = 0; iMapY < playfieldHeight; iMapY++)
{
for (int iMapX = 0; iMapX < playfieldWidth; iMapX++)
{
for (int yOffset = -dnDistance; yOffset <= dnDistance; yOffset++)
{
for (int xOffset = -dnDistance; xOffset <= dnDistance; xOffset++)
{
if ((iMapX + xOffset >= 0 && iMapX + xOffset < playfieldWidth) && (iMapY + yOffset >= 0 && iMapY + yOffset < playfieldHeight))
{
if (!(yOffset == 0 && xOffset == 0))
{
playfieldTiles[iMapX, iMapY].dnTiles.Add(playfieldTiles[iMapX + xOffset, iMapY + yOffset]);
}
}
}
}
}
}
Using this method, I have to loop through the entire 2d array a second time, creating a for loop, in a for loop, in a for loop, in a for loop which sometimes is quite unclear. There has to be a better way for this, right?
I found a post that looks to be similar but not quite the same, or I don't understand it properly:

When it just works then it's fine !
Here's a little optimization that makes it easier to debug:
var playfieldHeight = 5;
var playfieldWidth = 5;
var playfieldTiles = new byte[playfieldWidth + dnDistance * 2, playfieldHeight + dnDistance * 2];
var len1 = playfieldWidth * playfieldHeight;
var len2 = dnDistance * 2 + 1;
for (var i = 0; i < len1; i++)
{
var ix = i % playfieldWidth;
var iy = i / playfieldWidth;
for (var j = 0; j < len2 * len2; j++)
{
var jx = j % len2 - dnDistance;
var jy = j / len2 - dnDistance;
Console.WriteLine($"x1: {ix}, y1: {iy}, x2: {jx}, y2: {jy}");
}
}
You now have only 2 loops, the field and the neighbors.
You could further optimize it with a single for but I believe readability will decrease (inside the loop).

Related

Finding a minor matrix of a matrix with C# in Visual Studio

I'm developing an app will be used for calculating matrixes and I'm currently working on minor function and I need a minor matrix for that. So, I wrote the code shown below. It actually works without any error but there is an issue about 4x4 matrices. It changes the 4x1 and 4x2 values.
In this image, places of 9 and 10 are wrong. They are at each others place.
enter image description here
Note: There isn't any problem about textboxes, it's about the array (matrix).
Here is my code. I could easily solve it by rechanging them with a few lines of code but I'm trying to learn C# and Visual Studio, so I want to find the problem in my algorithm.
minorMatrix = new int[rowA.Value - 1, colA.Value - 1];
int k = 0, l = 0;
for (int i = 0; i < rowA.Value - 1; i++)
{
for (int j = 0; j < colA.Value - 1; j++)
{
if (i < row - 1)
{
k = i;
}
else
{
k = i + 1;
}
if (j < col - 1)
{
l = j;
}
else
{
l = j + 1;
}
minorMatrix[i, j] = matrixA[k, l];
}
}
Are you trying to remove the ith row and jth column? This should do it:
int[,] A = new int[3, 3]; // Original matrix
int[,] M = new int[2, 2]; // Matrix with ith row and jth column removed
int i = 2, j = 1; // 3rd row, 2nd column to remove
for (int Mi = 0; Mi < A.GetLength(0); Mi++)
for (int Mj = 0; Mj < A.GetLength(1); Mj++)
{
int Ai = Mi, Aj = Mj;
if (Mi >= i)
Ai = Mi + 1;
if (Mj >= j)
Aj = Mj + 1;
M[Mi, Mj] = A[Ai, Aj];
}

Optimizing my image search routine

My routine is as follows (the SpeedyBitmap class just locks each bitmap for faster pixel data access):
private static bool TestRange(int number, int lower, int upper) {
return number >= lower && number <= upper;
}
public static List<Point> Search(Bitmap toSearch, Bitmap toFind, double percentMatch = 0.85, byte rTol = 2, byte gTol = 2, byte bTol = 2) {
List<Point> points = new List<Point>();
Stopwatch stopwatch = new Stopwatch();
stopwatch.Start();
int findArea = toFind.Width * toFind.Height;
int allowedMismatches = findArea - (int)(findArea * percentMatch);
int mismatches = 0;
using (SpeedyBitmap speedySearch = new SpeedyBitmap(toSearch)) {
using (SpeedyBitmap speedyFind = new SpeedyBitmap(toFind)) {
for (int i = 0; i < speedySearch.Height - speedyFind.Height + 1; i++) {
for (int j = 0; j < speedySearch.Width - speedyFind.Width + 1; j++) {
for (int k = 0; k < speedyFind.Height; k++) {
for (int l = 0; l < speedyFind.Width; l++) {
Color searchColor = speedySearch[j + l, i + k];
Color findColor = speedyFind[l, k];
if (!TestRange(searchColor.R, findColor.R - rTol, findColor.R + rTol) ||
!TestRange(searchColor.G, findColor.G - gTol, findColor.G + gTol) ||
!TestRange(searchColor.B, findColor.B - bTol, findColor.B + bTol)) {
mismatches++;
if (mismatches > allowedMismatches) {
mismatches = 0;
goto notFound;
}
}
}
}
points.Add(new Point(j, i));
continue;
notFound:
;
}
}
}
}
Console.WriteLine(stopwatch.ElapsedMilliseconds);
return points;
}
Searching a moderately sized image (1000x1000) takes over 20 seconds. Removing the percent match takes it down to a few hundred milliseconds, so I think I've established where the major bottleneck is.
How can I make this run faster? Perhaps I could flatten the two-dimensional arrays down to one-dimensional arrays and then apply some sort of sequence check on the two to possibly match up the longest sequence of where the toFind bitmap data appears with the respective tolerances and match percent. If this would be a good solution, how would I begin to implement it?

Getting incorrect mathematical value after Matlab code converted to C#

I am using a method which was generated using the TableCurve program and written in Matlab. I am trying to convert the method into C# to use in my project but it is returning incorrect values. I think I have narrowed down the problem to a particular spot, the original code is
iv=1;
for j=1:1:tcnt
for m=j:-1:1
v(iv)=tx(m)*ty(j-m+1);
iv=iv+1;
end
end
where tx, ty, and v are lists of numbers and iv is a counter. and I have translated it into
n = 0;
for (int j = 1; j <= tcnt; j++)
{
for (int m = j; m >= 1; m--)
{
v[n] = tx[m - 1] * ty[j - m];
n++;
}
}
Does anyone see a problem with my indices? because I do not. If there is no problem with my indices I will edit this question to try and locate the actual problem area.
Edit
This is the entire working Matlab code (after variables are set up)
tx(1)=1.0;
ty(1)=1.0;
tx(2)=x;
ty(2)=y;
for j=2:1:tcnt-1
tx(j+1)=2*x*tx(j)-tx(j-1);
ty(j+1)=2*y*ty(j)-ty(j-1);
end
iv=1;
for j=1:1:tcnt
for m=j:-1:1
v(iv)=tx(m)*ty(j-m+1);
iv=iv+1;
end
end
z=0.0;
for j=1:1:order+1
z = z + p(j)*v(j);
end
And this is my C# code
tx[0] = 1.0;
ty[0] = 1.0;
tx[1] = x;
ty[1] = y;
for (int j = 2; j <= tcnt; j++)
{
tx[j] = 2 * x * tx[j - 1] - tx[j - 2];
ty[j] = 2 * y * ty[j - 1] - ty[j - 2];
}
n = 0;
for (int j = 1; j < tcnt; ++j)
{
for (int m = j; m >= 1; --m)
{
v[n] = tx[m] * ty[j - m + 1];
n++;
}
}
z = 0.0;
for (int j = 0; j <= order; j++)
{
z += constantList[j] * v[j];
}
What the bleep are you doing fitting data with a Chebyshev polynomial? Use a Fourier series, you already have the equation written out and tested to everyone's satisfaction. And the equation is bounded to the curve so you don't have to worry about discontinuities outside the range you have test data for. Hey, I have a good idea... I will do that.
The only difference I can see is that matlab indices start at 1, while c# indices start at 0, so the following should be equivalent:
iv=1;
for (int j = 1; j < tcnt; ++j) {
for (int m = j; m >= 1; --m) {
v[iv-1] = tx[m] * ty[j-m+1];
iv++;
}
}
Also, the first matlab loop runs from 2 to tcnt - 1, while the C# version runs from 2 to tcnt.

Damerau–Levenshtein distance algorithm, disable counting of delete

How can i disable counting of deletion, in this implementation of Damerau-Levenshtein distance algorithm, or if there is other algorithm already implemented please point me to it.
Example(disabled deletion counting):
string1: how are you?
string2: how oyu?
distance: 1 (for transposition, 4 deletes doesn't count)
And here is the algorithm:
public static int DamerauLevenshteinDistance(string string1, string string2, int threshold)
{
// Return trivial case - where they are equal
if (string1.Equals(string2))
return 0;
// Return trivial case - where one is empty
if (String.IsNullOrEmpty(string1) || String.IsNullOrEmpty(string2))
return (string1 ?? "").Length + (string2 ?? "").Length;
// Ensure string2 (inner cycle) is longer_transpositionRow
if (string1.Length > string2.Length)
{
var tmp = string1;
string1 = string2;
string2 = tmp;
}
// Return trivial case - where string1 is contained within string2
if (string2.Contains(string1))
return string2.Length - string1.Length;
var length1 = string1.Length;
var length2 = string2.Length;
var d = new int[length1 + 1, length2 + 1];
for (var i = 0; i <= d.GetUpperBound(0); i++)
d[i, 0] = i;
for (var i = 0; i <= d.GetUpperBound(1); i++)
d[0, i] = i;
for (var i = 1; i <= d.GetUpperBound(0); i++)
{
var im1 = i - 1;
var im2 = i - 2;
var minDistance = threshold;
for (var j = 1; j <= d.GetUpperBound(1); j++)
{
var jm1 = j - 1;
var jm2 = j - 2;
var cost = string1[im1] == string2[jm1] ? 0 : 1;
var del = d[im1, j] + 1;
var ins = d[i, jm1] + 1;
var sub = d[im1, jm1] + cost;
//Math.Min is slower than native code
//d[i, j] = Math.Min(del, Math.Min(ins, sub));
d[i, j] = del <= ins && del <= sub ? del : ins <= sub ? ins : sub;
if (i > 1 && j > 1 && string1[im1] == string2[jm2] && string1[im2] == string2[jm1])
d[i, j] = Math.Min(d[i, j], d[im2, jm2] + cost);
if (d[i, j] < minDistance)
minDistance = d[i, j];
}
if (minDistance > threshold)
return int.MaxValue;
}
return d[d.GetUpperBound(0), d.GetUpperBound(1)] > threshold
? int.MaxValue
: d[d.GetUpperBound(0), d.GetUpperBound(1)];
}
public static int DamerauLevenshteinDistance( string string1
, string string2
, int threshold)
{
// Return trivial case - where they are equal
if (string1.Equals(string2))
return 0;
// Return trivial case - where one is empty
// WRONG FOR YOUR NEEDS:
// if (String.IsNullOrEmpty(string1) || String.IsNullOrEmpty(string2))
// return (string1 ?? "").Length + (string2 ?? "").Length;
//DO IT THIS WAY:
if (String.IsNullOrEmpty(string1))
// First string is empty, so every character of
// String2 has been inserted:
return (string2 ?? "").Length;
if (String.IsNullOrEmpty(string2))
// Second string is empty, so every character of string1
// has been deleted, but you dont count deletions:
return 0;
// DO NOT SWAP THE STRINGS IF YOU WANT TO DEAL WITH INSERTIONS
// IN A DIFFERENT MANNER THEN WITH DELETIONS:
// THE FOLLOWING IS WRONG FOR YOUR NEEDS:
// // Ensure string2 (inner cycle) is longer_transpositionRow
// if (string1.Length > string2.Length)
// {
// var tmp = string1;
// string1 = string2;
// string2 = tmp;
// }
// Return trivial case - where string1 is contained within string2
if (string2.Contains(string1))
//all changes are insertions
return string2.Length - string1.Length;
// REVERSE CASE: STRING2 IS CONTAINED WITHIN STRING1
if (string1.Contains(string2))
//all changes are deletions which you don't count:
return 0;
var length1 = string1.Length;
var length2 = string2.Length;
// PAY ATTENTION TO THIS CHANGE!
// length1+1 rows is way too much! You need only 3 rows (0, 1 and 2)
// read my explanation below the code!
// TOO MUCH ROWS: var d = new int[length1 + 1, length2 + 1];
var d = new int[2, length2 + 1];
// THIS INITIALIZATION COUNTS DELETIONS. YOU DONT WANT IT
// or (var i = 0; i <= d.GetUpperBound(0); i++)
// d[i, 0] = i;
// But you must initiate the first element of each row with 0:
for (var i = 0; i <= 2; i++)
d[i, 0] = 0;
// This initialization counts insertions. You need it, but for
// better consistency of code I call the variable j (not i):
for (var j = 0; j <= d.GetUpperBound(1); j++)
d[0, j] = j;
// Now do the job:
// for (var i = 1; i <= d.GetUpperBound(0); i++)
for (var i = 1; i <= length1; i++)
{
//Here in this for-loop: add "%3" to evey term
// that is used as first index of d!
var im1 = i - 1;
var im2 = i - 2;
var minDistance = threshold;
for (var j = 1; j <= d.GetUpperBound(1); j++)
{
var jm1 = j - 1;
var jm2 = j - 2;
var cost = string1[im1] == string2[jm1] ? 0 : 1;
// DON'T COUNT DELETIONS! var del = d[im1, j] + 1;
var ins = d[i % 3, jm1] + 1;
var sub = d[im1 % 3, jm1] + cost;
// Math.Min is slower than native code
// d[i, j] = Math.Min(del, Math.Min(ins, sub));
// DEL DOES NOT EXIST
// d[i, j] = del <= ins && del <= sub ? del : ins <= sub ? ins : sub;
d[i % 3, j] = ins <= sub ? ins : sub;
if (i > 1 && j > 1 && string1[im1] == string2[jm2] && string1[im2] == string2[jm1])
d[i % 3, j] = Math.Min(d[i % 3, j], d[im2 % 3, jm2] + cost);
if (d[i % 3, j] < minDistance)
minDistance = d[i % 3, j];
}
if (minDistance > threshold)
return int.MaxValue;
}
return d[length1 % 3, d.GetUpperBound(1)] > threshold
? int.MaxValue
: d[length1 % 3, d.GetUpperBound(1)];
}
here comes my explanation why you need only 3 rows:
Look at this line:
var d = new int[length1 + 1, length2 + 1];
If one string has the length n and the other has the length m, then your code needs a space of (n+1)*(m+1) integers. Each Integer needs 4 Byte. This is waste of memory if your strings are long. If both strings are 35.000 byte long, you will need more than 4 GB of memory!
In this code you calculate and write a new value for d[i,j]. And to do this, you read values from its upper neighbor (d[i,jm1]), from its left neighbor (d[im1,j]), from its upper-left neighbor (d[im1,jm1]) and finally from its double-upper-double-left neighbour (d[im2,jm2]). So you just need values from your actual row and 2 rows before.
You never need values from any other row. So why do you want to store them? Three rows are enough, and my changes make shure, that you can work with this 3 rows without reading any wrong value at any time.
I would advise not rewriting this specific algorithm to handle specific cases of "free" edits. Many of them radically simplify the concept of the problem to the point where the metric will not convey any useful information.
For example, when substitution is free the distance between all strings is the difference between their lengths. Simply transmute the smaller string into the prefix of the larger string and add the needed letters. (You can guarantee that there is no smaller distance because one insertion is required for each character of edit distance.)
When transposition is free the question reduces to determining the sum of differences of letter counts. (Since the distance between all anagrams is 0, sorting the letters in each string and exchanging out or removing the non-common elements of the larger string is the best strategy. The mathematical argument is similar to that of the previous example.)
In the case when insertion and deletion are free the edit distance between any two strings is zero. If only insertion OR deletion is free this breaks the symmetry of the distance metric - with free deletions, the distance from a to aa is 1, while the distance from aa to a is 1. Depending on the application this could possibly be desirable; but I'm not sure if it's something you're interested in. You will need to greatly alter the presented algorithm because it makes the mentioned assumption of one string always being longer than the other.
Try to change var del = d[im1, j] + 1; to var del = d[im1, j];, I think that solves your problem.

creating a random (Gantt-ish) chart

I'm trying to write a test for one of my audio programs, and I'm having trouble wrapping my brain around this test setup. First, I've got a table of 60 rows by 10000 columns that needs to be filled. Each cell has a value of ON, OFF, or LEFT (meaning I have the same value as my nearest ON/OFF to my left). I want a random twenty to forty rows to be on at any given time. Each has to be on for a random 6 to 200 cells. The commands to set ON or OFF have to be ordered by row then column. I'm picturing a sparse dictionary coming out with a coordinate key and on/off value. What I don't understand is how store my ON/OFF cells such that I can easily determine if my current row is ON or OFF. Help? Thanks for your time.
After further thought on this, I realized I could do it in two passes. Here's what I ended up with. Feel free to comment on this approach:
var table = new byte[60, 10000];
for(int i = 0; i < 60; i++)
{
// we want at least half the row to be blank
int j = 0;
while(j < 10000)
{
var width = rand.Next(7, 200);
j += width * 2;
var vol = (byte)rand.Next(50, 125);
for(int k = j - width; k < Math.Min(10000, j); k++)
table[i, k] = vol;
}
}
var midiEvents = new List<BASS_MIDI_EVENT>();
midiEvents.Add(new BASS_MIDI_EVENT(BASSMIDIEvent.MIDI_EVENT_PROGRAM, 0, 0, 0, 0));
for(int j = 0; j < 10000; j++)
{
for(int i = 0; i < 60; i++)
{
var cur = (int)table[i, j];
var left = j > 0 ? table[i, j - 1] : 0;
if(cur > 0 && left == 0)
{
cur <<= 8;
cur |= i + 33;
midiEvents.Add(new BASS_MIDI_EVENT(BASSMIDIEvent.MIDI_EVENT_NOTE, cur, 0, j, 0));
}
else if(cur == 0 && left > 0)
{
midiEvents.Add(new BASS_MIDI_EVENT(BASSMIDIEvent.MIDI_EVENT_NOTE, i + 33, 0, j, 0));
}
}
}

Categories