How can I reset the i variable inside my for loop? - c#

for (int k = 0; k < 32;k=k+1)
{
for (int j = 1; j < 32; j++)
{
double Score = (user0[k] * user0[j] + user1[k] * user1[j] + user2[k] * user2[j] + user3[k] * user3[j] + user4[k] * user4[j] + user5[k] * user5[j] + user6[k] * user6[j] + user7[k] * user7[j] + user8[k] * user8[j] + user9[k] * user9[j] + user10[k] * user10[j] + user11[k] * user11[j] + user12[k] * user12[j] + user13[k] * user13[j] + user14[k] * user14[j] + user15[k] * user15[j] + user16[k] * user16[j] + user17[k] * user17[j] + user18[k] * user18[j] + user19[k] * user19[j] + user20[k] * user20[j] + user21[k] * user21[j] + user22[k] * user22[j] + user23[k] * user23[j] + user24[k] * user24[j] + user25[k] * user25[j] + user26[k] * user26[j] + user27[k] * user27[j] + user28[k] * user28[j] + user29[k] * user29[j] + user30[k] * user30[j] + user31[k] * user31[j]) / ((Math.Sqrt(user0[k] * user0[k] + user1[k] * user1[k] + user2[k] * user2[k] + user3[k] * user3[k] + user4[k] * user4[k] + user5[k] * user5[k] + user6[k] * user6[k] + user7[k] * user7[k] + user8[k] * user8[k] + user9[k] * user9[k] + user10[k] * user10[k] + user11[k] * user11[k] + user12[k] * user12[k] + user13[k] * user13[k] + user14[k] * user14[k] + user15[k] * user15[k] + user16[k] * user16[k] + user17[k] * user17[k] + user18[k] * user18[k] + user19[k] * user19[k] + user20[k] * user20[k] + user21[k] * user21[k] + user22[k] * user22[k] + user23[k] * user23[k] + user24[k] * user24[k] + user25[k] * user25[k] + user26[k] * user26[k] + user27[k] * user27[k] + user28[k] * user28[k] + user29[k] * user29[k] + user30[k] * user30[k] + user31[k] * user31[k])) * (Math.Sqrt(user0[j] * user0[j] + user1[j] * user1[j] + user2[j] * user2[j] + user3[j] * user3[j] + user4[j] * user4[j] + user5[j] * user5[j] + user6[j] * user6[j] + user7[j] * user7[j] + user8[j] * user8[j] + user9[j] * user9[j] + user10[j] * user10[j] + user11[j] * user11[j] + user12[j] * user12[j] + user13[j] * user13[j] + user14[j] * user14[j] + user15[j] * user15[j] + user16[j] * user16[j] + user17[j] * user17[j] + user18[j] * user18[j] + user19[j] * user19[j] + user20[j] * user20[j] + user21[j] * user21[j] + user22[j] * user22[j] + user23[j] * user23[j] + user24[j] * user24[j] + user25[j] * user25[j] + user26[j] * user26[j] + user27[j] * user27[j] + user28[j] * user28[j] + user29[j] * user29[j] + user30[j] * user30[j] + user31[j] * user31[j])));
if (Score > simScore)
{
simScore = Score;
}
}
System.Console.WriteLine("Score =" + simScore);
}
I think you can ignore the long equation.
I need this program to compare 1 book to 32 other books.
The for (int k = 0; k < 5;k=k+1)loop is for the one selected book to be compared to 32 others (then give the greatest similarity rating).
The for (int j = 1; j < 32; j++) loop is to allow all the different books to be compared to the the book selected (book k).
The problem is that a book cannot be compared to itself because I think it ruins the equation and I get non-sensical values for the similarity ratings.
How can I omit a book?
(For example: When comparing k=3 (book 3) to other books, how can I make it so that j will not use the same reference?

Start the second loop from one past the first. Comparing the pair (a,b) is the same as comparing (b,a), so you don't need to start from the beginning with each subsequent book.
for (var k = 0; k < 5; k++)
{
for (var j = k + 1; j < 5; j++)
{
....
// if you need the complete set, you can store both
rating[j][k] = ...
rating[k][j] = ...
}
}

The most straightforward way would be to put an if statement that only evaluates the body of the second loop if k != j. I'm not familiar with C#, so there may be other ways of doing it that I'm not aware of, but this is how I would do it in Python.

How about using continue?
for (int k = 0; k < 32; k++) {
for (int j = 0; j < 32; j++) {
if (j == k)
continue;
// Compare
if (isMatch)
break;
}
}

Related

C# Multiplication Table - Only 1 multiplication is showing

I am creating a multiplication table, when you input a number and click the calculate button it should display. I have tried watching a few tutorials on YouTube and have checked out some coding forums however I can only find people using the Console Application however I am using the Windows Form Application
1 * 1 = 1
2 * 1 = 2
3 * 1 = 3
4 * 1 = 4
5 * 1 = 5
6 * 1 = 6
7 * 1 = 7
8 * 1 = 8
9 * 1 = 9
10 * 1 = 10
However, when I run the program it only displays
1 * 10 = 10
Here is my code;
private void btnCalc_Click(object sender, EventArgs e)
{
int n, i;
n = Int32.Parse(txtNum.Text);
for (i = 1; i <= 10; ++i)
txtCalc.Text = Convert.ToString(n + " * " + i + " = " + n * i);
}
This loop keeps setting the control's text to a different value over and over, leaving you to see only the final value.
for (i = 1; i <= 10; ++i)
{
txtCalc.Text = Convert.ToString(n + " * " + i + " = " + n * i);
}
A straightforward solution is:
string text = "";
for (i = 1; i <= 10; ++i)
{
text += Convert.ToString(n + " * " + i + " = " + n * i);
}
txtCalc.Text = text;
You will still run into some formatting issues you'll need to solve, but you'll get the fundamental info in there.
You're overwriting the text over and over again. What you want to do is append new text every time through the loop. Try something like:
txtCalc.Text = "";
for (i = 1; i <= 10; ++i)
{
txtCalc.Text += Convert.ToString(n + " * " + i + " = " + n * i) + Environment.NewLine;
}
your txtCalc.Text... overwrites the field in every iteration. You probably want something like this:
txtCalc.Text = "";
for (i = 1; i <= 10; ++i)
{
txtCalc.Text += Convert.ToString(n + " * " + i + " = " + n * i);
}

C# double rounded down to zero [duplicate]

This question already has answers here:
Why does division result in zero instead of a decimal?
(5 answers)
Closed 5 years ago.
I have some C# code for converting an omnicam picture to a normal image. However, the formula is behaving differently than expected.
Code:
int Rin = 35; //Image specific variable
int Rout = 237; //Image specific variable
int cx = 312; //Image specific variable
int cy = 239; //Image specific variable
int width = (int) Math.Round(2 * Math.PI * Rout);
int height = (int) Math.Round((double)(Rout - Rin));
for (int i = 0; i < image.GetLength(0); i++)
{
double y1 = i / height;
double r = Rin + (1 - y1 * height);
for (int j = 0; j < image.GetLength(1); j++)
{
double x1 = j / width;
double theta = -2 * Math.PI * (j / width);
double xp = r * Math.Cos(theta) + cx;
double yp = r * Math.Sin(theta) + cy;
Console.WriteLine(theta + " = " + (-2 * Math.PI) + " * (" + j + " / " + width + ")");
Console.WriteLine(xp + " = " + r + " * " + Math.Cos(theta) + " + " + cx);
Console.WriteLine(yp + " = " + r + " * " + Math.Sin(theta) + " + " + cy);
Console.WriteLine("");
omnicam[i, j] = Color.FromArgb(1, 1, 1);
}
}
However, this prints out
0 = -6.28318530717959 * (82 / 1489)
348 = 36 * 1 + 312
239 = 36 * 0 + 239
If I print x1, it shows 0 for all values. However, I'm not sure why. The loop goes up to 895, and 895 / 1489 = 0.601... So why is it rounded down automatically?
double x1 = j / width;
Both j and width are INTs.
double x1 = j / (double)width;
Will give you the expected result.
If both of the arguments in C# is a int, then the result will be a int.
If any of the arguments in C# is a double, a double divide is used which results in a double.

How do I merge layers using raw pixel data in C# UWP?

Because the Universal Windows Platform does not support flattening Bitmap "layers" by default, I have been creating an algorithm (based on one found here) to take each individual pixels of each layer, and overlay them one over another. The problem is that because I'm using Alpha to allow images to have transparent pixels, I am unsure how to properly merge the pixels so it looks as though one is being places on top of the other.
Here is my code so far:
private unsafe SoftwareBitmap CompileImage()
{
SoftwareBitmap softwareBitmap = new SoftwareBitmap(BitmapPixelFormat.Bgra8, Width, Height);
using (BitmapBuffer buffer = softwareBitmap.LockBuffer(BitmapBufferAccessMode.Write))
{
using (var reference = buffer.CreateReference())
{
byte* dataInBytes;
uint capacity;
((IMemoryBufferByteAccess)reference).GetBuffer(out dataInBytes, out capacity);
BitmapPlaneDescription bufferLayout = buffer.GetPlaneDescription(0);
for (int index = 0; index < layers.Length; index++)
{
for (int i = 0; i < bufferLayout.Height; i++)
{
for (int j = 0; j < bufferLayout.Width; j++)
{
if(index == 0)
{
dataInBytes[bufferLayout.StartIndex + bufferLayout.Stride * i + 4 * j + 0] =
layers[index].pixelData[bufferLayout.StartIndex + bufferLayout.Stride * i + 4 * j + 0]; //B
dataInBytes[bufferLayout.StartIndex + bufferLayout.Stride * i + 4 * j + 1] =
layers[index].pixelData[bufferLayout.StartIndex + bufferLayout.Stride * i + 4 * j + 1]; //G
dataInBytes[bufferLayout.StartIndex + bufferLayout.Stride * i + 4 * j + 2] =
layers[index].pixelData[bufferLayout.StartIndex + bufferLayout.Stride * i + 4 * j + 2]; //R
dataInBytes[bufferLayout.StartIndex + bufferLayout.Stride * i + 4 * j + 3] =
layers[index].pixelData[bufferLayout.StartIndex + bufferLayout.Stride * i + 4 * j + 3]; //A
}
else if(index > 0)
{
//Attempts to "Average" pixel data
dataInBytes[bufferLayout.StartIndex + bufferLayout.Stride * i + 4 * j + 0] =
(byte)(dataInBytes[bufferLayout.StartIndex + bufferLayout.Stride * i + 4 * j + 0] *
layers[index].pixelData[bufferLayout.StartIndex + bufferLayout.Stride * i + 4 * j + 0] / 2);
dataInBytes[bufferLayout.StartIndex + bufferLayout.Stride * i + 4 * j + 1] =
(byte)(dataInBytes[bufferLayout.StartIndex + bufferLayout.Stride * i + 4 * j + 1] *
layers[index].pixelData[bufferLayout.StartIndex + bufferLayout.Stride * i + 4 * j + 1] / 2);
dataInBytes[bufferLayout.StartIndex + bufferLayout.Stride * i + 4 * j + 2] =
(byte)(dataInBytes[bufferLayout.StartIndex + bufferLayout.Stride * i + 4 * j + 2] *
layers[index].pixelData[bufferLayout.StartIndex + bufferLayout.Stride * i + 4 * j + 2] / 2);
dataInBytes[bufferLayout.StartIndex + bufferLayout.Stride * i + 4 * j + 3] =
(byte)(dataInBytes[bufferLayout.StartIndex + bufferLayout.Stride * i + 4 * j + 3] *
layers[index].pixelData[bufferLayout.StartIndex + bufferLayout.Stride * i + 4 * j + 3] / 2);
}
}
}
}
}
}
return softwareBitmap;
}
The current algorithm will take the average BGRA values of each pixel in every layer, and return the result. This is not what I intended it to do. How do I go about correcting it? Thanks in advance
To apply an alpha mix you multiply the previous color by the inverse alpha and the current color by the alpha, of course you should do it on a 0-1 scale (that's why graphic cards work with floats for colors).
Per example, suppose you have three layers, A, B and C, you use A as a base, then mix it with B: (pseudocode)
//bAlpha is thge alpha value of the current pixel from B in byte value
var alpha = bAlpha / 255.0f;
var invAlpha = 1.0f - alpha;
//aRGB is the RGB value of the pixel on the layer A
//bRGB is the RGB value of the pixel on the layer B
var abRGB = aRGB * invAlpha + bRGB * alpha;
And now you apply the third layer:
//cAlpha is thge alpha value of the current pixel from C in byte value
alpha = cAlpha / 255.0f;
invAlpha = 1.0f - alpha;
//abRGB is the RGB value of the previously mixed layers
//cRGB is the RGB value of the pixel on the layer C
var abcRGB = abRGB * invAlpha + cRGB * alpha;
Of course all of this is on ARGB mode, there's also pARGB in which the colors are already premultiplied, so you only need to multiply the previous color by the inverse alpha and add the current color.
//bAlpha is the alpha value of the current pixel from B in byte value
var invAlpha = 1.0f - (bAlpha / 255.0f);
//aRGB is the RGB value of the pixel on the layer A
//bRGB is the RGB value of the pixel on the layer B
var abRGB = aRGB * invAlpha + bRGB;
invAlpha = 1.0f - (cAlpha / 255.0f);
//abRGB is the RGB value of the previously mixed layers
//cRGB is the RGB value of the pixel on the layer C
var abcRGB = abRGB * invAlpha + cRGB;

"Object cannnot be cast from DBNull to other types"

I am working on an asp.net project, it takes values from the front end, stores them as string in a matrix. When the matrix size is greater than 5 * 5, it keep returns an InvalidCastException (It works fine on 5 * 5 matrix and under).
The code is attached below, with a screen shot of Exception:
public partial class WebForm1 : System.Web.UI.Page
{
public DataSet relmatrix = new DataSet();
public DataTable rt = new DataTable();
public Double[] inconsistency;
public int nodersnum;
public string strrel;
protected void Button1_Click(object sender, EventArgs e)
{
nodersnum = Convert.ToInt16(count.Text);
switch (nodersnum)
{
case 1:
break;
case 2:
{ strrel = RelationAB2.Text; }
break;
case 3:
{ strrel = RelationAB3.Text + " " + RelationAC3.Text + " " + RelationBC3.Text; }
break;
case 4:
{ strrel = RelationAB4.Text + " " + RelationAC4.Text + " " + RelationAD4.Text + " " + RelationBC4.Text + " " + RelationBD4.Text + " " + RelationCD4.Text; }
break;
case 5:
{ strrel = RelationAB5.Text + " " + RelationAC5.Text + " " + RelationAD5.Text + " " + RelationAE5.Text + " " + RelationBC5.Text + " " + RelationBD5.Text + " " + RelationBE5.Text + " " + RelationCD5.Text + " " + RelationCE5.Text + " " + RelationDE5.Text; }
break;
case 6:
{ strrel = RelationAB6.Text + " " + RelationAC6.Text + " " + RelationAD6.Text + " " + RelationAE6.Text + " " + RelationAF6.Text + " " + RelationBC6.Text + " " + RelationBD6.Text + " " + RelationBE6.Text + " " + RelationBF6.Text + " " + RelationCD6.Text + " " + RelationCE6.Text + " " + RelationCF6.Text + " " + RelationDE6.Text + " " + RelationDF6.Text + " " + RelationEF6.Text; }
break;
case 7:
{ strrel = RelationAB7.Text + " " + RelationAC7.Text + " " + RelationAD7.Text + " " + RelationAE7.Text + " " + RelationAF7.Text + " " + RelationAG7.Text + " " + RelationBC7.Text + " " + RelationBD7.Text + " " + RelationBE7.Text + " " + RelationBF7.Text + " " + RelationBG7.Text + " " + RelationCD7.Text + " " + RelationCE7.Text + " " + RelationCF7.Text + " " + RelationCG7.Text + " " + RelationDE7.Text + " " + RelationDF7.Text + " " + RelationDG7.Text + " " + RelationEF7.Text + " " + RelationEG7.Text + " " + RelationFG7.Text; }
break;
default:
{ strrel = ""; }
break;
}
GenerateTable.generatetable(relmatrix, strrel, rt, nodersnum);
Class1.generatePC(nodersnum, relmatrix);
int num = 0;
double maxincon = 0.0;
int mi = 0, mj = 0, mk = 0;
inconsistency = new Double[43] { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
for (int i = 0; i < nodersnum - 2; i++)
{
for (int k = i + 1; k < nodersnum - 1; k++)
{
for (int j = k + 1; j < nodersnum; j++)
{
if ((Convert.ToString(relmatrix.Tables["relTable"].Rows[i][j]) != " ") && (Convert.ToString(relmatrix.Tables["relTable"].Rows[i][k]) != " ") && (Convert.ToString(relmatrix.Tables["relTable"].Rows[k][j]) != " "))
{
Double a = Convert.ToDouble(relmatrix.Tables["relTable"].Rows[i][j]);//obtain value from the matrix
Double b = Convert.ToDouble(relmatrix.Tables["relTable"].Rows[i][k]);//obtain value from the matrix
//PROBLEM
Double c = Convert.ToDouble(relmatrix.Tables["relTable"].Rows[k][j]);//obtain value from the matrix
inconsistency[num] = (Class1.Min(System.Math.Abs(1 - a / (b * c)), System.Math.Abs(1 - (b * c) / a)));//calculate the inconsistency value and store in the inconsistency array
//Get the biggest inconsistency number
if (inconsistency[num] >= maxincon)
{
maxincon = inconsistency[num];
mi = i;
mj = j;
mk = k;
}
num++;
}
}
}
}
Class1.sort(inconsistency);//sort inconsistency array
while (inconsistency[0] > 0.3333333)
{
for (int i = 0; i < nodersnum - 2; i++)
{
for (int k = i + 1; k < nodersnum - 1; k++)
{
for (int j = k + 1; j < nodersnum; j++)
{
if ((Convert.ToString(relmatrix.Tables["relTable"].Rows[i][j]) != " ") && (Convert.ToString(relmatrix.Tables["relTable"].Rows[i][k]) != " ") && (Convert.ToString(relmatrix.Tables["relTable"].Rows[k][j]) != " "))
{
Double a = Convert.ToDouble(relmatrix.Tables["relTable"].Rows[i][j]);//obtain value from the matrix
Double b = Convert.ToDouble(relmatrix.Tables["relTable"].Rows[i][k]);//obtain value from the matrix
// PROBLEM
Double c = Convert.ToDouble(relmatrix.Tables["relTable"].Rows[k][j]);//obtain value from the matrix
if (inconsistency[0] == (Class1.Min(System.Math.Abs(1 - a / (b * c)), System.Math.Abs(1 - (b * c) / a))))//calculate the inconsistency value and store in the inconsistency array
{
if ((b * c) < a)
{
double A = (b * c) / ((a + b + c) * (a + b + c));
double B = (a + 2 * b * c) / (a + b + c);
double C = b * c - a;
double m = B * B - 4 * A * C;
if (m < 0)
{
Console.Write("error");
break;
}
else
{
double x1 = (-1 * B + System.Math.Sqrt(m)) / (2 * A);
double x2 = (-1 * B - Math.Sqrt(m)) / (2 * A);
if ((x1 > 0) && (x2 < 0))
{
b = (float)(b + (b * x1) / (a + b + c));
c = (float)(c + (c * x1) / (a + b + c));
a = (float)(a - (a * x1) / (a + b + c));
}
else if ((x1 < 0) && (x2 > 0))
{
b = (float)(b + (b * x2) / (a + b + c));
c = (float)(c + (c * x2) / (a + b + c));
a = (float)(a - (a * x2) / (a + b + c));
}
else if ((x1 > 0) && (x2 > 0))
{
double x = Class1.Min((float)x1, (float)x2);
b = (float)(b + (b * x) / (a + b + c));
c = (float)(c + (c * x) / (a + b + c));
a = (float)(a - (a * x) / (a + b + c));
}
else if ((x1 < 0) && (x2 < 0))
{
break;
}
}
}
else if ((b * c) > a)
{
double A = (b * c) / ((a + b + c) * (a + b + c));
double B = -1 * (a + 2 * b * c) / (a + b + c);
double C = b * c - a;
double m = B * B - 4 * A * C;
if (m < 0)
{
Console.Write("error");
break;
}
else
{
double x1 = (-1 * B + Math.Sqrt(m)) / (2 * A);
double x2 = (-1 * B - Math.Sqrt(m)) / (2 * A);
if ((x1 > 0) && (x2 < 0))
{
b = (float)(b - (b * x1) / (a + b + c));
c = (float)(c - (c * x1) / (a + b + c));
a = (float)(a + (a * x1) / (a + b + c));
}
else if ((x1 < 0) && (x2 > 0))
{
b = (float)(b - (b * x2) / (a + b + c));
c = (float)(c - (c * x2) / (a + b + c));
a = (float)(a + (a * x2) / (a + b + c));
}
else if ((x1 > 0) && (x2 > 0))
{
double x = Class1.Min((float)x1, (float)x2);
b = (float)(b - (b * x) / (a + b + c));
c = (float)(c - (c * x) / (a + b + c));
a = (float)(a + (a * x) / (a + b + c));
}
else if ((x1 < 0) && (x2 < 0))
{
break;
}
}
}
}
relmatrix.Tables["relTable"].Rows[i][j] = Convert.ToString(a);
relmatrix.Tables["relTable"].Rows[i][k] = Convert.ToString(b);
relmatrix.Tables["relTable"].Rows[k][j] = Convert.ToString(c);
}
}
}
}
num = 0;
maxincon = 0.0;
mi = 0;
mj = 0;
mk = 0;
for (int i = 0; i < nodersnum - 2; i++)
{
for (int k = i + 1; k < nodersnum - 1; k++)
{
for (int j = k + 1; j < nodersnum; j++)
{
if ((Convert.ToString(relmatrix.Tables["relTable"].Rows[i][j]) != " ") && (Convert.ToString(relmatrix.Tables["relTable"].Rows[i][k]) != " ") && (Convert.ToString(relmatrix.Tables["relTable"].Rows[k][j]) != " "))
{
Double a = Convert.ToDouble(relmatrix.Tables["relTable"].Rows[i][j]);//obtain value from the matrix
Double b = Convert.ToDouble(relmatrix.Tables["relTable"].Rows[i][k]);//obtain value from the matrix
Double c = Convert.ToDouble(relmatrix.Tables["relTable"].Rows[k][j]);//obtain value from the matrix
inconsistency[num] = (Class1.Min(System.Math.Abs(1 - a / (b * c)), System.Math.Abs(1 - (b * c) / a)));//calculate the inconsistency value and store in the inconsistency array
// Get the biggest inconsistency number
if (inconsistency[num] >= maxincon)
{
maxincon = inconsistency[num];
mi = i;
mj = j;
mk = k;
}
num++;
}
}
}
}
//sort inconsistency array
Class1.sort(inconsistency);
}
//Fill up the whole pairwise comparsion matrix, when row=col, the value =1, when row<col the vaule[col][row] is "1/"[col][row]
//nodersnum is how many nodes, row is the matrix row, col is column of matrix
for (int row = 0; row < nodersnum; row++)
{
for (int col = row; col < nodersnum; col++)
{
if (row < col)
{
//set the the value of lower matrix
relmatrix.Tables["relTable"].Rows[col][row] = "1/" + relmatrix.Tables["relTable"].Rows[row][col];
}
//set the value of diagnoal
else if (row == col)
{
relmatrix.Tables["relTable"].Rows[row][col] = "1";
}
}
}
//compute the weight of each element
Double[] rowproduct;
rowproduct = new Double[7] { 1, 1, 1, 1, 1, 1, 1 };
for (int i = 0; i < nodersnum; i++)
{
for (int j = 0; j < nodersnum; j++)
{
if (i >= j)
{
rowproduct[i] = rowproduct[i] / Convert.ToDouble(relmatrix.Tables["relTable"].Rows[j][i]);
}
else
{
rowproduct[i] = rowproduct[i] * Convert.ToDouble(relmatrix.Tables["relTable"].Rows[i][j]);
}
}
}
Double[] num1;
num1 = new Double[7] { 0, 0, 0, 0, 0, 0, 0 };
double numsum = 0;
//compute each row total number(power of node number)
for (int i = 0; i < nodersnum; i++)
{
num1[i] = Math.Pow(rowproduct[i], 1 / (double)nodersnum);
numsum = numsum + num1[i];
}
//transfer into the number of percentage
Double[] weight;
weight = new Double[7] { 0, 0, 0, 0, 0, 0, 0 };
for (int i = 0; i < nodersnum; i++)
{
weight[i] = (int)Math.Round(100 * num1[i] / numsum * 100) / 100f;
Console.WriteLine(weight[i]);
}
GridView2.DataSource = relmatrix;
GridView2.DataBind();
this.GridView2.Rows[mi].Cells[mj].BackColor = System.Drawing.Color.Red;
this.GridView2.Rows[mi].Cells[mk].BackColor = System.Drawing.Color.Red;
this.GridView2.Rows[mk].Cells[mj].BackColor = System.Drawing.Color.Red;
Label3.Text = "Maximum Inconsistency after Reduction: " + Convert.ToString(inconsistency[0]);
TextBox1.Text = Convert.ToString(weight[0]);
TextBox2.Text = Convert.ToString(weight[1]);
TextBox3.Text = Convert.ToString(weight[2]);
TextBox4.Text = Convert.ToString(weight[3]);
TextBox5.Text = Convert.ToString(weight[4]);
TextBox6.Text = Convert.ToString(weight[5]);
TextBox7.Text = Convert.ToString(weight[6]);
}
}
Looks like your matrix contains null values. You cannot cast them into Double.
Check like this:
if (relmatrix.Tables["relTable"].Rows[i][k] != DBNull.Value)
Or
if (relmatrix.Tables["relTable"].Rows[i][k] != null)
Use nullable double as shown below:
var mynull = DBNull.Value;
Double? c = Convert.IsDBNull(mynull) ? (double?)null : Convert.ToDouble(mynull);
In your case:
Double? c = Convert.IsDBNull(relmatrix.tables["relTable"].Rows[k][j]) ? (double?)null : Convert.ToDouble(relmatrix.tables["relTable"].Rows[k][j]);;
Alternatively using if-else:
if(Convert.IsDBNull(relmatrix.tables["relTable"].Rows[k][j]) == true)
{
Double? c = (double?)null;
}
else
{
Double? c = Convert.ToDouble(relmatrix.tables["relTable"].Rows[k][j]);
}

mipmapping producing wrong results

I'm trying to generate the mipmap of an image. The pixels are stored as a byte[] and the format is {r,g,b,a,r,g,b,a,r,g,b,a ... }
What this is trying to do is get each group of four pixels in the image and find the average of those four pixels, then put that into a new image.
The result of creating all the mipmaps for a sample texture are here: http://imgur.com/KdEEzAw
If there is a way to create the mipmaps without using my own algorithm, and without directx or anything (i'm not using the mipmaps for rendering, i'm saving them to a file) that would be good
public static byte[] mipmap(byte[] inPixels, int width, int height)
{
// add one to width and height incase they are 1
byte[] outPixels = new byte[((width + 1) / 2) * ((height + 1) / 2) * 4];
for (int y = 0; y < height; y += 2)
{
for (int x = 0; x < width; x += 2)
{
// get the four red values
int[] r = new int[4];
r[0] = (int)inPixels[x + y * width + 0]; // top left
r[1] = (int)inPixels[(x + 1) + y * width + 0]; // top right
r[2] = (int)inPixels[(x + 1) + (y + 1) * width + 0]; // bottom right
r[3] = (int)inPixels[x + (y + 1) * width + 0]; // bottom left
// get the four green values
int[] g = new int[4];
g[0] = (int)inPixels[x + y * width + 1]; // top left
g[1] = (int)inPixels[(x + 1) + y * width + 1]; // top right
g[2] = (int)inPixels[(x + 1) + (y + 1) * width + 1]; // bottom right
g[3] = (int)inPixels[x + (y + 1) * width + 1]; // bottom left
// get the four blue values
int[] b = new int[4];
b[0] = (int)inPixels[x + y * width + 2]; // top left
b[1] = (int)inPixels[(x + 1) + y * width + 2]; // top right
b[2] = (int)inPixels[(x + 1) + (y + 1) * width + 2]; // bottom right
b[3] = (int)inPixels[x + (y + 1) * width + 2]; // bottom left
// get the four alpha values
int[] a = new int[4];
a[0] = (int)inPixels[x + y * width + 3]; // top left
a[1] = (int)inPixels[(x + 1) + y * width + 3]; // top right
a[2] = (int)inPixels[(x + 1) + (y + 1) * width + 3]; // bottom right
a[3] = (int)inPixels[x + (y + 1) * width + 3]; // bottom left
// the index in the new image, we divide by 2 because the image is half the size of the original image
int index = (x + y * width) / 2;
outPixels[index + 0] = (byte)((r[0] + r[1] + r[2] + r[3]) / 4);
outPixels[index + 1] = (byte)((g[0] + g[1] + g[2] + g[3]) / 4);
outPixels[index + 2] = (byte)((b[0] + b[1] + b[2] + b[3]) / 4);
outPixels[index + 3] = (byte)((a[0] + a[1] + a[2] + a[3]) / 4);
}
}
return outPixels;
}
I think the problem lies here:
inPixels[x + y * width + 0]
normally this runs correct when one array element is one pixel, only one element is one channel of one pixel. So each pixel starts a (x + (y * width)) * 4 so it should be something like this:
inPixels[((x + y * width) * 4) + 0]
I wrote some code for optimalization maybe you get some extra ideas, but i did not test it:
public static byte[] mipmap(byte[] inPixels, int width, int height)
{
// add one to width and height incase they are 0
byte[] outPixels = new byte[((width / 2) * (height / 2)) * 4];
// the offsets of the neighbor pixels (with *4 for the channel)
// this will automatically select a channel of a pixel one line down.
int[] neighborOffsets = new int[] { 0 * 4, 1 * 4, width * 4, (width + 1) * 4 };
// an 'offset for output buffer'
int outputOffset = 0;
// an 'offset for input buffer'
int inputOffset = 0;
for (int y = 0; y < height / 2; y++)
{
for (int x = 0; x < width / 2; x++)
{
// calculate the average of each channel
for (int channelIndex = 0; channelIndex < 4; channelIndex++)
{
int totalValue = 0;
for (int offset = 0; offset < 4; offset++)
totalValue = (int)inPixels[inputOffset + neighborOffsets[offset] + channelIndex];
// write it to the ouput buffer and increase the offset.
outPixels[outputOffset++] = (byte)(totalValue / 4);
}
// set the input offset on the next pixel. The next pixel is current + 2.
inputOffset += 2 * 4; // *4 for the channelcount (*4 will be optimized by the compiler)
}
inputOffset += width * 4; // skip an extra line. *4 for the channelcount (*4 will be optimized by the compiler)
}
return outPixels;
}
There maybe some little mistakes in it, but it probably run 4 times as fast. Try to avoid redundancy on multiplications.

Categories