我刚看了这教程。它是关于游戏的发展,它基本上是说,我需要存储加速度,速度,位置作为矢量,使游戏物理。希望这有意义!我现在需要选择数据类型。例如,我需要存储像.
(3、5、2) (6、2、3) (2,3)
还有,我需要做这样的加减法。
(0,1,4) + (3,-2,5) = (0 + 3,1-2,4+ 5) = (3,-1,9)
在这种情况下我应该使用什么数据类型?
对于一个向量,可能有两个整数(浮点数/双数)?可能是一个向量的数组,其中的值是整数(浮点数/双数)?
发布于 2011-04-30 19:45:57
听起来您需要三个值(可能是双倍值):
public class Vector3
{
private final double x;
private final double y;
private final double z;
public Vector3(double x, double y, double z)
{
this.x = x;
this.y = y;
this.z = z;
}
public Vector3 plus(Vector3 other)
{
return new Vector3(x + other.x, y + other.y, z + other.z);
}
// etc
}请注意,我已经使这个不变--这并不总是性能的最佳选择(这可能与您的情况相关),但有助于提高可读性。
发布于 2011-04-30 19:56:09
也许公域-数学 OpenMapRealVector是有用的。
发布于 2011-04-30 20:02:41
或者您可以使用泛型:
public interface MathVector<T extends Number>
{
MathVector<T> add(MathVector<T> addend);
T innerProduct();
// Vectors have more than this, but it makes the point
}https://stackoverflow.com/questions/5844193
复制相似问题