你能用 Java 原生做向量加法吗?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1632481/
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
Can you do Vector addition in Java, natively?
提问by Jenny
I Know there's a "Vector" class in java, but it seems to be just a simpler ArrayList type of deal, not an actual, mathematical Vector (as in a magnitude and a direction).
我知道 Java 中有一个“Vector”类,但它似乎只是一种更简单的 ArrayList 类型的交易,而不是实际的数学矢量(如大小和方向)。
Is there any way to do Vector manipulations (particularly addition) in Java? Or am I stuck on my own having to implement it or use a third party module?
有没有办法在 Java 中进行 Vector 操作(特别是加法)?还是我必须自己实现它或使用第三方模块?
采纳答案by dfa
Yes, you'll have to write a class or use a library such as JScience
是的,您必须编写一个类或使用诸如JScience 之类的库
回答by erickson
Yes, you'll have to write a library (or use a third-party library) in order to perform vector arithmetic.
是的,您必须编写一个库(或使用第三方库)才能执行向量算术。
回答by luvieere
回答by M1EK
Java3D has various forms of Vector classes (Vector3d, Vector3f, Vector4d, etc). Java3D, of course, is somewhat risky these days, though, as it's seemingly set for abandonment.
Java3D 有多种形式的 Vector 类(Vector3d、Vector3f、Vector4d 等)。当然,现在 Java3D 有点冒险,因为它似乎已经准备好被放弃了。
回答by Stegger
If you are looking to make a vector in 2d space, couldn't you just go with a simple Point2D(x,y)
and let the length of your vector define magnitude?
如果您想在 2d 空间中创建一个向量,难道您不能简单地Point2D(x,y)
让向量的长度定义大小吗?
So that Point2D a = new Point2D(1,1);
has a magnitude of 1.4, and a NE direction. And a Point2D b = new Point2D(2,2);
has the same direction but a magnitude of 2.8...
所以它Point2D a = new Point2D(1,1);
的大小为 1.4,并且是 NE 方向。并且 aPoint2D b = new Point2D(2,2);
具有相同的方向但大小为 2.8...
Addition would then just be: Point2D c = new Point2D(a.x + b.x, a.y + b.y);
加法就是: Point2D c = new Point2D(a.x + b.x, a.y + b.y);
In 3d space I would create my own class, or an entirely different data structure depending on you actual problem.
在 3d 空间中,我会根据您的实际问题创建自己的类或完全不同的数据结构。
Edit: I hope he has found a solution in the past 3 years..
编辑:我希望他在过去 3 年中找到了解决方案。
回答by Felix Scheffer
I know this is old but maybe someone will find this useful:
我知道这很旧,但也许有人会发现这很有用:
There is also Apache Commons Mathwhich has a Vector2Dclass.
还有具有Vector2D类的Apache Commons Math。