-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMatrix.cs
78 lines (72 loc) · 2.32 KB
/
Matrix.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
namespace _3D_Renderer
{
readonly struct Matrix
{
public Arr2D<double> Values { get; }
public Matrix(Arr2D<double> Values)
{
this.Values = Values;
}
public int Width()
{
return Values.Width;
}
public int Height()
{
return Values.Height;
}
public Matrix Transpose()
{
Matrix Transpose = new Matrix(new Arr2D<double>(Height(), Width()));
for (int x = 0; x < Height(); x++)
{
for (int y = 0; y < Width(); y++)
{
Transpose.Values[y, x] = Values[x, y];
}
}
return Transpose;
}
public Vec3D ToVec3D()
{
return new Vec3D(Values[0, 0], Values[1, 0], Values[2, 0]);
}
public Vec2D ToVec2D()
{
return new Vec2D(Values[0, 0], Values[1, 0]);
}
public static Matrix Multiply(Matrix MatrixA, Matrix MatrixB)
{
Matrix Product = new Matrix(new Arr2D<double>(MatrixB.Width(), MatrixB.Height()));
for (int ProductX = 0; ProductX < Product.Height(); ProductX++)
{
for (int ProductY = 0; ProductY < Product.Width(); ProductY++)
{
double Value = 0;
int MatrixAX = 0;
int MatrixBY = 0;
while (MatrixAX < MatrixA.Height() && MatrixBY < MatrixB.Width())
{
Value += MatrixA.Values[ProductY, MatrixAX] * MatrixB.Values[MatrixBY, ProductX];
MatrixAX++;
MatrixBY++;
}
Product.Values[ProductY, ProductX] = Value;
}
}
return Product;
}
public static Matrix operator +(Matrix MatrixA, Matrix MatrixB)
{
Matrix Sum = new Matrix(new Arr2D<double>(MatrixB.Width(), MatrixB.Height()));
for (int x = 0; x < Sum.Height(); x++)
{
for (int y = 0; y < Sum.Width(); y++)
{
Sum.Values[x, y] = MatrixA.Values[x, y] + MatrixB.Values[x, y];
}
}
return Sum;
}
}
}