问题
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)
回答1:
Answering myself as I've found a solution.
Found a working nice snippet here: http://netzwerg.ch/blog/2015/03/22/javafx-3d-line/
Here is the code, it's simple:
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;
}
来源:https://stackoverflow.com/questions/38799322/javafx-3d-transforming-cylinder-to-defined-start-and-end-points