Improving loop efficiency/extensibility - c#

I have an array of pixels which make up a 2D grid and I want to move these pixels right and left over time... constantly however the loop I came up with is fairly inefficient and does not allow for much expansion in terms of changing the motion. Any ideas on how to improve what I already have would be much appreciated.
while (true)
{
for (float i = 0; i < x; i++)
{
foreach (Pixel p in pixels)
{
p.move(10, 0);
}
}
for (float i = 0; i < x * 2; i++)
{
foreach (Pixel p in pixels)
{
p.move(-10, 0);
}
}
for (float i = 0; i < x; i++)
{
foreach (Pixel p in pixels)
{
p.move(10, 0);
}
}
}
Edit: Sorry had an error in the code the middle loop needed to be -10 the first for loop moves all the pixels right, the second moves them back to the origin and then to the left, the third loop then moves them back to the origin.

You could employ an offset variable to be used in the code where pixels are actually used. Moving pixels around is then done by changing offset, which is one line of code.

Can't you just use a single loop and using int? Three loops seems to be redundant
Use one loop from 0 to x*2 and perform the movement choosing on the x value
Something like
for(int i = 0; i < x * 2; i++)
{
foreach(Pixel p in pixels)
{
if (i < x)
{
p.move(0.1, 0);
p.move(10, 0);
p.move(10, 0);
}
else
p.move(10,0)
}
}

Related

why is my marching cubes algorithm so slow?

i have somewhat implemented marching cubes in unity/c# (you dont need to know unity to help me though) and i cant stop feeling like i have made a big mistake in my code because it is so slow. i am already running it on a separate thread but it just takes ages to complete. please help me optimize my code.
private void _UpdateChunk()
{
lock (this)
{
// clear the tri, vert and uv lists
ClearMeshData();
// Loop through each "cube" in the terrain.
for (int x = 0; x < width; x++)
{
for (int y = 0; y < height; y++)
{
for (int z = 0; z < width; z++)
{
// Create an array of floats representing each corner of a cube and get the value from our terrainMap.
float[] cube = new float[8];
float[] strengths = new float[8];
for (int i = 0; i < 8; i++)
{
Vector3Int corner = new Vector3Int(x, y, z) + gamedata.CornerTable[i];
cube[i] = terrainMap[corner.x, corner.y, corner.z].BlockType;
strengths[i] = terrainMap[corner.x, corner.y, corner.z].Strength;
}
// Pass the value into the MarchCube function.
MarchCube(new Vector3(x, y, z), cube, strengths);
}
}
}
}
}
void MarchCube(Vector3 position, float[] cube, float[] strengths)
{
// Get the configuration index of this cube.
int configIndex = GetCubeConfiguration(cube);
// If the configuration of this cube is 0 or 255 (completely inside the terrain or completely outside of it) we don't need to do anything.
if (configIndex == 0 || configIndex == 255)
return;
// Loop through the triangles. There are never more than 5 triangles to a cube and only three vertices to a triangle.
int edgeIndex = 0;
Vector3 vert1 = new Vector3();
Vector3 vert2 = new Vector3();
float vert1sample = 0;
float vert2sample = 0;
float lerp = 0;
int indice = 0;
for (int i = 0; i < 5; i++)
{
for (int p = 0; p < 3; p++)
{
// Get the current indice. We increment triangleIndex through each loop.
indice = gamedata.TriangleTable[configIndex, edgeIndex];
// If the current edgeIndex is -1, there are no more indices and we can exit the function.
if (indice == -1)
return;
// Get the vertices for the start and end of this edge.
vert1 = position + gamedata.EdgeTable[indice, 0];
vert2 = position + gamedata.EdgeTable[indice, 1];
vert1sample = strengths[gamedata.EdgeIndexTable[indice, 0]];
vert2sample = strengths[gamedata.EdgeIndexTable[indice, 1]];
// Get the midpoint of this edge.
lerp = Mathf.Abs(vert1sample) / (Mathf.Abs(vert2sample) + Mathf.Abs(vert1sample));
Vector3 vertPosition = Vector3.Lerp(vert1, vert2, lerp);
// Add to our vertices and triangles list and incremement the edgeIndex.
vertices.Add(vertPosition);
triangles.Add(vertices.Count - 1);
if (getChunkVoxel(vert1 + chunkPosition) != 0)
{
uvs.Add(new Vector2(getChunkVoxel(vert1 + chunkPosition) - 1, 0));
}
else
{
uvs.Add(new Vector2(getChunkVoxel(vert2 + chunkPosition) - 1, getChunkVoxel(vert2 + chunkPosition) - 1));
}
edgeIndex++;
}
}
}
int GetCubeConfiguration(float[] cube)
{
// Starting with a configuration of zero, loop through each point in the cube and check if it is below the terrain surface.
int configurationIndex = 0;
for (int i = 0; i < 8; i++)
{
// If it is, use bit-magic to the set the corresponding bit to 1. So if only the 3rd point in the cube was below
// the surface, the bit would look like 00100000, which represents the integer value 32.
if (cube[i] < terrainSurface)
configurationIndex |= 1 << i;
}
return configurationIndex;
}
it appears that this is the part that slows my game down, help would be appreciated
i already made it faster by changing terrainpoint from a class to a struct but it is still very slow.
One main reason it is slow is that there is a lot of allocations in the loop putting a lot of pressure on the garbadge collector. There is currently 11 allocation per "cube" in the terrain in _UpdateChunk and up to 17 in MarchCube (possibly even more if the expressions like position + gamedata.EdgeTable[indice, 0] allocates a new vector). This is not reasonable. Many allocation are not needed. For example cube and strengths can be preallocated once for all the cubes in the beginning of _UpdateChunk. You do not need to allocate the vector in the expression to compute corner: you can just compute the components separately manually (or you can possibly preallocate the vector and reset its component when needed). The same thing applies for the new Vector3(x, y, z) can can be preallocated and set in the loop. Such an algorithm is computationally intensive so you should get away any overhead like virtual method calls and allocations/GC -- only low-level arrays accesses and mathematical operations should remains.
Note that some computations can be optimized. For example GetCubeConfiguration can be modified so to be branchless. Mathf.Abs(vert1sample) can be precomputed so not to compute it twice (though the compiler may already do that). I am also wondering if the expression like vertices.Add are efficient but this is dependent of the type of container which is not provided here.

