I have this code:
int tX = 1;
for (int y = 0; y < ROWS; y++)
{
for (int x = 0; x < tX; x++)
{
Tile t = new Tile()
{
Texture = tile,
Position = new Microsoft.Xna.Framework.Point(x, y),
Troops = rnd.Next(1, 4),
OwnedByPlayerIndex = 0
};
t.Tap += tile_Tap;
if (t.Position.Y < ROWS)
tiles.Add(t);
}
tX += 2;
tX = (int)MathHelper.Clamp(tX, 0, COLS);
}
And what im trying to do is create a map within a rect, limiting the map by number of rows and cols.
But it does not work as it does not follow up and finishes the last corner, leaving it uncomplete
You appear to have some redudant logic in there, and your inner loop is not iterating over all columns. If you want to fill the entire rectangle then you don't need tX at all. Example:
for (int y = 0; y < ROWS; y++)
{
for (int x = 0; x < COLS; x++)
{
Tile t = new Tile()
{
Texture = tile,
Position = new Microsoft.Xna.Framework.Point(x, y),
Troops = rnd.Next(1, 4),
OwnedByPlayerIndex = 0
};
t.Tap += tile_Tap;
tiles.Add(t);
}
}
Additionally, from your screenshot it looks like if the entire bottom row were filled then the lower staggered hexes would overlap the bottom of the red rectangle. If you don't want to add those lower hexes then you'll need to add them conditionally:
...
if (y > 0 || 0 == (x & 1))
tiles.Add(t);
Related
I want to create a world generation using two noise maps (altitude noise and moisture noise). I create maps of these noises and set them in the inspector to values between 0 and 1.
I want to get result like this:
If Elevation < 1000
{
If Moisture < 50: Desert
Else Forest
}
And it seems that I did it as it should, but for some reason the generation does not work correctly:
Here is my code:
for (int x=0; x < tilemap.Width; x++)
{
for (int y = 0; y < tilemap.Height; y++)
{
// Get height at this position
var height = noiseMap[y * tilemap.Width + x];
var moisureHeight = moisureMap[y * tilemap.Width + x];
// Loop over our configured tile types
for (int i = 0; i < TileTypes.Length; i++)
{
var TileType = TileTypes[i];
for (var j = 0; j < TileType.MoisureValues.Length; j++)
{
// If the height is smaller or equal then use this tiletype
if (height <= TileType.Height)
{
if (moisureHeight <=TileType.MoisureValues[j].MoisureHeight)
{
tilemap.SetTile(x, y, (int)TileType.MoisureValues[j].GroundTile);
break;
}
}
}
}
}
}
i want to change the texture of my terrain with certain texture. i got confuse to set the splatmapdata, anyone can help me out??
private void ChangeTexture(Vector3 WorldPos)
{
print ("changeTexture");
int mapX = (int)(((WorldPos.x - terrainPos.x) / terrainData.size.x) * terrainData.alphamapWidth);
int mapZ = (int)(((WorldPos.z - terrainPos.z) / terrainData.size.z) * terrainData.alphamapHeight);
float[,,] splatmapData = terrainData.GetAlphamaps(3, 3, 15, 15);
terrainData.SetAlphamaps (mapX, mapZ, splatmapData);
terrain.Flush ();
}
The data returned by GetAlphamaps
The returned array is three-dimensional - the first two dimensions represent x and y coordinates on the map, while the third denotes the splatmap texture to which the alphamap is applied.
Or in simple words a float[x, y, l] where
x = width in pixels
y = height in pixels
l = Texture-Layer
So lets say you want to set it to a certain texture at this pixel coordinates what you do is
set the weight for the texture's layer to 1
set all other layers weight to 0
So let's say you have e.g. 3 Layers and you want the second one (= index 1) to be the full weighted texture:
float[,,] splatmapData = terrainData.GetAlphamaps(mapX, mapZ, 15, 15);
// Iterate over x-y coordinates within the array
for(var y = 0; i < 15; y++)
{
for(var x = 0; x < 15; x++)
{
// Set first layers weight to 0
splatmapData[x, y, 0] = 0;
// Set second layer's weight to 1
splatmapData[x, y, 1] = 1;
// Set third layer's weight to 0
splatmapData[x, y, 2] = 0;
}
}
terrainData.SetAlphamaps(mapX, mapZ, splatmapData);
I would then implement an enum for the layers like let's say
public enum TerrainLayer
{
Default = 0,
Green,
Red
}
so you can simply pass the according layer index as a parameter - a bit more secure than passing in the int values themselves:
private void ChangeTexture(Vector3 worldPos, TerrainLayer toLayer)
{
print ("changeTexture");
int mapX = (int)(((worldPos.x - terrainPos.x) / terrainData.size.x) * terrainData.alphamapWidth);
int mapZ = (int)(((worldPos.z - terrainPos.z) / terrainData.size.z) * terrainData.alphamapHeight);
float[,,] splatmapData = terrainData.GetAlphamaps(mapX, mapZ, 15, 15);
for(var z = 0; z < 15; z++)
{
for(var x = 0; x < 15; x++)
{
// This ofcourse would be more efficient if you do this only once
// e.g. in Awake since the enum won't change on runtime
var values = (TerrainLAyer[])Enum.GetValues(typeof(TerrainLayer));
// Iterate through the enum and
for(var l = 0; l < values.Length; l++)
{
// set all layers to 0 except the toLayer
splatmapData[x, z, l] = values[l] == toLayer ? 1 : 0;
}
}
}
terrainData.SetAlphamaps (mapX, mapZ, splatmapData);
terrain.Flush ();
}
Now you would simply call it e.g.
ChangeTexture(somePosition, TerrainLayer.Green);
I'm trying to make a list using this class:
public class Edges
{
public int no_piece;
public int edge;
public int[,] edge_pixels;
}
This is to assemble a puzzle. I am trying to save the characteristics of each piece this way: no_piece - the number of the piece /// edge - which edge (top as '0', left as '1', bottom as '2' and right as '3') /// edge_pixels - the number of pixels for each channel, for example:
edge_pixels[0,x] would have the values of each pixel of an edge, of n piece);
This is the way I tried;
List<Edges> edges_list = new List<Edges>();
for (i = 0; i < number of pieces; i++)
{
for (y = vector_coordinates[i,1]; y < vector_coordinates[i,3]; y++)
{//vectors with top and bottom coordinates of the piece in the original image
for (x = vector_coordinates[i,0]; x < vector_coordinates[i,2]; x++)
{// the same goes for x but left and right
if (y == vector_coordinates[i, 1]) //top
{
for (aux_rgb = 0; aux_rgb < 3; aux_rgb++)
{
Edges edge = new Edges();
edge.no_piece = i;
edge.edge = 1;
edge.edge_pixels[aux_rgb, aux_count_pixel] = (int)dataPtr[aux_rgb];
edges_list.Add(edge);
}
}
aux_count_pixel++;
}
(...)
But it doesn't work and I donĀ“t understand why. I'm not sure if I made myself clear. Thank you
Try following.
List<Edges> edges_list = new List<Edges>();
for (i = 0; i < number of pieces; i++)
{
for (y = vector_coordinates[i,1]; y < vector_coordinates[i,3]; y++)
{//vectors with top and bottom coordinates of the piece in the original image
for (x = vector_coordinates[i,0]; x < vector_coordinates[i,2]; x++)
{// the same goes for x but left and right
if (y == vector_coordinates[i, 1]) //top
{
Edges edge = new Edges();
edges_list.Add(edge);
edge.no_piece = i;
for (aux_rgb = 0; aux_rgb < 3; aux_rgb++)
{
edge.edge = 1;
edge.edge_pixels[aux_rgb, aux_count_pixel] = (int)dataPtr[aux_rgb];
}
}
aux_count_pixel++;
}
I have an image, and I want to take each square of 256X256 pixels, find the mean color, and draw that square with said color.
Problem: it seems that after the first square the processing suddenly stops, but after following the program I can see the indexes are just fine. I have no idea if the problem lies with file writing in my computer system, or a wrong use of the "Bitmap" class functions.
original:
result:
code:
public const int big =256;
public const int small = 16;
static void Main(string[] args)
{
Bitmap bt = new Bitmap(#"C:\Users\mishe\Desktop\00_sorted images - training\general shores\agulhas_oli_2016146_lrg.jpg");
Bitmap bt2 = bt;
Color MeanColor;
double r = 0;
double g = 0;
double b = 0;
int i = 0;
int j = 0;
//big loop to go over all image
for (i = 0; i < bt.Height-257; i+=256)
{
for (j = 0; j < bt.Width-257; j+=256)
{
/////////////////////////////
//small loop on 1 square to get the mean color of the area
for (int x = i; x < big; x++)
{
for (int y = j; y < big; y++)
{
r += bt.GetPixel(x, y).R;
g += bt.GetPixel(x, y).G;
b += bt.GetPixel(x, y).B;
}
}
/////////////////////////////
r = r / Math.Pow(big, 2);
g = g / Math.Pow(big, 2);
b = b / Math.Pow(big, 2);
MeanColor = Color.FromArgb((int)r, (int)g, (int)b);
/////////////////////////////
//small loop on the same square to set the color
for (int x = i; x < big; x++)
{
for (int y = j; y < big; y++)
{
bt2.SetPixel(x, y, MeanColor);
}
}
/////////////////////////////
}
}
bt2.Save(#"C:\Users\mishe\Desktop\compressed image.jpg", ImageFormat.Jpeg);
}
This line:
//small loop on 1 square to get the mean color of the area
for (int x = i; x < big; x++)
After the first square, x will be 256, so it won't do the small loop.
I think you want:
for (int x = i; x < i + big; x++)
Or your small loop could be:
for (int x = 1; x < big; x++)
and then add the large and small values inside the loop:
r += bt.GetPixel(i + x, j + y).R;
I have an image that is 512x280 pixels. I want to populate a 64x35 array with every 8th pixel in the matrix.
Here is what I have right now:
Color[,] imgArray = new Color[b.Width, b.Height];
for (int y = 0; y < 35; y++)
{
for (int x = 0; x < 64; x++)
{
imgArray[x, y] = b.GetPixel(x, y);
}
}
But that will get just the top corner of the image. How would I change the loop so it grabs every 8th pixel to fill the array with?
edit: I think I may have gotten it. Can someone read this and assure me that it is correct?
Color[,] imgArray = new Color[64, 35];
for (int y = 0; y < 280; y+=8)
{
for (int x = 0; x < 512; x+=8)
{
imgArray[x, y] = b.GetPixel(x, y);
}
}
Simply multiply the coordinates by 8 when you get the pixels:
Color[,] imgArray = new Color[64, 35];
for (int y = 0; y < 35; y++) {
for (int x = 0; x < 64; x++) {
imgArray[x, y] = b.GetPixel(x * 8, y * 8);
}
}