Basically what im doing is using NAudio to get peak info. I've made a set of 20 images that are going to represent the EQ. I've done some code to check the peak value and change the image in the picturebox in realtime with a timer. Though sometimes I see visual lag and I think the code is quite inefficient. Is there a more efficient code than this large chunk of ifs
private void EQ_TIMER_Tick(object sender, EventArgs e)
{
int vol = (int)((float)(device.AudioMeterInformation.MasterPeakValue * 100));
if ((vol > 0) && (vol < 5))
{
WINGS_FRAME.Image = Properties.Resources.RES_EQ5;
}
else if ((vol > 5) && (vol < 10))
{
WINGS_FRAME.Image = Properties.Resources.RES_EQ10;
}
else if ((vol > 10) && (vol < 15))
{
WINGS_FRAME.Image = Properties.Resources.RES_EQ15;
}
else if ((vol > 15) && (vol < 20))
{
WINGS_FRAME.Image = Properties.Resources.RES_EQ20;
}
else if ((vol > 20) && (vol < 25))
{
WINGS_FRAME.Image = Properties.Resources.RES_EQ25;
}
else if ((vol > 25) && (vol < 30))
{
WINGS_FRAME.Image = Properties.Resources.RES_EQ30;
}
else if ((vol > 30) && (vol < 35))
{
WINGS_FRAME.Image = Properties.Resources.RES_EQ35;
}
else if ((vol > 35) && (vol < 40))
{
WINGS_FRAME.Image = Properties.Resources.RES_EQ40;
}
else if ((vol > 40) && (vol < 45))
{
WINGS_FRAME.Image = Properties.Resources.RES_EQ45;
}
else if ((vol > 45) && (vol < 50))
{
WINGS_FRAME.Image = Properties.Resources.RES_EQ50;
}
else if ((vol > 50) && (vol < 55))
{
WINGS_FRAME.Image = Properties.Resources.RES_EQ55;
}
else if ((vol > 55) && (vol < 60))
{
WINGS_FRAME.Image = Properties.Resources.RES_EQ60;
}
else if ((vol > 60) && (vol < 65))
{
WINGS_FRAME.Image = Properties.Resources.RES_EQ65;
}
else if ((vol > 65) && (vol < 70))
{
WINGS_FRAME.Image = Properties.Resources.RES_EQ70;
}
else if ((vol > 70) && (vol < 75))
{
WINGS_FRAME.Image = Properties.Resources.RES_EQ75;
}
else if ((vol > 75) && (vol < 80))
{
WINGS_FRAME.Image = Properties.Resources.RES_EQ80;
}
else if ((vol > 80) && (vol < 85))
{
WINGS_FRAME.Image = Properties.Resources.RES_EQ85;
}
else if ((vol > 85) && (vol < 90))
{
WINGS_FRAME.Image = Properties.Resources.RES_EQ90;
}
else if ((vol > 90) && (vol < 95))
{
WINGS_FRAME.Image = Properties.Resources.RES_EQ95;
}
else if ((vol > 95) && (vol < 100))
{
WINGS_FRAME.Image = Properties.Resources.RES_EQ100;
}
else if (vol == 0)
{
WINGS_FRAME.Image = null;
}
}
Try something like this:
int vol = 38;
int imgnum = vol/5*5;
string imgname = string.Format("RES_EQ{0}", imgnum);
var image = Properties.Resources.ResourceManager.GetObject(imgname);
Integer division "rounds" the volume to a step number, then multiplying by 5 gives the actual number for the image.
Related
im new to C# and my logic wont work
it keeps on displaying my else command
there is no errors
///////////////////////////////////////////////////////////////////
int age = 12;
if ((age <= 0) && (age >= 12))
{
Console.WriteLine("You are young");
}
else if ((age <= 13) && (age >= 17))
{
Console.WriteLine("You're a teen");
}
else if ((age <= 18) && (age >= 50))
{
Console.WriteLine("You're an adult");
}
else if ((age <= 51) && (age >= 120))
{
Console.WriteLine("You're Elderly");
}else
{
Console.Beep();
}
///////////////////////////////////////////////////////////////////
You just need to swap your conditions for every age range:
int age = 12;
if ((age >= 0) && (age <= 12))
{
Console.WriteLine("You are young");
}
else if ((age >= 13) && (age <= 17))
{
Console.WriteLine("You're a teen");
}
else if ((age >= 18) && (age <= 50))
{
Console.WriteLine("You're an adult");
}
else if ((age >= 51) && (age <= 120))
{
Console.WriteLine("You're Elderly");
}
else
{
Console.Beep();
}
int age = 12;
if ((age >= 0) && (age <= 12))
{
Console.WriteLine("You are young");
}
else if ((age >= 13) && (age <= 17))
{
Console.WriteLine("You're a teen");
}
else if ((age >= 18) && (age <= 50))
{
Console.WriteLine("You're an adult");
}
else if ((age >= 51) && (age <= 120))
{
Console.WriteLine("You're Elderly");
}else
{
Console.Beep();
}
There is nothing wrong with syntax but your logic
Take a look at all <= condition.
The conditions within the if statements aren't correct, try this:
int age = 12;
if ((age >= 0) && (age <= 12))
{
Console.WriteLine("You are young");
}
else if ((age >= 13) && (age <= 17))
{
Console.WriteLine("You're a teen");
}
else if ((age >= 18) && (age <= 50))
{
Console.WriteLine("You're an adult");
}
else if ((age >= 51) && (age <= 120))
{
Console.WriteLine("You're Elderly");
}
else
{
Console.Beep();
}
i´m having a little problem with my world collison in a platformer game i´m working on. I know why the (or I think I know) problem occours. My program seems to go into the first "if" when it´s not ment too. Something there is wrong (I think) but I can´t find what.
Secondly I wonder if my solution over all is an good idea or if a should go for something else. If so what´s wrong? I´m pretty beginner on programming so please be gentle with me ;D
protected void CheckWorldCollison()
{
float collisionWidth = this.width + (Tile.GetTileSize() * 2);
float collisionHeight = this.height + (Tile.GetTileSize() * 2);
for (int x = -1; x < (collisionWidth / Tile.GetTileSize()) - 1; x++)
{
for (int y = -1; y < (collisionHeight / Tile.GetTileSize()) - 1; y++)
{
Vector2 tempTilePosition = new Vector2((int)(this.position.X / Tile.GetTileSize()) + x, (int)(this.position.Y / Tile.GetTileSize()) + y);
if (tempTilePosition.X >= 0 && tempTilePosition.X < (Game1.testMap.GetWidth())
&& tempTilePosition.Y >= 0 && tempTilePosition.Y < (Game1.testMap.GetHeight()))
{
this.tilesToCheck.Add(Game1.testMap.GetTile(tempTilePosition));
}
}
}
foreach (Tile t in this.tilesToCheck)
{
t.setIsCkecked(true);
if (t.GetTileType() != TileType.transparent)
{
Vector2 distance = this.position - t.GetPosition();
//Console.WriteLine(distance);
Console.WriteLine((distance + this.origin).X);
if (((distance + this.origin).X >= 0 && (distance + this.origin).X <= Tile.GetTileSize())
|| ((distance - this.origin).X >= 0 && (distance - this.origin).X <= Tile.GetTileSize()))
{
Console.WriteLine((distance + this.origin).Y);
if ((distance + this.origin).Y <= 0)
{
if (this.speed.Y > 0)
{
if ((distance + this.origin + this.speed).Y > 0)
{
this.position.Y = (t.GetPosition() - this.origin).Y;
this.speed.Y = 0;
}
}
}
if (((distance - this.origin).Y - Tile.GetTileSize()) <= 0)
{
if (this.speed.Y < 0)
{
if (((distance - this.origin + this.speed).Y - Tile.GetTileSize()) < 0)
{
this.position.Y = (t.GetPosition() + this.origin).Y + Tile.GetTileSize();
this.speed.Y = 0;
}
}
}
}
if (((distance + this.origin).Y >= 0 && (distance + this.origin).Y <= Tile.GetTileSize())
|| ((distance - this.origin).Y >= 0 && (distance - this.origin).Y <= Tile.GetTileSize()))
{
if ((distance + this.origin).X <= 0)
{
if (this.speed.X > 0)
{
if ((distance + this.origin + this.speed).X > 0)
{
this.position.X = (t.GetPosition() - this.origin).X;
this.speed.X = 0;
}
}
}
if (((distance - this.origin).X - Tile.GetTileSize()) >= 0)
{
if (this.speed.X < 0)
{
if (((distance - this.origin + this.speed).X - Tile.GetTileSize()) < 0)
{
this.position.X = (t.GetPosition() + this.origin).X + Tile.GetTileSize();
this.speed.X = 0;
}
}
}
}
}
}
this.tilesToCheck.Clear();
}
Just wanna say that I guess that these questions pop up here like all the time, but I hadn´t written if I really didn´t need help.
I want to know performance differences between below two code lines. Which one is faster?
1.
anyList = Enumerable.Range(0, 1440).Select((n, i) =>
{
if ((i >= 480 && i < 790) || (i >= 1050 && i < 1170)
return 0;
else if ((i >= 790 && i < 1050)
return 1;
else
return 2;
}).ToList();
2.
for (int i = 0; i < 1440; i++)
{
if ((i >= 480 && i < 790) || (i >= 1050 && i < 1170)
anyLisy.Add(0);
else if ((i >= 790 && i < 1050)
anyLisy.Add(1);
else
anyLisy.Add(2);
}
Which one is faster, which one has much more allocated memory?
use StopWatch and make some benchmark something like this
Stopwatch stopWatch = new Stopwatch();
stopWatch.Start();
for (int i = 0; i < 1440; i++)
{
if ((i >= 480 && i < 790) || (i >= 1050 && i < 1170))
anyLisy.Add(0);
else if (i >= 790 && i < 1050)
anyLisy.Add(1);
else
anyLisy.Add(2);
}
stopWatch.Stop();
TimeSpan ts = stopWatch.Elapsed;
// Format and display the TimeSpan value.
string elapsedTime = String.Format("{0:00}:{1:00}:{2:00}.{3:00}",
ts.Hours, ts.Minutes, ts.Seconds,
ts.Milliseconds / 10);
Console.WriteLine("RunTime " + elapsedTime);
I want to loop all blueBallPosition[i] and greenBallPosition[i] except blueBallPosition[0] and greenBallPosition[0]. And also, I want to do an if else statement if all the variables except [0], are > 360. Whereas, [0] are < 360. I have tried to do a loop. However, if blueBallPosition[1] > 360 and greenBallPosition[1] > 360, pause will be set to true already. I want to loop through all the variables to make sure they are > 360, then pause set to true.
for (int i = 1; i < levelBall; i++)
{
if ((blueBallPosition[0] < 360) &&
(greenBallPosition[0] < 360) &&
(blueBallPosition[i] > 360) &&
(greenBallPosition[i] > 360))
{
pause = true;
}
}
For your information, levelBall is set as 4.
I have solved it. I just have to add a counter to it.
for (int i = 1; i < levelBall; i++)
{
if ((blueBallPosition[0] < 360) && (greenBallPosition[0] < 360) && (blueBallPosition[i] > 360) && (greenBallPosition[i] > 360))
{
count++;
}
}
if (count == (levelBall))
{
pause = true;
}
else
{
count = 0;
}
So you only want to pause if all balls (apart from 0) are > 360?
bool pause = false;
// Check ball[0] outside the loop for performance.
if (blueBallPosition[0] < 360 && greenBallPosition[0] < 360)
{
pause = true;
for (int i = 1; i < levelBall; i++)
{
if (blueBallPosition[i] <= 360) || (greenBallPosition[i] <= 360))
{
pause = false;
// You have found a ball that doesn't match, so no need
// to keep checking.
break;
}
}
}
Since you want to check if all the variables fulfill that condition you can't set it to true if one fulfills them, you can rewrite it to start as true, check if one of them does not fulfill it and then set it to false instead. If it still true afterwards you know what all the variables fulfill the condition.
pause = true;
for(...)
if(blue[0] >= 360) || green[0] >= 360 || blue[i] <= 360 || green[i] <= 360)
pause = false;
Try this:
if (blueBallPosition[0] < 360 &&
greenBallPosition[0] < 360 &&
blueBallPosition.Skip(1).All(b => b > 360) &&
greenBallPosition.Skip(1).All(b => b > 360))
pause = true;
you may try use linq to make it more readable:
if (blueBallPosition[0] < 360 && greenBallPosition[0] < 360)
{
var paulse = blueBallPosition.Select((b, idx) =>
new { Blue = b, Green = greenBallPosition[idx] })
.Skip(1).All(x => x.Blue > 360 && x.Green > 360);
}
pause = true;
for (int i = 1; i < levelBall; i++)
{
if ((blueBallPosition[0] >= 360) ||
(greenBallPosition[0] >= 360) ||
(blueBallPosition[i] <= 360) ||
(greenBallPosition[i] <= 360))
{
pause = false;
break;
}
}
First, move the checks that you can outside of the loop. Then assume that all checks pass and then undo that assumption if it turns out to be false:
if ((blueBallPosition[0] < 360) &&
(greenBallPosition[0] < 360))
{
pause = true;
for (int i = 1; i < levelBall; i++)
{
if((blueBallPosition[i] <= 360) ||
(greenBallPosition[i] <= 360))
{
pause = false;
}
}
}
very short code just like:
var pause = false;
if (blueBallPosition[0] < 360 && greenBallPosition[0] < 360)
{
pause = blueBallPosition.Any(b => b <= 360) || greenBallPosition.Any(b => b < 360);
}
Do not forget the using in namespace.
using System.Linq;
Alex and Rex the bad thing on your code u are using ALL = all means loop all elements if you modify your solution with Any will be better.
is it possible to detect certain color like grey or red from 24bpp image so i can extract or highlight the specific color on image provided
Yes, you can compare pixel values.
Following link shows
How to change color of Image at runtime
How to compare,
bmp.GetPixel(x, y) == Color.Red
Like #Tilak said, you can use bitmap.GetPixel with a for loops to search the image, like this
for(int y = 0; y < b.Height; y++)
{
for(int x = 0; x < b.Width; x++)
{
if(b.GetPixel(x, y) == Color.Red)
{
//do something
}
}
}
But if you want a "simpler" way, you can just use
foreach(Color c in b)
{
if (c == Color.Red)
{
//do something
}
}
But since the arbg values might not be exactly equal to Red's, do something like this:
int pixelA = c.A;
int pixelR = c.R;
int pixelG = c.G;
int pixelB = c.B;
int searchA = Color.Red.A;
int searchR = Color.Red.R;
int searchG = Color.Red.G;
int searchB = Color.Red.B;
diffA = Math.Abs(pixelA - searchA);
diffR = Math.Abs(pixelR - searchR);
diffG = Math.Abs(pixelG - searchG);
diffB = Math.Abs(pixelB - searchB);
if (diffA < 10 && diffB < 10 && diffG < 10 && diffR < 10)
{
//do something }
if (diffA > 10 && diffA < 20 && diffR > 10 && diffR < 20 && diffG > 10 && diffG < 20 &&
diffB > 10 && diffB < 20)
{
//do something }
if (diffA > 20 && diffA < 130 && diffR > 20 && diffR < 130 && diffG > 20 && diffG < 130 &&
diffB > 20 && diffB < 130)
{
//do something
}
if (diffA > 130 && diffA < 240 && diffR > 130 && diffR < 240 && diffG > 130 && diffG < 240 &&
diffB > 130 && diffB < 240)
{
//do something
}
if (diffA > 240 && diffA < 350 && diffR > 240 && diffR < 350 && diffG > 240 && diffG < 350 &&
diffB > 240 && diffB < 350)
{
//do something
}
So you can see the different degrees of the color. Hope this helps!