Understanding for-in-loop Calculs - c#

I have this little code here, pixels fall from the top, if they collide with a block they will Stay on the surface of the block. Then if there is an other Pixel falling on the pixel that is no more falling it will add up. Well that is what I was trying to do, but after the second pixel add up, there is no more pixels that would get on top of the others. I though that j-1 is the current int in the index (- 1) so if it's 10, then it would be 9, so I'm a little confused why (int)Position[j - 1].Y - 1 doesn't work properly.
public void ParticleUpdate()
{
for (int j = 0; j < 1000; j++)
{
Position[j].Y += Gravity;
for (int u = 0; u < GlobalClass.BlocksPositions.Count; u++)
{
if (new Rectangle((int)GlobalClass.BlocksPositions[u].X, (int)GlobalClass.BlocksPositions[u].Y, 8, 8).Intersects(new Rectangle((int)Position[j].X, (int)Position[j].Y, 1, 1)))
{
Position[j].Y = (int)GlobalClass.BlocksPositions[u].Y - 1;
//This code works perfectly.
}
}
if (j - 1 > 0)
{
if (new Rectangle((int)Position[j - 1].X, (int)Position[j - 1].Y, 1, 1).Intersects(new Rectangle((int)Position[j].X, (int)Position[j].Y, 1, 1)))
{
Position[j].Y = (int)Position[j - 1].Y - 1;
//Here is the problem.
}
}
}
}
Also I have some lag issues if I put a lot of blocks is there a way to optimize this?

Related

Generate vectors given degree of polynomial and list of coeficients

I'm using Unity, and I'm making a generator in which the user inputs a degree of a polynomial plus all of its coefficients. For example, I can have degree 3 and [1,2,3,4], which should be 1x^3 + 2x^2 + 3x^1 + 4x^0.
Here's what I have:
int[] coef = TitleToGame.coeficients;
for (int x = -10; x <= 10; x++)
{
float y = 0;
for (int i = 0; i < TitleToGame.degree - 1; i++)
{
if (i == 0)
{
y = coef[TitleToGame.degree] * Mathf.Pow(x, i);
}
else
{
y += coef[TitleToGame.degree - i] * Mathf.Pow(x, i);
}
Instantiate(block, new Vector3(x, y, 5), Quaternion.identity);
}
}
I'm trying to generate blocks from domain -10 to 10. However, the result looks a bit funky.
Degree 3 with [1,1,1,1] shows a parabola with an extra linear line:
Degree 2 shows a linear line (with an extra unwanted flat line), 1 doesn't show anything, and 4 also shows a parabola. What am I doing wrong?
You're calling Instantiate in the inner for loop, when I persume you want to call it in the outer loop (from -10 to 10).
Also, you probably don't need that if-else statement. Both lines do the same thing.
for (int i = 0; i < TitleToGame.degree - 1; i++)
{
y += coef[TitleToGame.degree - i] * Mathf.Pow(x, i);
}
Instantiate(block, new Vector3(x, y, 5), Quaternion.identity);

Efficient way to find neighboring cells in a 2d array

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).

Using an IF statement inside loop vs outside the loop

