コピペして使うがよい。
using System;
class MatrixLib {
/// <summary>
/// 3Dの[拡縮→回転→平行移動]変換行列を作る。
/// </summary>
/// <param name="xScale">X軸方向倍率</param>
/// <param name="yScale">Y軸方向倍率</param>
/// <param name="zScale">Z軸方向倍率</param>
/// <param name="yawRotate">ヨー角度(360.0で一周)</param>
/// <param name="pitchRotate">ピッチ角度(360.0で一周)</param>
/// <param name="rollRotate">ロール角度(360.0で一周)</param>
/// <param name="xTranslate">X軸方向移動量</param>
/// <param name="yTranslate">Y軸方向移動量</param>
/// <param name="zTranslate">Z軸方向移動量</param>
/// <returns></returns>
public static D3DMatrix Transform3D(double xScale, double yScale, double zScale, double yawRotate, double pitchRotate, double rollRotate, double xTranslate, double yTranslate, double zTranslate)
{
D3DMatrix m = D3DMatrix.Identity;
double cosYaw = Cos(yawRotate), sinYaw = Sin(yawRotate);
double cosPitch = Cos(pitchRotate), sinPitch = Sin(pitchRotate);
double cosRoll = Cos(rollRotate), sinRoll = Sin(rollRotate);
m.M11 = (float)(xScale * (cosRoll * cosYaw + sinRoll * sinPitch * sinYaw));
m.M12 = (float)(xScale * sinRoll * cosPitch);
m.M13 = (float)(xScale * (sinRoll * sinPitch * cosYaw - cosRoll * sinYaw));
m.M21 = (float)(yScale * (yScale * cosRoll * sinPitch * sinYaw - sinRoll * cosYaw));
m.M22 = (float)(yScale * cosRoll * cosPitch);
m.M23 = (float)(yScale * (sinRoll * sinYaw + cosRoll * sinPitch * cosYaw));
m.M31 = (float)(zScale * cosPitch * sinYaw);
m.M32 = (float)(zScale * -sinPitch);
m.M33 = (float)(zScale * cosPitch * cosYaw);
m.M41 = (float)xTranslate;
m.M42 = (float)yTranslate;
m.M43 = (float)zTranslate;
m.M44 = 1f;
return m;
}
/// <summary>
/// 2Dの[拡縮→回転→平行移動]変換行列を作る。
/// </summary>
/// <param name="scaleX">X軸方向倍率</param>
/// <param name="scaleY">Y軸方向倍率</param>
/// <param name="angle">回転角度(360.0で一周)</param>
/// <param name="transX">X軸方向平行移動</param>
/// <param name="transY">Y軸方向平行移動</param>
/// <returns>作られた行列</returns>
public static D3DMatrix Transform2D(double scaleX, double scaleY, double angle, double transX, double transY)
{
D3DMatrix m = D3DMatrix.Identity;
double cos = Cos(a), sin = Sin(a);
m.M11 = (float)(cos * scaleX);
m.M12 = (float)(sin * scaleX);
m.M21 = (float)(-sin * scaleY);
m.M22 = (float)(cos * scaleY);
m.M33 = 1.0f;
m.M41 = (float)transX;
m.M42 = (float)transY;
m.M44 = 1.0f;
return m;
}
static double Sin(double a) { return Math.Sin(a / 180d * Math.PI); }
static double Cos(double a) { return Math.Cos(a / 180d * Math.PI); }
static double Tan(double a) { return Math.Tan(a / 180d * Math.PI); }
static double Asin(double d) { return Math.Asin(d) / Math.PI * 180d; }
static double Acos(double d) { return Math.Acos(d) / Math.PI * 180d; }
static double Atan(double d) { return Math.Atan(d) / Math.PI * 180d; }
static double Atan2(double y, double x) { return Math.Atan2(y, x) / Math.PI * 180d; }
}