2
我需要一個項目的Vector2結構,我不是很強大的數學,這個類似乎工作,但我將不勝感激指示如何優化代碼或如果我已經做了任何錯誤。 還有一種比Math.Sqrt更便宜的距離計算方法嗎?C#Vector2代碼
private struct Vector2
{
public float X;
public float Y;
public Vector2(float x, float y)
{
this.X = x;
this.Y = y;
}
public static Vector2 operator +(Vector2 v1, Vector2 v2)
{
return new Vector2(v1.X + v2.X, v1.Y + v2.Y);
}
public static Vector2 operator -(Vector2 v1, Vector2 v2)
{
return new Vector2(v1.X - v2.X, v1.Y - v2.Y);
}
public static Vector2 operator *(Vector2 v1, float m)
{
return new Vector2(v1.X * m, v1.Y * m);
}
public static float operator *(Vector2 v1, Vector2 v2)
{
return v1.X * v2.X + v1.Y * v2.Y;
}
public static Vector2 operator /(Vector2 v1, float m)
{
return new Vector2(v1.X/m, v1.Y/m);
}
public static float Distance(Vector2 v1, Vector2 v2)
{
return (float)Math.Sqrt(Math.Pow(v1.X - v2.X, 2) + Math.Pow(v1.Y - v2.Y, 2));
}
public float Length()
{
return (float)Math.Sqrt(X * X + Y * Y);
}
}
http://www.opentk.com/doc/math看看這個,如果你不想重新發明輪子。 – CSharpie