JavaFX 3D:将圆柱体转换为定义的起点和终点 [英] JavaFX 3D: Transforming Cylinder to defined start and end points
问题描述
假设我希望Cylinder
从某个3D点开始并在另一个3D点结束.
Suppose that I want a Cylinder
to start in some 3D point and to end in some other 3D point.
据我所知,这样做的方法是计算两个点之间的欧几里得距离,并创建具有相同长度的圆柱体.然后,应平移并旋转圆柱体,使其真正在起点处开始,并在终点处结束.
As far as I know, the way to do that, is to compute the Euclidian distance between the 2 points and to create a Cylinder with the same length. Then, the cylinder should be translated and rotated, such that it really starts at the start point and ends at the end point.
我对这些转换感到迷惑,没有成功将圆柱体放置在正确的位置.
I get messed up with these transformation and do not succeed to place the Cylinder in its correct place.
能否请您分享该功能实现的一些代码段:
Could you please share some code snippet of implementation of the function:
void createCylinder(Group group, double p1X, double p1Y, double p1Z,
double p2X, double p2Y, double p2Z)
推荐答案
在找到解决方案时回答自己.
Answering myself as I've found a solution.
在此处找到了一个有效的代码段: http://netzwerg. ch/blog/2015/03/22/javafx-3d-line/
Found a working nice snippet here: 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;
}
这篇关于JavaFX 3D:将圆柱体转换为定义的起点和终点的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!