我需要一个项目的 Vector2 结构,我对数学不是很擅长,这个类似乎正在工作,但我将非常感谢任何关于如何优化代码或如果我犯了任何错误的指针。还有比 Math.Sqrt 更便宜的距离计算方法吗?
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);
}
}