how to get exact part of Bitmap object

i am using the getPixels of Bitmap in C# to detect patterns of colors. I have an image file, but I need only to investigate a part of the file (exactly a rectangle inside of the image, cropped 5% from all sides)
I was wondering where is the origin (0,0) of the file so I can use a simple function that goes through all the pixels (see code). is there a convention of where is the 0,0 ??? top left? top right? bottom left? bottom right?
the function i show you here is ok for me, no need for a more effective way to check the file, because the file is not big enough. so maximum 1 second if i go through all the file. I just need to understand how the axis x and y are located
thank you,
Josh.
ulong CountPixels(Bitmap bm, Color target_color)
{
// Loop through the pixels.
ulong matches = 0;
for (int y = 0; y < bm.Height; y++)
{
for (int x = 0; x < bm.Width; x++)
{
if (bm.GetPixel(x, y) == target_color)
{
matches++;
}
}
}
return matches;
}
You can just get a Rectangle as a parameter and look just at the points inside that rectangle, to make sure that all points in rectangle falls inside the bitmap, you need to do Math.Min(bmp.Height, region.Y + region.Height) and Math.Min(bmp.Width, region.X+Region.Width) instead of just region.Y + region.Height and region.X+Region.Width:
ulong CountPixels(Bitmap bm, Color target_color, Rectangle region)
{
// Loop through the pixels.
ulong matches = 0;
for (int y = region.Y; y < Math.Min(bmp.Height, region.Y + region.Height); y++)
{
for (int x = region.X; x < Math.Min(bmp.Width, region.X+Region.Width); x++)
{
if (bm.GetPixel(x, y) == target_color)
{
matches++;
}
}
}
return matches;
}

Per Pixel Collision within a list

