Unity and C# Study Notes - Computer Graphics Basics

Vector

public override string ToString(){
    return string.Format("(0:F2),(1:F2),(2:F2)", x, y, z);
}

Matrix

using System;
using System.Text;
using System.Runtime.InteropServices;

[StructLayout(LayoutKind.Sequential)]
namespace My{
struct Matrix4x4{
    [MarshalAs(UnmanagedType.ByValArray, SizeConst = 4*4)]
    private float[,] m; // = null
    public Matrix4x4(float [,] m){
        this.m = m;
    }
    public float this[int row, int col]{
        get{
            if (row < 0 || row >= 4)
				throw new IndexOutOfRangeException();
			if (col < 0 || col >= 4)
				throw new IndexOutOfRangeException();
			
            return m[row, col];
        }
        set{
            if (row < 0 || row >= 4)
				throw new IndexOutOfRangeException();
			if (col < 0 || col >= 4)
				throw new IndexOutOfRangeException();

			m[row, col] = value;
        }
    }
    public Matrix4x4 transpose{
        get{
            Matrix4x4 mRet = new Matrix4x4(new float [4,4]);
            for(int i = 0; i < 4; i++){
                for(int j = 0; j < 4; j++){
                    mRet[i, j] = this[j, i];
                }
            }
            return mRet;
        }
    }
    public static Matrix4x4 operator *(Matrix4x4 a, Matrix4x4 b){
        Matrix4x4 c = new Matrix4x4(new float [4,4]);
        for(int i = 0; i < 4; i++){
            for(int j = 0; j < 4; j++){
                c[i, j] = a[i, 0] * b[0, j] +
                          a[i, 1] * b[1, j] +
                          a[i, 2] * b[2, j] +
                          a[i, 3] * b[3, j];
            }
        }
        return c;
    }
    public override string ToString(){
        StringBuilder sb = new StringBuilder();
        for(int i = 0; i < 4; i++){
            for(int j = 0; j < 4 ;j++){
                sb.AppendFormat("{0:F2} ", m[i, j]);
            }
            sb.Append("\n");
        }
        return sb.ToString();
    }
}
} // namespace

Projection Matrix