2023-02-17 423
JDK8包含3个不同的Point2D类.
中
中
中
我应该使用哪个Point2D
使用/应用程序:我想执行几何计算,以确定一个点是否与线相交. (例如Line2D.contains(Point2D))
鉴于我还使用其他Javafx功能(即在javafx.*软件包中).我的第一个猜测是使用 javafx.geometry.Point2D class .但是,尽管此软件包确实包含Point2D类,但它不包含Line2D类,但是其他2个软件包 do 包含Line2D软件包./p>
另一方面,我不想选择将在不久的将来弃用的课程.
也许是一个较小的细节: awt和com.sun的Point2D类com.sun包装使用float定义其点,这需要大量铸造. javafx版本使用double,这很方便,因为Javafx也更喜欢double安排组件(例如getPrefWidth,getLayoutX,getLayoutX,…).
实际上Line2D课程并不是很大的帮助. contains方法总是返回false.因此,看来我无论如何都必须写自己的相交方法.
java.awt是与Javafx不同的UI工具包.不建议将不同库的类混合,特别是只要您已经使用了Javafx功能.
应避免使用com.sun开始的所有内容,因为它是私有API,并且不能保证它将继续在下一个更新中工作.
在这种情况下,您最好的行动是使用javafx.geometry.Point2D并实现自己的Line2D.作为替代方案,您可以使用javafx场景图及其Circle(半径为0.5)和Line(带1个宽度为1个)类来帮助您进行计算.
我接受了AlmastB的建议,并决定创建我自己的Line2D class 以兼容javafx.geometry.Point2D class.
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;
}
这对以前的两个说法(即精度和界限)进行了.
以上所述是小编给大家介绍的我应该使用哪个Point2D,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对77isp云服务器技术网的支持!
原文链接:https://77isp.com/post/34151.html
=========================================
https://77isp.com/ 为 “云服务器技术网” 唯一官方服务平台,请勿相信其他任何渠道。
数据库技术 2022-03-28
网站技术 2022-11-26
网站技术 2023-01-07
网站技术 2022-11-17
Windows相关 2022-02-23
网站技术 2023-01-14
Windows相关 2022-02-16
Windows相关 2022-02-16
Linux相关 2022-02-27
数据库技术 2022-02-20
抠敌 2023年10月23日
嚼餐 2023年10月23日
男忌 2023年10月22日
瓮仆 2023年10月22日
簿偌 2023年10月22日
扫码二维码
获取最新动态