[英]C#: Declaring and Using XNA Vectors for Matrix Multiplication, et. al
我正在嘗試在 C# 中聲明和使用 XNA 向量進行矩陣乘法、求和等。
這些將用於圖像處理,使其比常規的 SetPixel 和 GetPixel 更快。 但是,我總是找不到有效的示例,並且我在網上嘗試了很多示例,但似乎我遺漏了一些東西。
任何幫助和示例代碼?
謝謝!
如果您擔心性能,那么您可以在unsafe
的環境中恢復編碼。
通過使用 unsafe 關鍵字標記類型、類型成員或語句塊,您可以在 scope 內的 memory 上使用指針類型並執行 C++ 樣式的指針操作,並且能夠在框架內執行此操作。 不安全的代碼可以比相應的安全實現運行得更快。
這是一個很好的簡短示例,來自 C# 4.0 一書簡而言之:
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;
}
}
( 來源)
除此之外,您還應該看看這個 SO Question
Verctors 只是 1 xn 矩陣。 創建一個矩陣 class,帶有求和和乘法的方法。
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);
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.