i have a program with a nested nested nested loop (four loops altogether). I have a Boolean variable which i want to affect the code in the deepest loop and in the first nested loop a small amount. My dilemma is, i don't really want to have the if else statement put inside the loops as i would have thought that would check the Boolean's state every iteration using extra time to check the statement, and i know that the Boolean's state would not change when the loop starts. This lead me to think it would be better to place the if else statement outside the loops and just have my loops code slightly changed, however, this also looks messy there is a lot of repeated code.
One thing i thought might work, but of which i have little experience using, is a delegate, i could simply put some of the code in a method and then create a delegate, depending on the state of betterColor i could then assign that delegate methods with the different code on them beforehand, but this also seems messy.
Below is what i am trying to avoid as i thhought it may slow down my algorithm:
for (short y = 0; y < effectImage.Height; y++)
{
int vCalc = (y <= radius) ? 0 : y - radius;
for (short x = 0; x < effectImage.Width; x++)
{
int red = 0, green = 0, blue = 0;
short kArea = 0;
for (int v = vCalc; v <= y + radius && v < effectImage.Height; v++)
{
int calc = calcs[(y - v) + radius];
for (int h = (x <= calc || calc < 0) ? 0 : x - calc; h <= x + calc && h < effectImage.Width; h++)
{
if (betterColor == true)
{
red += colorImage[h, v].R * colorImage[h, v].R;
green += colorImage[h, v].G * colorImage[h, v].G;
blue += colorImage[h, v].B * colorImage[h, v].G;
kArea++;
}
}
}
if (betterColor == true)
effectImage.SetPixel(x, y, Color.FromArgb(red / kArea, green / kArea, blue / kArea));
else
effectImage.SetPixel(x, y, Color.FromArgb(Convert.ToInt32(Math.Sqrt(red / kArea)), Convert.ToInt32(Math.Sqrt(green / kArea)), Convert.ToInt32(Math.Sqrt(blue / kArea))));
}
if (y % 4 == 0) // Updates the image on screen every 4 y pixels calculated.
{
image.Image = effectImage;
image.Update();
}
}
And here is what my code now looks like:
if (betterColor == true)
{
for (short y = 0; y < effectImage.Height; y++)
{
int vCalc = (y <= radius) ? 0 : y - radius;
for (short x = 0; x < effectImage.Width; x++)
{
int red = 0, green = 0, blue = 0;
short kArea = 0;
for (int v = vCalc; v <= y + radius && v < effectImage.Height; v++)
{
int calc = calcs[(y - v) + radius];
for (int h = (x <= calc || calc < 0) ? 0 : x - calc; h <= x + calc && h < effectImage.Width; h++)
{
red += colorImage[h, v].R * colorImage[h, v].R;
green += colorImage[h, v].G * colorImage[h, v].G;
blue += colorImage[h, v].B * colorImage[h, v].G;
kArea++;
}
}
effectImage.SetPixel(x, y, Color.FromArgb(Convert.ToInt32(Math.Sqrt(red / kArea)), Convert.ToInt32(Math.Sqrt(green / kArea)), Convert.ToInt32(Math.Sqrt(blue / kArea))));
}
if (y % 4 == 0) // Updates the image on screen every 4 y pixels calculated.
{
image.Image = effectImage;
image.Update();
}
}
}
else
{
for (short y = 0; y < effectImage.Height; y++)
{
int vCalc = (y <= radius) ? 0 : y - radius;
for (short x = 0; x < effectImage.Width; x++)
{
int red = 0, green = 0, blue = 0;
short kArea = 0;
for (int v = vCalc; v <= y + radius && v < effectImage.Height; v++)
{
int calc = calcs[(y - v) + radius];
for (int h = (x <= calc || calc < 0) ? 0 : x - calc; h <= x + calc && h < effectImage.Width; h++)
{
red += colorImage[h, v].R;
green += colorImage[h, v].G;
blue += colorImage[h, v].B;
kArea++;
}
}
effectImage.SetPixel(x, y, Color.FromArgb(red / kArea, green / kArea, blue / kArea));
}
if (y % 4 == 0) // Updates the image on screen every 4 y pixels calculated.
{
image.Image = effectImage;
image.Update();
}
}
}
In terms of what the code does, it is a box blur that uses a circular kernal.
Moving the if out of the loop, and effectively duplicating the whole looping code is not really worth it. So if you have code like this:
for (i …)
{
if (something)
DoX(i);
else
DoY(i);
}
Then you should not replace it by this:
if (something)
{
for (i …)
DoX(i);
}
else
{
for (i …)
DoY(i);
}
Doing so will only make the code a lot more difficult to read and to maintain. One first need to figure out that this is actually the same code that’s being executed for each case (except that one tiny difference), and it’s super difficult to maintain once you need to change anything about the loop since you need to make sure that you edit both cases properly.
While in theory, performing a single check vs. performing that check N times is obviously faster, in practice, this rarely matters. If-branches that rely on a constant boolean are super fast, so if you calculate the condition outside of the loop (in your case betterColor is set outside the loop), then the performance difference will not be noticeable at all. In addition, branch prediction will usually make sure that there is no difference at all in these cases.
So no, don’t rewrite that code like that. Keep it the way that is more understandable.
In general, you should avoid these kind of micro optimizations anyway. It is very likely that you algorithm has much slower parts that are much more relevant to the overall performance than small constructs like that. So focusing on those small things which are already very fast will not help you make the total execution faster. You should only optimize things that are an actual performance bottleneck in your code, where a profiler showed that there is a performance issue or that optimizing that code will actively improve your performance. And stay away from optimizations that make code less readable unless you really need it (in most cases you won’t).
The way i see it, the code is not equivalent (in your first example if betterColor is false, nothing happens in the innermost loop).
But isn't this micro-optimizing?
You could probably do something with creating a function with a Func<> as argument for the innermost loop. And then pass the correct func depending on the betterColor value.
I.E. Blur(betterColor?FuncA:FuncB);
Although I don't think it will be faster then a boolean check... But that's my feeling.