The title says it all. I'm trying to make a collision with the help of the per pixel collision method that I have work for all my enemies. My method works fine with if im not using a list. Here's the code im using:
public bool IntersectPixels(
Matrix transformA, int widthA, int heightA, Color[] dataA,
Matrix transformB, int widthB, int heightB, Color[] dataB)
{
// Calculate a matrix which transforms from A's local space into
// world space and then into B's local space
Matrix transformAToB = transformA * Matrix.Invert(transformB);
// For each row of pixels in A
for (int yA = 0; yA < heightA; yA++)
{
// For each pixel in this row
for (int xA = 0; xA < widthA; xA++)
{
// Calculate this pixel's location in B
Vector2 positionInB =
Vector2.Transform(new Vector2(xA, yA), transformAToB);
// Round to the nearest pixel
int xB = (int)Math.Round(positionInB.X);
int yB = (int)Math.Round(positionInB.Y);
// If the pixel lies within the bounds of B
if (0 <= xB && xB < widthB &&
0 <= yB && yB < heightB)
{
// Get the colors of the overlapping pixels
Color colorA = dataA[xA + yA * widthA];
Color colorB = dataB[xB + yB * widthB];
// If both pixels are not completely transparent,
if (colorA.A != 0 && colorB.A != 0)
{
// then an intersection has been found
return true;
}
}
}
}
// No intersection found
return false;
}
To check the collision I use this bit of code:
public bool Update(Matrix PersonTransform2,Color[] data2)
{
personTransform1 = Matrix.CreateTranslation(new Vector3(new Vector2(position.X, position.Y), 0.0f));
if (game.IntersectPixels(PersonTransform2, texture.Width, texture.Height, data2,
PersonTransform1, texture.Width, texture.Height, data1))
{
return true;
}
return false;
}
My question here is how I can transform this bit of code to be able to work with a list.
You could add an overload and use ToArray() from Linq:
public bool Update(Matrix personTransform2, List<Color> data2)
{
Update(Matrix personTransform2, data2.ToArray())
}
but because of performance (the above creates a copy) I would probably replace the Color[] by List<Color> in both signatures.
On the other end, for these kind of calculations you probably shouldn't be using the List<Color> to start with. If you care about performance work with array's directly.
Mistakes were made... I forgot to put a foreach loop that went through all my list... The code was:
for(int i = 0; i < lista.Count; i++)
{
if (lista[i].Update(blockTransform, data2))
{
touched = true;
}
}

Procedural Map Generation - determine adjacent tiles

