JavaFX 3D:将Cylinder转换为定义的起点和终点

时间:2016-08-06 00:10:51

标签: javafx javafx-8 javafx-3d

假设我希望Cylinder以某个3D点开始并以某个其他3D点结束。

据我所知,这样做的方法是计算2点之间的欧几里德距离,并创建一个长度相同的圆柱体。然后,应该平移和旋转圆柱体,使其真正从起点开始并在终点结束。

我对这些转变感到困惑,并没有成功将Cylinder放在正确的位置。

请您分享一下该函数实现的一些代码片段:

void createCylinder(Group group, double p1X, double p1Y, double p1Z, 
                                 double p2X, double p2Y, double p2Z)

1 个答案:

答案 0 :(得分:1)

在我找到解决方案时回答自己。

在这里找到一个漂亮的代码片段:http://netzwerg.ch/blog/2015/03/22/javafx-3d-line/

以下是代码,很简单:

public Cylinder createConnection(Point3D origin, Point3D target) {
    Point3D yAxis = new Point3D(0, 1, 0);
    Point3D diff = target.subtract(origin);
    double height = diff.magnitude();

    Point3D mid = target.midpoint(origin);
    Translate moveToMidpoint = new Translate(mid.getX(), mid.getY(), mid.getZ());

    Point3D axisOfRotation = diff.crossProduct(yAxis);
    double angle = Math.acos(diff.normalize().dotProduct(yAxis));
    Rotate rotateAroundCenter = new Rotate(-Math.toDegrees(angle), axisOfRotation);

    Cylinder line = new Cylinder(1, height);

    line.getTransforms().addAll(moveToMidpoint, rotateAroundCenter);

    return line;
}