Having trouble with connected component labeling algorithm

So I've been trying to write a connected-component labeling algorithm but it's not giving me the desired result. Right now I have an image with 3 roses (that are not overlapping) and I want to label each rose with its own grey value. Before I apply the labeling algorithm I use a threshold to get rid of the background and only keep the roses. The roses get the gray value 1 (which is completely white) and the background gets gray value 0 (which is black). Here's an image of what this looks like:
After doing this I apply the labeling algorithm. It should give the roses three different gray values according to the label they've been given. But instead the algorithm creates this weird sort of gradient pattern over the first two roses whereas the last one seems to be a single gray value. Here's an image:
The algorithm might look complicated but it's actually really simple. I recurse on the columns first and then the rows and for each non-background pixel I check if any of its neighbors are already labeled (meaning their objectArray value is not zero). If so I add them to a list of neighbors. I then proceed to check if this list is not empty, if so I uniquely label the current pixel by incrementing the objects value and assigning its value to the current pixel's label value and I also set the current pixel's parent value to this unique label. If it's not empty, I determine the smallest label value in the neighbor list, set all the neighbors' parent values to this label value and I set the current pixel's label and parent values to this label value. I repeat this for every pixel until the whole image has been labeled.
Once this is done I recurse on the pixel values again, this time to set each pixel's label value to its parent value. I then proceed to assign the pixel a new gray value depending on its label value.
I don't understand why the algorithm isn't labeling the roses properly. Can anybody help me out? Here's the algorithm:
public void label()
{
int objects = 1;
int[,] objectArray = new int[colors.GetLength(1), colors.GetLength(0)];
DisjointSets disjointSet = new DisjointSets();
int[,] parents = new int[colors.GetLength(1), colors.GetLength(0)];
List<List<int>> eqSet = new List<List<int>>();
for (int i = 0; i < colors.GetLength(1); i++) for (int j = 0; j < colors.GetLength(0); j++)
{
if (this[i, j].Gray == 1)
{
List<Label> neighbors = new List<Label>();
if (i > 0)
{
if (this[i - 1, j].Gray == 1)
{
if (objectArray[i - 1, j] != 0)
{
neighbors.Add(new Label(i - 1, j, 0));
}
}
if (j > 0)
{
if (this[i - 1, j - 1].Gray == 1)
{
if (objectArray[i - 1, j - 1] != 0)
{
neighbors.Add(new Label(i - 1, j - 1, 0));
}
}
}
if (j < colors.GetLength(0))
{
if (this[i - 1, j + 1].Gray == 1)
{
if (objectArray[i - 1, j] != 0)
{
neighbors.Add(new Label(i - 1, j, 0));
}
}
}
}
if (j > 0)
{
if (this[i, j - 1].Gray == 1)
{
if (objectArray[i, j - 1] != 0)
{
neighbors.Add(new Label(i, j - 1, 0));
}
}
if (i < colors.GetLength(1))
{
if (this[i + 1, j - 1].Gray == 1)
{
if (objectArray[i + 1, j - 1] != 0)
{
neighbors.Add(new Label(i + 1, j - 1, 0));
}
}
}
}
if (i < colors.GetLength(1))
{
if (this[i + 1, j].Gray == 1)
{
if (objectArray[i + 1, j] != 0)
{
neighbors.Add(new Label(i + 1, j, 0));
}
}
if (this[i + 1, j + 1].Gray == 1)
{
if (objectArray[i + 1, j + 1] != 0)
{
neighbors.Add(new Label(i + 1, j + 1, 0));
}
}
}
if (j < colors.GetLength(0))
{
if (this[i, j + 1].Gray == 1)
{
if (objectArray[i, j + 1] != 0)
{
neighbors.Add(new Label(i, j + 1, 0));
}
}
}
if (neighbors.Count == 0)
{
objects++;
objectArray[i, j] = objects;
parents[i, j] = objects;
}
if (neighbors.Count > 0)
{
int smallestLabel = 10000;
foreach (Label x in neighbors)
if (objectArray[x.X, x.Y] < smallestLabel)
smallestLabel = objectArray[x.X, x.Y];
foreach (Label x in neighbors)
parents[x.X, x.Y] = smallestLabel;
objectArray[i, j] = smallestLabel;
parents[i, j] = smallestLabel;
}
}
}
for (int i = 0; i < colors.GetLength(1); i++) for (int j = 0; j < colors.GetLength(0); j++)
{
if (this[i, j].Gray == 1)
{
if (objectArray[i, j] != 0)
{
objectArray[i, j] = parents[i, j];
ColorWrap c = this[i, j];
c.X = (float)objectArray[i, j] / objects;
c.Y = (float)objectArray[i, j] / objects;
c.Z = (float)objectArray[i, j] / objects;
this[i, j] = c;
}
}
}
}
You've got an index error when checking for the third neighbour:
if (this[i - 1, j + 1].Gray == 1)
{
if (objectArray[i - 1, j] != 0)
{
neighbors.Add(new Label(i - 1, j, 0));
}
}
That should be 'j + 1' in all three spots.
That doesn't solve your problem, though. Your algorithm has problems when it meets diagonal edges that are black to thze nortwest and white to the southeast.
You scan the image column-wise from left to right and each column row-wise from top to bottom. You check for eight potential neighbours, but in reality, you can only add pixels to your neighbour list that you have already passed, namely the three pixels in the column to your left and the pixel above your current position. The other four adjacent pixels will have a parent (or object index) of 0.
Now consider an edge like this:
#######...
######....
#####.....
####......
###O......
###.......
##x.......
#xx.......
('#' is black, '.' is unassigned white, 'x' is white that has been assigned a parent and 'O' marks your current position.)
You will only find black or unassigned adjacent tiles. Your neighbour list will be empty, which means that your algorithm creates a new object, although it should logically belong to the yet undiscovered object to the southeast.
(You try to back-track that by assigning all neighbour pixels the value of the new group, but that only fixes one pixel. It can also create empty groups, i.e. objects that have no corresponding pixel.)
I think that your approach is too complicated, anyway. It also doesn't account for the nooks above and to the left of the main body of components. There's no need to create extra data structures, you can do the labelling in the picture itself if you use a greyscale image. After converting the image to pure black and white, pass all pixels once. If a pixel is white, flood-fill the picture from there with a positive gray value that corresponds to the next object and increment the number of objects. Then pass all pixels again and adjust the grey value according to the object id and the number of objects.
Caveat: When I say you can do everything in the picture itself, you limit your labelling to 254 objects.
Aside: You might want to clean up your code a bit. You don't use eqSet and disjointSet, so please remove them from your code. The two arrays objectArray and parents are used alogside each other for the same thing; merge them into one. You should also refactor finding the eight neighbour pixels into a function (with one call for each potential neighbour) to make things easier to read and to avoid indexing errors as above.

