java 除了 3D 点之外,是否有等效于 Point 类?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5759375/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Is there and equivalent of Point class but for 3D points?
提问by SimpleSi
I would like to store the x y and z co-ords for some objects for a game but I can't find a built in class like Point. Is there a nice standard class I could add in and use that would handle distance between points/bearings from one object to another etc?
我想为游戏的某些对象存储 xy 和 z 坐标,但我找不到像 Point 这样的内置类。是否有一个很好的标准类我可以添加并使用它来处理从一个对象到另一个对象的点/轴承之间的距离等?
采纳答案by Noah
Having recently done some vector mapping (including z / 3D), and seeing your Android tag, I recommend rolling your own.
最近做了一些矢量映射(包括 z / 3D),并看到了你的 Android 标签,我建议你自己滚动。
The reasons are many:
原因有很多:
- You can customize to meet your specific precision / memory / performance constraints.
- If multi threaded, you can make your class immutable and thread-safe
- I.e. If memory constrained you can store all three dimensions in an int or long
- If cpu constrained you can use plain-old separate numbers
- If GC / Garbage constrained, you can recycle and pool instances (mutable)
- 您可以自定义以满足您特定的精度/内存/性能限制。
- 如果是多线程的,您可以使您的类不可变且线程安全
- 即如果内存受限,您可以将所有三个维度存储在 int 或 long 中
- 如果 cpu 受限,您可以使用普通的单独数字
- 如果 GC / Garbage 受限,您可以回收和池化实例(可变)
In the end, most of these primitives are quite simple to write, test, etc. The main methods you'll need to write (beyond boilerplate constructor/get/set/...) - Distance - Dot product - Unitize (make length == 1 for various math ops) - And I've used DistanceSquared in the past for comparison functions... This removes the sqrt operator from most distance methods, while computing a relative distance useful enough for comparing point distances etc.
最后,这些原语中的大多数都非常容易编写、测试等。您需要编写的主要方法(除了样板构造函数/get/set/...) - 距离 - 点积 - Unitize(使长度== 1 用于各种数学运算) - 我过去曾将 DistanceSquared 用于比较函数......这从大多数距离方法中删除了 sqrt 运算符,同时计算了对比较点距离等足够有用的相对距离。