Which Point2D should be used

jdk8 contains 3 different classes Point2D.

  • java.awt.geom.Point2D in rt.jar
  • javafx.geometry.Point2D in jfxrt.jar
  • com.sun.javafx.geom.Point2D in jfxrt.jar

Which class Point2Dshould be used?

Usage / Application: I want to perform geometric calculations to determine if a point intersects a line. (e.g. Line2D.contains(Point2D))

Given the fact that I also use other javafx functions (i.e. also in the package javafx.*). My first guess: use a class . However, although this package contains a class , it does not contain a class , but the remaining 2 do packages contain a package . javafx.geometry.Point2DPoint2DLine2DLine2D

On the other hand, I do not want to choose a class that will become obsolete in the near future.

EDIT:

Perhaps a small detail: the Point2Dpackage class awtalso com.sunuses floatto determine its points, which requires a lot of casting. While the version javafxused double, which is rather convenient because javafx also prefers doubleto place components (for example getPrefWidth, getLayoutX...).

EDIT2:

Actually a class is Line2Dnot a big help. Methods containsalways return false. So it looks like I should write my own intersection method.

+4
source share
2 answers

java.awt - JavaFX. , JavaFX.

, com.sun, , API, , .

- javafx.geometry.Point2D Line2D. JavaFX Circle ( 0.5) Line ( 1), .

+5

AlmastB Line2D javafx.geometry.Point2D.

public class Line2D {
    double x1;
    double y1;
    double x2;
    double y2;

    public Line2D(double x1, double y1, double x2, double y2) {
        this.x1 = x1;
        this.y1 = y1;
        this.x2 = x2;
        this.y2 = y2;
    }

    public Line2D(Point2D point1, Point2D point2) {
        this(point1.getX(), point1.getY(), point2.getX(), point2.getY());
    }

    public double length()
    {
        return new Point2D(x1, y1).distance(x2, y2);
    }

    public double distance(double x, double y) {
        double d1 = x * (y2 - y1) - y * (x2 - x1) + x2 * y1 - y2 * x1;
        return Math.abs(d1) / length();
    }

    public double distance(Point2D toPoint) {
        return distance(toPoint.getX(), toPoint.getY());
    }
}

, , distance. intersects , double. . : , , , . , .

distance . , 2 , (.. ).

intersects :

    public boolean intersects(Point2D toPoint, double precision, boolean checkBounds) {
        if (checkBounds &&
                ((toPoint.getX() < Math.min(x1, x2) - precision) ||
                 (toPoint.getX() > Math.max(x1, x2) + precision) ||
                 (toPoint.getY() < Math.min(y1, y2) - precision) ||
                 (toPoint.getY() > Math.max(y1, y2) + precision))) return false;
        return distance(toPoint.getX(), toPoint.getY()) < precision;
    }

2 (.. ).

0

Source: https://habr.com/ru/post/1606560/


All Articles