Is there and equivalent of Point class but for 3D points?

11,804

Solution 1

Having recently done some vector mapping (including z / 3D), and seeing your Android tag, I recommend rolling your own.

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)

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.

Solution 2

Maybe Point3D is what you need.

Solution 3

There is also a JavaFX class Point3D that meets your requirements.

Share:
11,804
SimpleSi
Author by

SimpleSi

Updated on June 04, 2022

Comments

  • SimpleSi
    SimpleSi almost 2 years

    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?

  • gcooney
    gcooney about 13 years
    Just wanted to add that you'll need to download the vecmath library to use that class. Links to the library can be found in another stack overflow question: stackoverflow.com/questions/4381291/import-javax-vecmath
  • SimpleSi
    SimpleSi about 13 years
    mm- I followed your link but didn't find anything to actually download :(
  • SimpleSi
    SimpleSi about 13 years
    Well you might be right but mainly because I can't find a ladder to stand on the shoulders of giants :) However, I will look up immutable and learn a bit more about Java :)
  • DPM
    DPM over 6 years
    Vecmath's point3d is mutable so I would avoid it like the plague. JavaFX is now part of the standard JDK and has an immutable point3d class. Of course it's cumbersome when you need to import 3rd party library for more complex math operations, libraries that usually have horrible interfaces and mutable objects (anything apache, jmonkeyengine) and have to do all the point conversions back and forth...
  • DPM
    DPM over 6 years
    While points, vectors and tuples operations don't have so much code, you can still copy the source code of some libraries class and then add immutability or whatever. Many of these mathematical-related libraries are so badly designed that it is not always straightforward reducing code duplication: final methods, final classes and lack of interfaces make it a pain. If they are not going to do a sound design, it's really intriguing why they are so opinionated about finals...
  • DPM
    DPM over 6 years
    Another minor defect of all point classes of any commonly used library, including javafx, is that they do equality comparison based on the coordinates' value. For objects composed of floating point values, I find it more sensible to use reference equality and always perform equality within an epsilon of error. Otherwise it can lead to errors in applications where different point objects could share the same spatial position.