1

假设我希望 aCylinder从某个 3D 点开始,并在某个其他 3D 点结束。

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

我搞砸了这些转换,并且没有成功将圆柱体放置在正确的位置。

您能否分享一些实现该功能的代码片段:

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

1 回答 1

3

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

在这里找到了一个很好的片段: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;
}
于 2016-08-06T00:36:15.257 回答