27

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?

David Koelle
  • 20,726
  • 23
  • 93
  • 130
J.R.
  • 5,789
  • 11
  • 55
  • 78

6 Answers6

13

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

dfa
  • 114,442
  • 31
  • 189
  • 228
  • Thanks for the link, JScience really does look great! – J.R. Oct 27 '09 at 19:21
  • Although in retrospect, it confuses me greatly. It does things I've never seen before. I guess I'll make a new question about it... – J.R. Oct 27 '09 at 19:34
  • Looking at the JScience website and CVS repository, it looks like nothing has changed in the past 2 years. Not exactly a good sign ... – Stephen C Oct 27 '09 at 23:10
4

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

devrys
  • 1,571
  • 3
  • 27
  • 43
Stegger
  • 41
  • 2
  • This is one solution to the OP's problem, but not an answer to the OP's question. – gh. Sep 25 '12 at 10:43
  • @gh. I don't know if it's that black and white... it certainly seems to clearly answer the second part of the OP's question "Is there any way to do Vector manipulations (particularly addition) in Java?" – Benjamin R Oct 12 '14 at 01:49
3

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.

luvieere
  • 37,065
  • 18
  • 127
  • 179
2

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.

M1EK
  • 820
  • 5
  • 10
  • Java3D is definitely set for abandonment. However, this is still a good answer, because you can easily just take "vecmath.jar" from Java3D and use the vector classes by themselves. – jprete Oct 27 '09 at 17:51
2

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

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

hoijui
  • 3,615
  • 2
  • 33
  • 41
Felix Scheffer
  • 346
  • 2
  • 5
  • 4
1

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

erickson
  • 265,237
  • 58
  • 395
  • 493