I'm drawing a cross in the console. Here's my code:
for (int x = 0; x < 320; x++)
{
for (int y = 0; y < 100; y++)
{
Console.SetCursorPosition(Convert.ToInt32(x / 4),Convert.ToInt32(y / 4));
if (x == 160)
{
if (y == 50)
{
Console.Write("┼");
}
else
{
Console.Write("│");
}
}
else
{
if (y == 50)
{
Console.Write("─");
}
}
}
}
The console draws the cross except the middle "┼" symbol. When I debugged the program it hit the Console.Write("┼"); line. Instead the program written the "─" symbol. What I'm doing wrong and how to solve this problem?
The problem seems to be that you are writing to each location multiple times because of the part where you divide by 4.
When (x, y) is (160, 50) you write a cross at (40, 12). Then (x, y) is (160, 51) so you write a vertical pipe at the same location, overwriting the cross. Then later when (x, y) becomes (161, 50) you overwrite the pipe with a dash.
Try this instead:
for (int x = 0; x < 80; x++)
{
Console.SetCursorPosition(x, 12);
Console.Write("─");
}
for (int y = 0; y < 25; y++)
{
Console.SetCursorPosition(40, y);
Console.Write("|");
}
Console.SetCursorPosition(40, 12);
Console.Write("┼");
Related
I'm working on a procedurally generated map/game. I have 3D chunks with 20x20x20 points in a 3D array. I'm using marching cubes algorithm to generate the chunks. I try to manipulate the density with adding and subtracting the values but in the same time trying to sync the neighbour chunks edge together the with chunk im modifying.
Here is the code i tough it will work, but its not working:
`for (int i = 0; i < 9; i++)
{
Chunk nChunk = neighbours[i].GetComponent<Chunk>();
if (i == 4) continue; //skip self
for (int z = 0; z < 20; z += 19)
{
for (int x = 0; x < 20; x += 19)
{
for (int zz = 0; zz < 20; zz += 19)
{
for (int xx = 0; xx < 20; xx += 19)
{
for (int y = 0; y < 20; y++)
{
if (Points[xx, y, zz].wPosition == nChunk.Points[x, y, z].wPosition)
nChunk.Points[x, y, z].density = Points[xx, y, zz].density;
}
}
}
}
}
}`
I want to check the edges and not all Points to save performance. the y coords the height.
Here is how i call this:
//mouse click
if (Input.GetMouseButtonDown(0))
{
Ray raym = Camera.main.ScreenPointToRay(Input.mousePosition);
RaycastHit hitm;
if (Physics.Raycast(raym, out hitm))
{
if (hitm.transform.GetComponent<Chunk>())
{
Chunk cScript = hitm.transform.GetComponent<Chunk>();
for (int z = 0; z < 20; z++)
{
for (int y = 1; y < 19; y++)
{
for (int x = 0; x < 20; x++)
{
if (PointInsideSphere(cScript.Points[x, y, z].wPosition, hitm.point, 1f))
cScript.Points[x, y, z].density -= 0.1f;
}
}
}
cScript.SyncNeighbours();
}
}
}
In this way its working good but i want to check only the edges.
for (int i = 0; i < 9; i++)
{
Chunk nChunk = neighbours[i].GetComponent<Chunk>();
if (i == 4) continue; //skip self
for (int z = 0; z < 20; z ++)
{
for (int x = 0; x < 20; x ++)
{
for (int zz = 0; zz < 20; zz ++)
{
for (int xx = 0; xx < 20; xx ++)
{
for (int y = 0; y < 20; y++)
{
if (Points[xx, y, zz].wPosition.x == nChunk.Points[x, y, z].wPosition.x && Points[xx, y, zz].wPosition.y == nChunk.Points[x, y, z].wPosition.y && Points[xx, y, zz].wPosition.z == nChunk.Points[x, y, z].wPosition.z)
nChunk.Points[x, y, z].density = Points[xx, y, zz].density;
}
}
}
}
}
}
for (int j = 0; j < 9; j++)
{
neighbours[j].GetComponent<Chunk>().UpdateChunk();
}
It's solved in different way: When the game creates a chunk it checks points with 0 and 19 positions in 3d array then add them to a list. from that list i can check the points on neighbouring faces and make the densitys equal.
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'm doing a bit of optimization and found out that this:
for (int x = -1; x < 2; x++)
{
for (int y = -1; y < 2 ; y++)
{
if (((x * x) ^ (y * y)) != 1)
{
continue;
}
}
}
takes more than twice as long to run as this:
for (int x = -1; x < 2; x++)
{
for (int y = -1; y < 2 && ((x * x) ^ (y * y)) == 1; y++)
{
}
}
What is going on here?
The purpose is avoiding diagonal(-1,-1; -1,1 ...) and the origin(0,0) fields
The second code ends the inner loop when ((x * x) ^ (y * y)) != 1, whereas the first code only skips one iteration of the inner loop. Since they don't do the same thing, all bets are off on comparing performance.
I want to create a simple square on C# which will be used as a game board.
I am trying to do it using nested loops and have looked into how people make squares this way however I am having difficulty understanding how its done.
This is my code so far for the board:
for (int x = 0; x < 8; x = x + 1)
for (int y = 0; y < 8; y = y + 1)
if (board[x, y] == SquareState.gotCheese)
Console.Write("C");
else
Console.Write("*");
It does print out a * if there is no cheese and a C is there is cheese on the board, however its all in a line and doesn't look like a board. Like this:
*****************C*******
This is the structure for the board if its any help
static SquareState[,] board = new SquareState[8, 8];
The fact that it is writing all in line is because you are now telling the console to create a new line. Console.write() just append strings inline with the precedent.
You for cycle should also be an y-first cycle, so you will cycle each horizontal value (x) and then pass to a new vertical one.
for (int y = 0; y < 8; y++){
for (int x = 0; x < 8; x++){
if (board[x, y] == SquareState.gotCheese)
Console.Write("C");
else
Console.Write("*");
}
Console.WriteLine();
}
If you don't swap the cycles your result will be wrong, for example in a 3 by 3 square where x goes from 0 to 2, from left to right and y goes from 0 to 2 from top to bottom, you will have:
External FOR entering x = 0
Internal FOR entering y = 0
writing the 'cell' (0, 0)
Internal FOR entering y = 1
writing the 'cell' (0, 1)
Internal FOR entering y = 2
writing the 'cell' (0, 2)
writing a new line
External FOR entering x = 1
...
The result of this will be:
(0,0)(0,1)(0,2)
(1,0)(1,1)(1,2)
(2,0)(2,1)(2,2)
That is wrong, it should be:
--------------------> x
(0,0)(1,0)(2,0) |
(0,1)(1,1)(2,1) |
(0,2)(1,2)(2,2) |
V y
You need to print a newline after the inner loop but inside the outer loop.
Console.WriteLine("");
for (int x = 0; x < 8; x = x + 1){
for (int y = 0; y < 8; y = y + 1){
if (board[x, y] == SquareState.gotCheese)
Console.Write("C");
else
Console.Write("*");
Console.WriteLine("");
}
}
I was bored and had only 30 minutes of free time so I decided to have a crack at making the game of life. I followed that rules on wikipedia and it doesn't seem to be working correctly. Could someone please tell me what I would be doing wrong?
Here are the rules:
Any live cell with fewer than two live neighbours dies, as if caused by under-population.
Any live cell with two or three live neighbours lives on to the next generation.
Any live cell with more than three live neighbours dies, as if by overcrowding.
Any dead cell with exactly three live neighbours becomes a live cell, as if by reproduction.
public void PerformLogic()
{
if (in_game)
{
time_elapsed += rate;
if (time_elapsed > frame_rate)
{
time_elapsed = 0;
for (int x = 0; x < board_width; x++)
{
for (int y = 0; y < board_height; y++)
{
if (board[x, y] == alive)
{
int surrounding_cells = 0;
for (int x2 = -1; x2 <= 1; x2++)
{
for (int y2 = -1; y2 <= 1; y2++)
{
if (!(x2 + x <= -1 || y2 + y <= -1 || x + x2 >= board_width || y + y2 >= board_height))
{
if (board[x + x2, y + y2] == alive)
{
surrounding_cells++;
}
}
}
}
if (surrounding_cells < 2)
{
board[x, y] = dead;
}
if (surrounding_cells == 2 ||
surrounding_cells == 3)
{
board[x, y] = alive;
}
if (surrounding_cells > 3)
{
board[x, y] = dead;
}
}
else if (board[x, y] == dead)
{
int surrounding_cells = 0;
for (int x2 = -1; x2 <= 1; x2++)
{
for (int y2 = -1; y2 <= ; y2++)
{
if (!(x2 + x <= -1 || y2 + y <= -1 || x + x2 >= board_width || y + y2 >= board_height))
{
if (board[x + x2, y + y2] == alive)
{
surrounding_cells++;
}
}
}
}
if (surrounding_cells == 3)
{
board[x, y] = alive;
}
}
}
}
}
}
}
Any ideas?
I believe you are updating the board too early. The game of life should update the board after it finishes scanning the whole board, rather than while scanning.
E.g.:
if (surrounding_cells > 3)
{
board[x, y] = dead;
}
After this, for the cell next to it, this cell would be treated as dead.
Marc is right, too.
for (int x2 = -1; x2 <= 1; x2++)
{
for (int y2 = -1; y2 <= 1; y2++)
{
looks to me like you're including the central cell in this loop, so 9 instead of 8.
I'm not sure the nested for is the best option, but if you are using that, add:
if(x2 == 0 && y2 == 0) continue;
at the start of the inner loop (i.e. after the last line that I've posted above)