Nested for loop not iterating properly

Guys i have a really weird problem. I am trying to implement a nested for loop which splits up a rectangle into smaller blocks and then checks to see which of these smaller blocks have points from an array i have defined in them(big rectangle is 320*240 btw):
int[,] edgetrunc = new int[edgeIndex, 2]; //edgeIndex is the number of points in the external array
Array.Copy(edgePoint, edgetrunc, edgeIndex*2);//truncates the nulls from the tail of my array
int delx = 16;//width of block
int dely = 12;//height of block
int blockIndex = new int();
bool[] block = new bool[(depthFrame.Width/delx)*(depthFrame.Height/dely)];
for (int ymin = 0;ymin < depthFrame.Height;ymin += dely)
{
for (int xmin = 0; xmin < depthFrame.Width; xmin += delx)
{
blockIndex = (xmin / delx) + (ymin / dely);
for (int i = 0; i < edgeIndex; i++)
{
if (edgetrunc[i, 0] >= xmin && edgetrunc[i, 0] < xmin++ && edgetrunc[i, 1] >= ymin && edgetrunc[i, 1] < ymin++)
{
block[blockIndex] = true;
break;
}
}
}
}
heres the problem though, i put a breakpoint on the second for loop(the xmin loop) and started to iterate it, and the value of xmin jumped from 0 to 320 on the first iteration, and after that stayed there and ymin alone changed on each iteration. Am i going cray? what did i do wrong?
PS i even tested this and i got the same problem:
for (int ymin = 0;ymin < depthFrame.Height;ymin += dely)
{
for (int xmin = 0; xmin < depthFrame.Width; xmin += delx)
{
}
}
EDIT:
figured it out, it was strange, apparantly it had to do with the way i was trying to find block index. To fix it, i initialized blockIndex to 0 outside the for loops and put blockIndex++ after the 3rd for loop, thanks for the help anyways =)
int blockIndex = 0;
bool[] block = new bool[(depthFrame.Width/delx)*(depthFrame.Height/dely)];
for (int ymin = 0;ymin < depthFrame.Height;ymin += dely)
{
for (int xmin = 0; xmin < depthFrame.Width; xmin += delx)
{
for (int i = 0; i < edgeIndex; i++)
{
if ((edgetrunc[i, 0] >= xmin) && (edgetrunc[i, 0] < (xmin + delx)) && (edgetrunc[i, 1] >= ymin) && (edgetrunc[i, 1] < (ymin + dely)))
{
block[blockIndex] = true;
break;
}
}
blockIndex++;
}
}
Instead of using xmin++ and ymin++ I think you probably meant to use xmin+1 and ymin+1. ++ will change the value in the variable.
On your first inner loop iteration, the following gets run:
xmin++
as part of the if statement in the innermost loop. This gets executed 320 times, hence your value. One the second iteration,
edgetrunc[i, 0] >= xmin
will be false, and so the xmin++ line will not be executed. In general, you should avoid using mutation statements like xmin++ inside if statements. Refactor that out, and see if it fixes your problem
The reason it's jumping up like that is because you have xmin++ in the if statement inside your inner loop.
This could quite easily drive the value of xmin up quickly, depending on the value of edgeIndex.
x++ increments x then uses its old value. I think what you need is simply x+1 which uses the value plus one, without changing the value.
Ditto for the ymin++ in there as well.
In other words, it probably should be:
if (edgetrunc[i, 0] >= xmin && edgetrunc[i, 0] < xmin + 1 &&
edgetrunc[i, 1] >= ymin && edgetrunc[i, 1] < ymin + 1)

Categories