The jdk8 contains 3 different Point2D
classes.
java.awt.geom.Point2D
in thert.jar
javafx.geometry.Point2D
in thejfxrt.jar
com.sun.javafx.geom.Point2D
in thejfxrt.jar
Which Point2D
class should I use ?
Use/application: I want to perform geometric calculations to determine if a point intersects with a line. (e.g. Line2D.contains(Point2D)
)
Given the fact that I'm also using other javafx features (i.e. also in the javafx.*
package). My first guess, would be to use the javafx.geometry.Point2D
class. However while this package does contain a Point2D
class, it does not contain a Line2D
class, but the other 2 packages do contain a Line2D
package.
On the other hand, I don't want to pick a class that will be deprecated in the near future.
EDIT:
Perhaps a minor detail: the Point2D
class of the awt
and com.sun
package use float
's for defining their points, which requires a lot of casting. While the javafx
version uses double
, which is pretty convenient, since javafx also prefers double
for arranging components (e.g. getPrefWidth
, getLayoutX
, ...).
EDIT2:
Actually the Line2D
class is not a big help. The contains
methods always return false
. So, it looks like I have to write my own intersect method anyway.
I took the advice of AlmastB, and decided to create my own
Line2D
class for compatibility with thejavafx.geometry.Point2D
class.As you will notice, originally I only made a
distance
method. Anintersects
method is tricky, because it requires comparingdouble
values. Operations with floating point numbers can introduce small deviations. In short: you can only judge if a point is on a line, by a certain precision. It's hard to say what that precision should be.The
distance
method has a point of attention as well. It assumes that the line runs through the 2 points, but is not bounded by it (i.e. it has an infinit length).Intersects method
An
intersects
method would look as follows:This takes the 2 previous remarks in account (i.e. precision and bounds).
java.awt
is a different UI toolkit to JavaFX. It is not recommended to mix classes from different libraries, especially provided that you already use JavaFX features.Everything that starts with
com.sun
should be avoided as it is private API and there is no guarantee that it will continue working in the next update.Your best course of action in this scenario is to use
javafx.geometry.Point2D
and implement your ownLine2D
. As an alternative you can use the JavaFX scene graph and itsCircle
(with radius of 0.5) andLine
(with stroke width of 1) classes to help you with your calculations.