開発環境 Microsoft Visual C# 2010 Express (SP1)
実行環境 Microsoft Windows XP Home Edition (SP3)
プロジェクトの種類 空のプロジェクト
プロジェクト名 Quaternion

参考

Program.cs
// Quaternion3 球面線形補間の実験3
using System;
using Microsoft.Xna.Framework; // .NET参照
 
class Program
{
    static void Main()
    {
        float rad = MathHelper.ToRadians(60);
        float x = (float)Math.Cos(rad);
        float y = (float)Math.Sin(rad);
 
        Vector3 v1 = new Vector3(1, 0, 0);
        Vector3 v2 = new Vector3(x, y, 0);
        Vector3 v3 = new Vector3(-x, y, 0);
 
        Print(v1);
        Print(v2);
        Print(v3);
 
        float dot;
        dot = Vector3.Dot(v1, v2);
        Console.WriteLine(string.Format("dot={0:f2}", dot));
        Console.WriteLine("0°→60°");
        Test(v1, v2);
 
        dot = Vector3.Dot(v1, v3);
        Console.WriteLine(string.Format("dot={0:f2}", dot));
        Console.WriteLine("0°→120°");
        Test(v1, v3);
 
        Console.ReadLine();
    }
 
    static void Print(Vector3 v)
    {
        Console.WriteLine(string.Format("x={0:f2} y={1:f2} z={2:f2} len={3:f2}",
            v.X, v.Y, v.Z, v.Length()));
    }
 
    static void Test(Vector3 v1, Vector3 v2)
    {
        for (int n = 0; n <= 4; n++)
        {
            float t = n / 4.0f;
            Vector3 v = Slerp(v1, v2, t);
            float lat = MathHelper.ToDegrees((float)Math.Asin(v.Y));
            Console.Write(string.Format("{0} lat={1:f1} / ", n, lat));
            Print(v);
        }
    }
 
    static Vector3 Slerp(Vector3 value1, Vector3 value2, float amount)
    {
        value1.Normalize();
        value2.Normalize();
        float dot = Vector3.Dot(value1, value2); // cosθ
        float angle = (float)Math.Acos(dot); // 2ベクトル間の角度
        float Ps = (float)Math.Sin(angle * (1 - amount));
        float Pe = (float)Math.Sin(angle * amount);
        Vector3 v = (Ps * value1 + Pe * value2) / (float)Math.Sin(angle);
        v.Normalize();
        return v;
    }
}
 

出力
x=1.00 y=0.00 z=0.00 len=1.00
x=0.50 y=0.87 z=0.00 len=1.00
x=-0.50 y=0.87 z=0.00 len=1.00
dot=0.50
0°→60°
0 lat=0.0 / x=1.00 y=0.00 z=0.00 len=1.00
1 lat=15.0 / x=0.97 y=0.26 z=0.00 len=1.00
2 lat=30.0 / x=0.87 y=0.50 z=0.00 len=1.00
3 lat=45.0 / x=0.71 y=0.71 z=0.00 len=1.00
4 lat=60.0 / x=0.50 y=0.87 z=0.00 len=1.00
dot=-0.50
0°→120°
0 lat=0.0 / x=1.00 y=0.00 z=0.00 len=1.00
1 lat=30.0 / x=0.87 y=0.50 z=0.00 len=1.00
2 lat=60.0 / x=0.50 y=0.87 z=0.00 len=1.00
3 lat=90.0 / x=0.00 y=1.00 z=0.00 len=1.00
4 lat=60.0 / x=-0.50 y=0.87 z=0.00 len=1.00
最終更新:2012年12月14日 17:32