记住已计算坐标的最快方法 - Java

Fastest way to remember already calculated coordinates - Java

所以我正在尝试在 java 中制作 3d 游戏。我已经完成了渲染工作,现在我在考虑什么是最快的记忆和检查坐标是否已经计算的方法?

例如,我有一个立方体,它有 8 个角,我的游戏计算每个角 3 次(总共 24 次),因为每个角都与 3 个面共享。

那么,存储和检查坐标是否已计算的最快方法是什么?

此外,我已经尝试搜索此内容,但找不到结果。如果有这样的问题已经回答了。

谢谢。

确保您的坐标 class 覆盖 hashCode()equals(Object) 并将您已经渲染的每个坐标存储在 HashSet.

如果您发现这开始占用过多内存,您可以尝试使用某种 LRU 缓存。

可以使用HashMap,以自己的3D点值对象为key,计算结果类型为value。

class Point3D {
    private final double x, y, z;
    public Point3D(final double x, final double y, final double z) {
        this.x=x;
        this.y=y;
        this.z=z;
    }
    @Override
    public int hashCode() {
        final int prime = 31;
        int result = 1;
        long temp;
        temp = Double.doubleToLongBits(x);
        result = prime * result + (int) (temp ^ (temp >>> 32));
        temp = Double.doubleToLongBits(y);
        result = prime * result + (int) (temp ^ (temp >>> 32));
        temp = Double.doubleToLongBits(z);
        result = prime * result + (int) (temp ^ (temp >>> 32));
        return result;
    }
    @Override
    public boolean equals(Object obj) {
        if (this == obj)
            return true;
        if (obj == null)
            return false;
        if (getClass() != obj.getClass())
            return false;
        Point3D other = (Point3D) obj;
        if (Double.doubleToLongBits(x) != Double.doubleToLongBits(other.x))
            return false;
        if (Double.doubleToLongBits(y) != Double.doubleToLongBits(other.y))
            return false;
        if (Double.doubleToLongBits(z) != Double.doubleToLongBits(other.z))
            return false;
        return true;
    }
}
public class Main{
    public static void main(String[] args){
        final Map<Point3D, Result> cache = new HashMap<>();
        cache.put(new Point3D(0, 0, 1), new Result(...));
    }
}