Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

java

android

3d

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?

like image 203
SimpleSi Avatar asked Apr 22 '11 19:04

SimpleSi


People also ask

Is there a point class in Java?

Class Point. A point representing a location in (x,y) coordinate space, specified in integer precision.

What is a Point class?

In the mathematical field of descriptive set theory, a pointclass is a collection of sets of points, where a point is ordinarily understood to be an element of some perfect Polish space.


3 Answers

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.

like image 170
Noah Avatar answered Nov 15 '22 00:11

Noah


Maybe Point3D is what you need.

like image 29
duffymo Avatar answered Nov 14 '22 23:11

duffymo


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

like image 27
Maciej Avatar answered Nov 14 '22 23:11

Maciej