I was originally using a 2d Array of "Tile"s to store a procedural generated map with its various contents.
Each Tile contains a List adjacent which allows every single tile to know which vertices are closest to it, touching it on 8 different sides (straight adjacent and diagonally adjacent).
The general idea was taken from Amit's polygonal map generation, but I attempted to simplify it by using a grid setup instead of voronois, however I've run into more trouble than I originally thought would be possible. My current predicament is figuring out adjacency when I've scrapped 2d Arrays.
This is how I was doing it before changing to a list:
private void ConstructAdjacencyList() {
// Create Adjacency List
for (int x = 0; x < mapWidth; x++) {
for (int y = 0; y < mapHeight; y++) {
// Bool to find position of point
bool omitLeft = false; bool omitRight = false;
bool omitTop = false; bool omitBottom = false;
// Enable bools based on position, reset on each loop
if (x == 0)
omitLeft = true;
else if (x == mapWidth - 1)
omitRight = true;
if (y == 0)
omitTop = true;
else if (y == mapHeight - 1)
omitBottom = true;
// Add entries to list based on bool settings
if (!omitLeft) {
// Left center
islandMap[x,y].adjacent.Add(islandMap[x-1,y]);
if (!omitTop)
islandMap[x,y].adjacent.Add(islandMap[x-1,y-1]);
if (!omitBottom)
islandMap[x,y].adjacent.Add(islandMap[x-1,y+1]);
}
if (!omitTop) // Top Center
islandMap[x,y].adjacent.Add(islandMap[x,y-1]);
if (!omitBottom) // Bottom Center
islandMap[x,y].adjacent.Add(islandMap[x,y+1]);
if (!omitRight) {
// Right Center
islandMap[x,y].adjacent.Add(islandMap[x+1,y]);
if (!omitTop)
islandMap[x,y].adjacent.Add(islandMap[x+1,y-1]);
if (!omitBottom)
islandMap[x,y].adjacent.Add(islandMap[x+1,y+1]);
}
}
} // End Adjacency
Debug.Log ("Adjacencies Built");
}
The x, y values now are held in islandMap.point (A Vector 2d storing the x and y values generated as follows:)
public MapController() {
width = height = (int)Mathf.Sqrt (tileCount);
// Lists for points
var points = new List<Vector2>();
// Build a random set of points.
for (float x = 0; x < width; x++) {
for (float y = 0; y < height; y++) {
points.Add(new Vector2(x,y));
}
}
map = new Map (points, width, height, lakeTreshold);
}
And the Map itself has the following currently:
public class Map {
Func<Vector2, bool> inside; // Contains function to randomly seed area
bool needsMoreRandomness;
public List<Tile> islandMap; // Previously was Tile[,] islandMap
public int mapWidth { get; private set; } // Calculated as Sqrt(totalPoints)
public int mapHeight { get; private set; }
Along with other methods such as the ConstructAdjacencyList() method I'm currently stuck on.
So how can I go on about constructing an adjacency list of surrounding points without relying on array positioning? Could I temporarily reference the entire list from an array, place references to each tile in the entire list in this 2d array, setup adjacencies and then remove the array without losing the information? I believe it would only use references, so it should be fine... Each tile contains an index to store the order with which it was built like so:
foreach (var point in points) {
var p = new Tile { index = islandMap.Count, point = point };
p.border = point.x == 0 || point.x == mapWidth || point.y == 0 || point.y == mapHeight;
islandMap.Add (p);
tileLookup[point] = p;
}
Sorry if this is too long... I just realized it's quite massive -.-
Assuming you haven't done anything to mess with the order of the points, you can treat a 1D list/array as 2D list/array. The example I linked is in C, but the idea language agnostic.
That being said, assuming that you're only going to do this once during the initialization & given the relatively low number of points, you could just as easily brute force it with a function that loops through the point list & picks out the neighbors as needed. My C# is a bit rusty, but I'm talking about something like this:
private List<Vector2> getAdjacenctPointList(List<Vector2> pointsList, Vector2 point){
var adjacencyList = new List<Vector2>();
foreach (var pt in pointList){
var offset = Math.abs(pt.x - point.x) + Math.abs(pt.y - point.y);
if(offset > 0 && offset <= 1.0){
adjacencyList.add(pt);
}
}
return adjacencyList;
}
The final answer I went with which was the least amount of work involved and allowed me to recycle my original 2d Array code posted above was to simply construct a temporary 2d array and store all the references to it in it - create all adjacencies as necessary and then dispose (simply by losing scope) of the 2d Array.
Here's the actual method with the 2d array system in place. First couple statements followed by the nested for loop was all it took:
private void ConstructAdjacencyList() {
Tile[,] tempArray = new Tile[mapWidth, mapHeight];
int count = 0;
// Populate the temp 2D array with list references
for (int x = 0; x < mapWidth; x++) {
for (int y = 0; y < mapHeight; y++) {
tempArray[x,y] = islandMap[count];
count++;
}
}
// Create Adjacency List using our TempArray
for (int x = 0; x < mapWidth; x++) {
for (int y = 0; y < mapHeight; y++) {
// Bool to find position of point
bool omitLeft = false; bool omitRight = false;
bool omitTop = false; bool omitBottom = false;
// Enable bools based on position, reset on each loop
if (x == 0)
omitLeft = true;
else if (x == mapWidth - 1) // Optimize with if else to split checks in half.
omitRight = true;
if (y == 0)
omitTop = true;
else if (y == mapHeight - 1)
omitBottom = true;
// Add entries to list based on bool settings
if (!omitLeft) {
// Left center
tempArray[x,y].adjacent.Add(tempArray[x-1,y]);
if (!omitTop)
tempArray[x,y].adjacent.Add(tempArray[x-1,y-1]);
if (!omitBottom)
tempArray[x,y].adjacent.Add(tempArray[x-1,y+1]);
}
if (!omitTop) // Top Center
tempArray[x,y].adjacent.Add(tempArray[x,y-1]);
if (!omitBottom) // Bottom Center
tempArray[x,y].adjacent.Add(tempArray[x,y+1]);
if (!omitRight) {
// Right Center
tempArray[x,y].adjacent.Add(tempArray[x+1,y]);
if (!omitTop)
tempArray[x,y].adjacent.Add(tempArray[x+1,y-1]);
if (!omitBottom)
tempArray[x,y].adjacent.Add(tempArray[x+1,y+1]);
}
}
} // End Adjacency
Debug.Log ("Adjacencies Built");
}
To ensure this worked out as I wanted it to, I tested it visually by setting one random cube and tested various points all around the area to ensure there were no mistakes and there weren't.
The end result is as expected, depicted below:
Thanks for the help :)

How to delete certain pixels in a rectangle

I am designing a space invaders game and I need to make the barriers break from where they were hit like the original game, As of now my barrier just shrinks:
for (int r = 0; r < BarrierXPos.Count; r++) {
if (CollisionCheck("Barrier", BarrierXPos[r], BarrierYPos[r], null) == true)
{
BarrierFactor[r] += 1;
}
}
for (int x = 0; x < BarrierXPos.Count; x++)
{
e.Graphics.FillRectangle(Brushes.White, (int)BarrierXPos[x], (int)BarrierYPos[x], 100 - (10 * BarrierFactor[x]), 50);
}
So as of now the size is determined by the amount of times the block has been hit. However I want it to be more like the original space invaders and would like the block to be dynamically broken from where it was hit. What I am wondering is how I can determine the pixels in which the bullet hits the barrier bitmap and subsequently remove them.

Categories

Resources