I am trying to declare and use XNA Vectors for Matrix Multiplication, Summation, etc. in C#.
Those will be used for Image processing to make it faster than regular SetPixel and GetPixel. However, I am always failing to find a working example and I tried many examples online but it seems I am missing something.
Any help and sample code?
Thanks!
If you are worried about Performance then you can revert to coding in unsafe
context.
By marking a type, type member, or statement block with the unsafe keyword, you're permitted to use pointer types and perform C++ style pointer operations on memory within that scope, and to be able to do this within the managed execution framework. Unsafe code can run faster than a corresponding safe implementation.
Here is a nice, short example that comes from the book C# 4.0 in a Nutshell:
unsafe void BlueFilter (int[,] bitmap)
{
int length = bitmap.Length;
fixed (int* b=bitmap)
{
int* p=b;
for (int i=0, i<length; i++)
*p++ &= 0xFF;
}
}
( Source )
Apart from that you should also take a look at this SO Question
Verctors are just 1 xn matrices. Create a Matrix class, with methods for summation and multiplication.
public class Matrix
{
private int[,] m_array;
public Matrix(int m, int n)
{
m_array = new int[m, n];
}
public Matrix(int[,] other)
{
m_array = other;
}
public Matrix Mult(Matrix other)
{
if (this.m_array.GetLength(1) != other.m_array.GetLength(0))
return null;
int[,] res = new int[this.m_array.GetLength(0), other.m_array.GetLength(1)];
for (int i = 0; i < this.m_array.GetLength(0); ++i)
for (int j = 0; j < other.m_array.GetLength(1); ++j)
{
int s = 0;
for (int k = 0; k < this.m_array.GetLength(1); ++k)
s += this.m_array[i, k] * other.m_array[k, j];
res[i, j] = s;
}
return new Matrix(res);
}
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.