Replies: 16 comments 5 replies
-
小鲨 (程式) 答案public class Question2
{
public struct Matrix3x3
{
/// <summary>
/// Inner matrix
/// </summary>
private float[,] _matrix;
#region -- Properties --
public float m00
{
get { return _matrix[0, 0]; }
set { _matrix[0, 0] = value; }
}
public float m01
{
get { return _matrix[0, 1]; }
set { _matrix[0, 1] = value; }
}
public float m02
{
get { return _matrix[0, 2]; }
set { _matrix[0, 2] = value; }
}
public float m10
{
get { return _matrix[1, 0]; }
set { _matrix[1, 0] = value; }
}
public float m11
{
get { return _matrix[1, 1]; }
set { _matrix[1, 1] = value; }
}
public float m12
{
get { return _matrix[1, 2]; }
set { _matrix[1, 2] = value; }
}
public float m20
{
get { return _matrix[2, 0]; }
set { _matrix[2, 0] = value; }
}
public float m21
{
get { return _matrix[2, 1]; }
set { _matrix[2, 1] = value; }
}
public float m22
{
get { return _matrix[2, 2]; }
set { _matrix[2, 2] = value; }
}
#endregion
/// <summary>
/// Get/Set an element by 1D-Index
/// </summary>
public float this[int index]
{
get
{
return _matrix[index / 3, index % 3];
}
set
{
_matrix[index / 3, index % 3] = value;
}
}
/// <summary>
/// Get/Set an element by row and colomn
/// </summary>
public float this[int row, int col]
{
get
{
return _matrix[row, col];
}
set
{
_matrix[row, col] = value;
}
}
#region -- Constructors --
public Matrix3x3(params float[] array)
{
if (array == null || array.Length != 9)
{
throw new ArgumentException("Array length must be equal to 9");
}
_matrix = new float[3, 3];
for (int i = 0; i < array.Length; i++)
{
_matrix[i / 3, i % 3] = array[i];
}
}
public Matrix3x3(float m00, float m01, float m02, float m10, float m11, float m12, float m20, float m21, float m22)
{
_matrix = new float[,]
{
{m00, m01, m02 },
{m10, m11, m12 },
{m20, m21, m22 },
};
}
#endregion
/// <summary>
/// Parse matrix into 2D-Array
/// </summary>
/// <returns>Parsed 2D-Array</returns>
public float[,] To2DArray()
{
float[,] result = new float[3, 3];
for (int i = 0; i < result.Length; i++)
{
result[i / 3, i % 3] = _matrix[i / 3, i % 3];
}
return result;
}
}
} |
Beta Was this translation helpful? Give feedback.
-
JIA(程式新手) 我的答案public float this[int index]
{
get => _matrix[index / 3, index % 3];
set => _matrix[index / 3, index % 3] = value;
}
public float this[int row, int col]
{
get => _matrix[row, col];
set => _matrix[row, col] = value;
}
public float[,] To2DArray()
{
float[,] array = new float[3, 3];
for (int i = 0; i < 3; i++)
{
for (int j = 0; j < 3; j++)
{
array[i, j] = _matrix[i, j];
}
}
return array;
} |
Beta Was this translation helpful? Give feedback.
-
老蕭OLDShaw(程式) 我的答案public class Question2
{
public struct Matrix3x3
{
/// <summary>
/// Inner matrix
/// </summary>
private float[,] _matrix;
#region -- Properties --
public float m00
{
get { return _matrix[0, 0]; }
set { _matrix[0, 0] = value; }
}
public float m01
{
get { return _matrix[0, 1]; }
set { _matrix[0, 1] = value; }
}
public float m02
{
get { return _matrix[0, 2]; }
set { _matrix[0, 2] = value; }
}
public float m10
{
get { return _matrix[1, 0]; }
set { _matrix[1, 0] = value; }
}
public float m11
{
get { return _matrix[1, 1]; }
set { _matrix[1, 1] = value; }
}
public float m12
{
get { return _matrix[1, 2]; }
set { _matrix[1, 2] = value; }
}
public float m20
{
get { return _matrix[2, 0]; }
set { _matrix[2, 0] = value; }
}
public float m21
{
get { return _matrix[2, 1]; }
set { _matrix[2, 1] = value; }
}
public float m22
{
get { return _matrix[2, 2]; }
set { _matrix[2, 2] = value; }
}
#endregion
/// <summary>
/// Get/Set an element by 1D-Index
/// </summary>
public float this[int index]
{
get
{
return _matrix[index / 3, index % 3];
}
set
{
_matrix[index / 3, index % 3] = value;
}
}
/// <summary>
/// Get/Set an element by row and colomn
/// </summary>
public float this[int row, int col]
{
get
{
return _matrix[row, col];
}
set
{
_matrix[row, col] = value;
}
}
#region -- Constructors --
public Matrix3x3(params float[] array)
{
if (array == null || array.Length != 9)
{
throw new ArgumentException("Array length must be equal to 9");
}
_matrix = new float[3, 3];
for (int i = 0; i < array.Length; i++)
{
_matrix[i / 3, i % 3] = array[i];
}
}
public Matrix3x3(float m00, float m01, float m02, float m10, float m11, float m12, float m20, float m21, float m22)
{
_matrix = new float[,]
{
{m00, m01, m02 },
{m10, m11, m12 },
{m20, m21, m22 },
};
}
#endregion
/// <summary>
/// Parse matrix into 2D-Array
/// </summary>
/// <returns>Parsed 2D-Array</returns>
public float[,] To2DArray()
{
float[,] result = new float[3, 3];
for (int x = 0; x < 3; x++)
{
for (int y = 0; y < 3; y++)
{
result[x, y] = _matrix[x, y];
}
}
return result;
}
}
} 不知道To2DArray() Reference轉換用手填來省效能有沒有必要。 |
Beta Was this translation helpful? Give feedback.
-
Beta Was this translation helpful? Give feedback.
-
Cliff Lee CL (程式、專案管理) 我的答案public class Question2
{
public struct Matrix3x3
{
/// <summary>
/// Inner matrix
/// </summary>
private float[,] _matrix;
#region -- Properties --
public float m00
{
get { return _matrix[0, 0]; }
set { _matrix[0, 0] = value; }
}
public float m01
{
get { return _matrix[0, 1]; }
set { _matrix[0, 1] = value; }
}
public float m02
{
get { return _matrix[0, 2]; }
set { _matrix[0, 2] = value; }
}
public float m10
{
get { return _matrix[1, 0]; }
set { _matrix[1, 0] = value; }
}
public float m11
{
get { return _matrix[1, 1]; }
set { _matrix[1, 1] = value; }
}
public float m12
{
get { return _matrix[1, 2]; }
set { _matrix[1, 2] = value; }
}
public float m20
{
get { return _matrix[2, 0]; }
set { _matrix[2, 0] = value; }
}
public float m21
{
get { return _matrix[2, 1]; }
set { _matrix[2, 1] = value; }
}
public float m22
{
get { return _matrix[2, 2]; }
set { _matrix[2, 2] = value; }
}
#endregion
/// <summary>
/// Get/Set an element by 1D-Index
/// </summary>
public float this[int index]
{
get => _matrix[Get1DRow(index), Get1DCol(index)];
set => _matrix[Get1DRow(index), Get1DCol(index)] = value;
}
int Get1DRow(int index)
{
return index / _matrix.GetLength(0);
}
int Get1DCol(int index)
{
return index % _matrix.GetLength(1);
}
/// <summary>
/// Get/Set an element by row and colomn
/// </summary>
public float this[int row, int col]
{
get => _matrix[row, col];
set => _matrix[row, col] = value;
}
#region -- Constructors --
public Matrix3x3(params float[] array)
{
if (array == null || array.Length != 9)
{
throw new ArgumentException("Array length must be equal to 9");
}
_matrix = new float[3, 3];
for (int i = 0; i < array.Length; i++)
{
_matrix[i / 3, i % 3] = array[i];
}
}
public Matrix3x3(float m00, float m01, float m02, float m10, float m11, float m12, float m20, float m21, float m22)
{
_matrix = new float[,]
{
{m00, m01, m02 },
{m10, m11, m12 },
{m20, m21, m22 },
};
}
#endregion
/// <summary>
/// Parse matrix into 2D-Array
/// </summary>
/// <returns>Parsed 2D-Array</returns>
public float[,] To2DArray()
{
// Deep copy _matrix to avoid changing the original matrix
var result = new float[_matrix.GetLength(0), _matrix.GetLength(1)];
for (int i = 0; i < _matrix.GetLength(0); i++)
{
for (int j = 0; j < _matrix.GetLength(1); j++)
{
result[i, j] = _matrix[i, j];
}
}
return result;
}
}
} |
Beta Was this translation helpful? Give feedback.
-
答案public float this[int index]
{
get
{
int x = index / 3;
int y = index % 3;
return _matrix[x, y];
}
set
{
int x = index / 3;
int y = index % 3;
_matrix[x, y] = value;
}
}
public float this[int row, int col]
{
get
{
return _matrix[row, col];
}
set
{
_matrix[row, col] = value;
}
}
public float[,] To2DArray()
{
float[,] array = new float[3, 3];
Array.Copy(_matrix, array, array.Length);
return array;
} |
Beta Was this translation helpful? Give feedback.
-
Tina( Unity程式 | 佛系劇本 ) 我的答案using System;
using System.Linq.Expressions;
namespace PG0004.Questions
{
public class Question2
{
public struct Matrix3x3
{
/// <summary>
/// Inner matrix
/// </summary>
private float[,] _matrix;
#region -- Properties --
public float m00
{
get { return _matrix[0, 0]; }
set { _matrix[0, 0] = value; }
}
public float m01
{
get { return _matrix[0, 1]; }
set { _matrix[0, 1] = value; }
}
public float m02
{
get { return _matrix[0, 2]; }
set { _matrix[0, 2] = value; }
}
public float m10
{
get { return _matrix[1, 0]; }
set { _matrix[1, 0] = value; }
}
public float m11
{
get { return _matrix[1, 1]; }
set { _matrix[1, 1] = value; }
}
public float m12
{
get { return _matrix[1, 2]; }
set { _matrix[1, 2] = value; }
}
public float m20
{
get { return _matrix[2, 0]; }
set { _matrix[2, 0] = value; }
}
public float m21
{
get { return _matrix[2, 1]; }
set { _matrix[2, 1] = value; }
}
public float m22
{
get { return _matrix[2, 2]; }
set { _matrix[2, 2] = value; }
}
#endregion
/// <summary>
/// Get/Set an element by 1D-Index
/// </summary>
public float this[int index]
{
get
{
switch (index)
{
case 0: return _matrix[0, 0];
case 1: return _matrix[0, 1];
case 2: return _matrix[0, 2];
case 3: return _matrix[1, 0];
case 4: return _matrix[1, 1];
case 5: return _matrix[1, 2];
case 6: return _matrix[2, 0];
case 7: return _matrix[2, 1];
case 8: return _matrix[2, 2];
default: return default;
}
}
set
{
switch (index)
{
case 0: _matrix[0, 0] = value; break;
case 1: _matrix[0, 1] = value; break;
case 2: _matrix[0, 2] = value; break;
case 3: _matrix[1, 0] = value; break;
case 4: _matrix[1, 1] = value; break;
case 5: _matrix[1, 2] = value; break;
case 6: _matrix[2, 0] = value; break;
case 7: _matrix[2, 1] = value; break;
case 8: _matrix[2, 2] = value; break;
}
}
}
/// <summary>
/// Get/Set an element by row and colomn
/// </summary>
public float this[int row, int col]
{
get
{
return _matrix[row, col];
}
set
{
_matrix[row, col] = value;
}
}
#region -- Constructors --
public Matrix3x3(params float[] array)
{
if (array == null || array.Length != 9)
{
throw new ArgumentException("Array length must be equal to 9");
}
_matrix = new float[3, 3];
for (int i = 0; i < array.Length; i++)
{
_matrix[i / 3, i % 3] = array[i];
}
}
public Matrix3x3(float m00, float m01, float m02, float m10, float m11, float m12, float m20, float m21, float m22)
{
_matrix = new float[,]
{
{m00, m01, m02 },
{m10, m11, m12 },
{m20, m21, m22 },
};
}
#endregion
/// <summary>
/// Parse matrix into 2D-Array
/// </summary>
/// <returns>Parsed 2D-Array</returns>
public float[,] To2DArray()
{
float[,] temp = new float[3,3];
Array.Copy(_matrix, temp, _matrix.Length);
return temp;
}
}
}
} 然後看了解答後,才知道原來 |
Beta Was this translation helpful? Give feedback.
-
七七(程式) 我的答案public class Question2
{
public struct Matrix3x3
{
/// <summary>
/// Inner matrix
/// </summary>
private float[,] _matrix;
#region -- Properties --
public float m00
{
get { return _matrix[0, 0]; }
set { _matrix[0, 0] = value; }
}
public float m01
{
get { return _matrix[0, 1]; }
set { _matrix[0, 1] = value; }
}
public float m02
{
get { return _matrix[0, 2]; }
set { _matrix[0, 2] = value; }
}
public float m10
{
get { return _matrix[1, 0]; }
set { _matrix[1, 0] = value; }
}
public float m11
{
get { return _matrix[1, 1]; }
set { _matrix[1, 1] = value; }
}
public float m12
{
get { return _matrix[1, 2]; }
set { _matrix[1, 2] = value; }
}
public float m20
{
get { return _matrix[2, 0]; }
set { _matrix[2, 0] = value; }
}
public float m21
{
get { return _matrix[2, 1]; }
set { _matrix[2, 1] = value; }
}
public float m22
{
get { return _matrix[2, 2]; }
set { _matrix[2, 2] = value; }
}
#endregion
/// <summary>
/// Get/Set an element by 1D-Index
/// </summary>
public float this[int index]
{
get
{
int x = index / 3;
int y = index % 3;
return _matrix[x, y];
}
set
{
int x = index / 3;
int y = index % 3;
_matrix[x, y] = value;
}
}
/// <summary>
/// Get/Set an element by row and colomn
/// </summary>
public float this[int row, int col]
{
get { return _matrix[row, col]; }
set { _matrix[row, col] = value; }
}
#region -- Constructors --
public Matrix3x3(params float[] array)
{
if (array == null || array.Length != 9)
{
throw new ArgumentException("Array length must be equal to 9");
}
_matrix = new float[3, 3];
for (int i = 0; i < array.Length; i++)
{
_matrix[i / 3, i % 3] = array[i];
}
}
public Matrix3x3(
float m00,
float m01,
float m02,
float m10,
float m11,
float m12,
float m20,
float m21,
float m22
)
{
_matrix = new float[,]
{
{ m00, m01, m02 },
{ m10, m11, m12 },
{ m20, m21, m22 },
};
}
#endregion
/// <summary>
/// Parse matrix into 2D-Array
/// </summary>
/// <returns>Parsed 2D-Array</returns>
public float[,] To2DArray()
{
float[,] array = new float[3, 3];
Array.Copy(_matrix, array, array.Length);
return array;
}
} (¯﹃¯) |
Beta Was this translation helpful? Give feedback.
-
Syuan 我的答案public class Question2
{
public struct Matrix3x3
{
private float[,] _matrix;
#region -- Properties --
public float m00
{
get { return _matrix[0, 0]; }
set { _matrix[0, 0] = value; }
}
public float m01
{
get { return _matrix[0, 1]; }
set { _matrix[0, 1] = value; }
}
public float m02
{
get { return _matrix[0, 2]; }
set { _matrix[0, 2] = value; }
}
public float m10
{
get { return _matrix[1, 0]; }
set { _matrix[1, 0] = value; }
}
public float m11
{
get { return _matrix[1, 1]; }
set { _matrix[1, 1] = value; }
}
public float m12
{
get { return _matrix[1, 2]; }
set { _matrix[1, 2] = value; }
}
public float m20
{
get { return _matrix[2, 0]; }
set { _matrix[2, 0] = value; }
}
public float m21
{
get { return _matrix[2, 1]; }
set { _matrix[2, 1] = value; }
}
public float m22
{
get { return _matrix[2, 2]; }
set { _matrix[2, 2] = value; }
}
#endregion
public float this[int index]
{
get
{
return _matrix[index / 3, index % 3];
}
set
{
_matrix[index / 3, index % 3] = value;
}
}
public float this[int row, int col]
{
get
{
return _matrix[row, col];
}
set
{
_matrix[row, col] = value;
}
}
#region -- Constructors --
public Matrix3x3(params float[] array)
{
if (array == null || array.Length != 9)
{
throw new ArgumentException("Array length must be equal to 9");
}
_matrix = new float[3, 3];
for (int i = 0; i < array.Length; i++)
{
_matrix[i / 3, i % 3] = array[i];
}
}
public Matrix3x3(float m00, float m01, float m02, float m10, float m11, float m12, float m20, float m21, float m22)
{
_matrix = new float[,]
{
{m00, m01, m02 },
{m10, m11, m12 },
{m20, m21, m22 },
};
}
#endregion
public float[,] To2DArray()
{
float[,] copy_data = new float[3, 3];
for(int i = 0; i < 9; ++i)
{
copy_data[i / 3, i % 3] = _matrix[i / 3, i % 3];
}
return copy_data;
}
}
} |
Beta Was this translation helpful? Give feedback.
-
罐頭(程式) 我的答案ヾ(•ω•`)onamespace PG0004.Questions
{
public class Question2
{
public struct Matrix3x3
{
/// <summary>
/// Inner matrix
/// </summary>
private float[,] _matrix;
#region -- Properties --
public float m00
{
get { return _matrix[0, 0]; }
set { _matrix[0, 0] = value; }
}
public float m01
{
get { return _matrix[0, 1]; }
set { _matrix[0, 1] = value; }
}
public float m02
{
get { return _matrix[0, 2]; }
set { _matrix[0, 2] = value; }
}
public float m10
{
get { return _matrix[1, 0]; }
set { _matrix[1, 0] = value; }
}
public float m11
{
get { return _matrix[1, 1]; }
set { _matrix[1, 1] = value; }
}
public float m12
{
get { return _matrix[1, 2]; }
set { _matrix[1, 2] = value; }
}
public float m20
{
get { return _matrix[2, 0]; }
set { _matrix[2, 0] = value; }
}
public float m21
{
get { return _matrix[2, 1]; }
set { _matrix[2, 1] = value; }
}
public float m22
{
get { return _matrix[2, 2]; }
set { _matrix[2, 2] = value; }
}
#endregion
/// <summary>
/// Get/Set an element by 1D-Index
/// </summary>
public float this[int index]
{
get => _matrix[index / _matrix.GetLength(0), index % _matrix.GetLength(0)];
set => _matrix[index / _matrix.GetLength(0), index % _matrix.GetLength(0)] = value;
}
/// <summary>
/// Get/Set an element by row and colomn
/// </summary>
public float this[int row, int col]
{
get => _matrix[row, col];
set => _matrix[row, col] = value;
}
#region -- Constructors --
public Matrix3x3(params float[] array)
{
if (array == null || array.Length != 9)
{
throw new ArgumentException("Array length must be equal to 9");
}
_matrix = new float[3, 3];
for (int i = 0; i < array.Length; i++)
{
_matrix[i / 3, i % 3] = array[i];
}
}
public Matrix3x3(float m00, float m01, float m02, float m10, float m11, float m12, float m20, float m21,
float m22)
{
_matrix = new float[,]
{
{ m00, m01, m02 },
{ m10, m11, m12 },
{ m20, m21, m22 },
};
}
#endregion
/// <summary>
/// Parse matrix into 2D-Array
/// </summary>
/// <returns>Parsed 2D-Array</returns>
public float[,] To2DArray()
{
float[,] array = new float[_matrix.GetLength(0), _matrix.GetLength(1)];
Array.Copy(_matrix, array, array.Length);
return array;
}
}
}
} 不知道有沒有可以寫更好的地方,但寫得很快樂O(∩_∩)O |
Beta Was this translation helpful? Give feedback.
-
肥羊(程式) 我的答案public class Question2
{
public struct Matrix3x3
{
/// <summary>
/// Inner matrix
/// </summary>
private float[,] _matrix;
#region -- Properties --
public float m00
{
get { return _matrix[0, 0]; }
set { _matrix[0, 0] = value; }
}
public float m01
{
get { return _matrix[0, 1]; }
set { _matrix[0, 1] = value; }
}
public float m02
{
get { return _matrix[0, 2]; }
set { _matrix[0, 2] = value; }
}
public float m10
{
get { return _matrix[1, 0]; }
set { _matrix[1, 0] = value; }
}
public float m11
{
get { return _matrix[1, 1]; }
set { _matrix[1, 1] = value; }
}
public float m12
{
get { return _matrix[1, 2]; }
set { _matrix[1, 2] = value; }
}
public float m20
{
get { return _matrix[2, 0]; }
set { _matrix[2, 0] = value; }
}
public float m21
{
get { return _matrix[2, 1]; }
set { _matrix[2, 1] = value; }
}
public float m22
{
get { return _matrix[2, 2]; }
set { _matrix[2, 2] = value; }
}
#endregion
/// <summary>
/// Get/Set an element by 1D-Index
/// </summary>
public float this[int index]
{
get
{
int x = index / 3; //index0-0,index1-1,index2-2,index3-1,index4-1,index6-2,index7-2
int y = index % 3; //index0-0,index1-1,index2-2,index3-0,index4-1,index6-0,index7-1
return _matrix[x, y];
}
set
{
int x = index / 3;
int y = index % 3;
_matrix[x, y] = value;
}
}
/// <summary>
/// Get/Set an element by row and colomn
/// </summary>
public float this[int row, int col]
{
get
{
return _matrix[row, col];
}
set
{
_matrix[row, col] = value;
}
}
#region -- Constructors --
public Matrix3x3(params float[] array)
{
if (array == null || array.Length != 9)
{
throw new ArgumentException("Array length must be equal to 9");
}
_matrix = new float[3, 3];
for (int i = 0; i < array.Length; i++)
{
_matrix[i / 3, i % 3] = array[i];
}
}
public Matrix3x3(float m00, float m01, float m02, float m10, float m11, float m12, float m20, float m21, float m22)
{
_matrix = new float[,]
{
{m00, m01, m02 },
{m10, m11, m12 },
{m20, m21, m22 },
};
}
#endregion
/// <summary>
/// Parse matrix into 2D-Array
/// </summary>
/// <returns>Parsed 2D-Array</returns>
public float[,] To2DArray()
{
float[,] newArray = new float[3, 3];
Array.Copy(_matrix, newArray, newArray.Length);
return newArray;
}
}
} |
Beta Was this translation helpful? Give feedback.
-
Snoweve (程式) 我的答案
<<反饋>> |
Beta Was this translation helpful? Give feedback.
-
TWEdward(新人/程式/企劃) 我的答案public class Question2
{
public struct Matrix3x3
{
/// <summary>
/// Inner matrix
/// </summary>
private float[,] _matrix;
#region -- Properties --
public float m00
{
get { return _matrix[0, 0]; }
set { _matrix[0, 0] = value; }
}
public float m01
{
get { return _matrix[0, 1]; }
set { _matrix[0, 1] = value; }
}
public float m02
{
get { return _matrix[0, 2]; }
set { _matrix[0, 2] = value; }
}
public float m10
{
get { return _matrix[1, 0]; }
set { _matrix[1, 0] = value; }
}
public float m11
{
get { return _matrix[1, 1]; }
set { _matrix[1, 1] = value; }
}
public float m12
{
get { return _matrix[1, 2]; }
set { _matrix[1, 2] = value; }
}
public float m20
{
get { return _matrix[2, 0]; }
set { _matrix[2, 0] = value; }
}
public float m21
{
get { return _matrix[2, 1]; }
set { _matrix[2, 1] = value; }
}
public float m22
{
get { return _matrix[2, 2]; }
set { _matrix[2, 2] = value; }
}
#endregion
/// <summary>
/// Get/Set an element by 1D-Index
/// </summary>
public float this[int index]
{
get
{
if (index < 0 || index >= 9)
return 0;
return _matrix[index/3, index%3];
}
set
{
if(index >= 0 && index < 9)
_matrix[index/3,index%3]=value;
}
}
/// <summary>
/// Get/Set an element by row and colomn
/// </summary>
public float this[int row, int col]
{
get
{
if (row < 0 || row >= 3 || col < 0 || col >= 3)
return 0;
return _matrix[row,col];
}
set
{
if(row >= 0 && row < 3 && col >= 0 || col < 3)
_matrix[row,col] = value;
}
}
#region -- Constructors --
public Matrix3x3(params float[] array)
{
if (array == null || array.Length != 9)
{
throw new ArgumentException("Array length must be equal to 9");
}
_matrix = new float[3, 3];
for (int i = 0; i < array.Length; i++)
{
_matrix[i / 3, i % 3] = array[i];
}
}
public Matrix3x3(float m00, float m01, float m02, float m10, float m11, float m12, float m20, float m21, float m22)
{
_matrix = new float[,]
{
{m00, m01, m02 },
{m10, m11, m12 },
{m20, m21, m22 },
};
}
#endregion
/// <summary>
/// Parse matrix into 2D-Array
/// </summary>
/// <returns>Parsed 2D-Array</returns>
public float[,] To2DArray()
{
float[,] arrayCopy = new float[3, 3];
Array.Copy(_matrix,arrayCopy,_matrix.Length);
return arrayCopy;
}
}
} 解題心得感想感覺這題我寫的沒有很好,只能先求能跑能動能通過單元測試就好,透過IDE的提示功能有看到throw......Exception()等等選項可以選,但是因為還沒有很了解,我會再研究看看!! |
Beta Was this translation helpful? Give feedback.
-
肉鬆(程式 / 音樂) 我的答案 public struct Matrix3x3
{
public float this[int index]
{
get
{
int tempRow = index / 3;
int tempCol = index % 3;
return _matrix[tempRow, tempCol];
}
set
{
int tempRow = index / 3;
int tempCol = index % 3;
_matrix[tempRow, tempCol] = value;
}
}
public float this[int row, int col]
{
get
{
return _matrix[row, col];
}
}
public float[,] To2DArray()
{
float[,] outputArr = new float[3, 3];
for(int i = 0; i < outputArr.Length; i++)
{
outputArr[i / 3, i % 3] = _matrix[i / 3, i % 3];
}
return outputArr;
}
} 課程回饋感謝勞師的講解! |
Beta Was this translation helpful? Give feedback.
-
答案 public class Question2
{
public struct Matrix3x3
{
/// <summary>
/// Inner matrix
/// </summary>
private float[,] _matrix;
#region -- Properties --
public float m00
{
get { return _matrix[0, 0]; }
set { _matrix[0, 0] = value; }
}
public float m01
{
get { return _matrix[0, 1]; }
set { _matrix[0, 1] = value; }
}
public float m02
{
get { return _matrix[0, 2]; }
set { _matrix[0, 2] = value; }
}
public float m10
{
get { return _matrix[1, 0]; }
set { _matrix[1, 0] = value; }
}
public float m11
{
get { return _matrix[1, 1]; }
set { _matrix[1, 1] = value; }
}
public float m12
{
get { return _matrix[1, 2]; }
set { _matrix[1, 2] = value; }
}
public float m20
{
get { return _matrix[2, 0]; }
set { _matrix[2, 0] = value; }
}
public float m21
{
get { return _matrix[2, 1]; }
set { _matrix[2, 1] = value; }
}
public float m22
{
get { return _matrix[2, 2]; }
set { _matrix[2, 2] = value; }
}
#endregion
/// <summary>
/// Get/Set an element by 1D-Index
/// </summary>
public float this[int index]
{
get
{
int r = index / 3;
int c = index % 3;
return _matrix[r, c];
}
set
{
int r = index / 3;
int c = index % 3;
_matrix[r, c] = value;
}
}
/// <summary>
/// Get/Set an element by row and colomn
/// </summary>
public float this[int row, int col]
{
get
{
return _matrix[row, col];
}
set
{
_matrix[row, col] = value;
}
}
#region -- Constructors --
public Matrix3x3(params float[] array)
{
if (array == null || array.Length != 9)
{
throw new ArgumentException("Array length must be equal to 9");
}
_matrix = new float[3, 3];
for (int i = 0; i < array.Length; i++)
{
_matrix[i / 3, i % 3] = array[i];
}
}
public Matrix3x3(float m00, float m01, float m02, float m10, float m11, float m12, float m20, float m21, float m22)
{
_matrix = new float[,]
{
{m00, m01, m02 },
{m10, m11, m12 },
{m20, m21, m22 },
};
}
#endregion
/// <summary>
/// Parse matrix into 2D-Array
/// </summary>
/// <returns>Parsed 2D-Array</returns>
public float[,] To2DArray()
{
var NewArray = new float[3, 3];
Array.Copy(_matrix, NewArray, 9);
return NewArray;
}
}
} 回饋
在做這題的時候不太理解題目的目標,能理解數值與參照的不同,但要做什麼不太懂,看了答案,要使用 |
Beta Was this translation helpful? Give feedback.
-
極光(程式) 我的答案public class Question2
{
public struct Matrix3x3
{
/// <summary>
/// Inner matrix
/// </summary>
private float[,] _matrix;
#region -- Properties --
public float m00
{
get { return _matrix[0, 0]; }
set { _matrix[0, 0] = value; }
}
public float m01
{
get { return _matrix[0, 1]; }
set { _matrix[0, 1] = value; }
}
public float m02
{
get { return _matrix[0, 2]; }
set { _matrix[0, 2] = value; }
}
public float m10
{
get { return _matrix[1, 0]; }
set { _matrix[1, 0] = value; }
}
public float m11
{
get { return _matrix[1, 1]; }
set { _matrix[1, 1] = value; }
}
public float m12
{
get { return _matrix[1, 2]; }
set { _matrix[1, 2] = value; }
}
public float m20
{
get { return _matrix[2, 0]; }
set { _matrix[2, 0] = value; }
}
public float m21
{
get { return _matrix[2, 1]; }
set { _matrix[2, 1] = value; }
}
public float m22
{
get { return _matrix[2, 2]; }
set { _matrix[2, 2] = value; }
}
#endregion
/// <summary>
/// Get/Set an element by 1D-Index
/// </summary>
public float this[int index]
{
get
{
int row = index / 3;
int col = index % 3;
return _matrix[row, col];
}
set
{
int row = index / 3;
int col = index % 3;
_matrix[row, col] = value;
}
}
/// <summary>
/// Get/Set an element by row and colomn
/// </summary>
public float this[int row, int col]
{
get
{
return _matrix[row, col];
}
set
{
_matrix[row, col] = value;
}
}
#region -- Constructors --
public Matrix3x3(params float[] array)
{
if (array == null || array.Length != 9)
{
throw new ArgumentException("Array length must be equal to 9");
}
_matrix = new float[3, 3];
for (int i = 0; i < array.Length; i++)
{
_matrix[i / 3, i % 3] = array[i];
}
}
public Matrix3x3(float m00, float m01, float m02, float m10, float m11, float m12, float m20, float m21, float m22)
{
_matrix = new float[,]
{
{m00, m01, m02 },
{m10, m11, m12 },
{m20, m21, m22 },
};
}
#endregion
/// <summary>
/// Parse matrix into 2D-Array
/// </summary>
/// <returns>Parsed 2D-Array</returns>
public float[,] To2DArray()
{
float[,] newArr = new float[3, 3];
for(int x=0; x<3; x++)
{
for(int y=0;y<3;y++)
{
newArr[x, y] = _matrix[x, y];
}
}
return newArr;
}
}
} |
Beta Was this translation helpful? Give feedback.
-
Introduction
根據題目實作3x3矩陣(Matrix)結構的下列方法:
Answer
答案
最後的To2DArray,也能建立一個新的Array,然後用迴圈一個一個塞值也可以,
答案中的寫法是C#的System Lib提供的工具,寫起來挺方便的。
Beta Was this translation helpful? Give feedback.
All reactions