Can you do Vector addition in Java, natively?

2019-01-25 02:59发布

问题:

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).

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?

-Jenny

回答1:

Yes, you'll have to write a class or use a library such as JScience



回答2:

I don't think there is a built-in way to do vector addition, however I've found a series describing how this could be done.



回答3:

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?

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...

Addition would then just be: 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.

Edit: I hope he has found a solution in the past 3 years..



回答4:

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.



回答5:

Yes, you'll have to write a library (or use a third-party library) in order to perform vector arithmetic.



回答6:

I know this is old but maybe someone will find this useful:

There is also Apache Commons Math which has a Vector2D class.



标签: java math vector