There was a need to connect two points with a cylinder . There are coordinates of two points defined as instances of Point3D :

 Point3D pointA = new Point3D(100,200,150); Point3D pointB = new Point3D(200,150,-100); 

There is a group ( javafx.scene.Group ) in which constructions are made:

 Group space3D = new Group(); 

Task: connect these two virtual points (can be placed in these coordinates on the Sphere instance with a radius of 10 , for clarity) with a javafx.scene.shape.Cylinder instance javafx.scene.shape.Cylinder radius of 2 equal to the distance from point A to point B with the minimum number of actions.

PS: Which alternatives would be more effective for visually connecting two points in 3D space?

  • one
    See what I found in the source code of GeoGebra: here . It is true here to understand and understand :) - pank
  • @Sergey Pestov, thanks for the interesting link. Briefly, a cylinder is built there by two points and a radius: they take points, create two bases with a given radius and connect them. Clear and correct solution in terms of geometry. How would you effectively implement this solution in JavaFX? (Note that the cylinder is already represented in JavaFX as a graphics primitive.) - DimXenon
  • If I were you, I would use something else for 3D. - pank
  • Is there anything simpler and more portable than javaFX? Please tell us! - DimXenon
  • one
    For example, ThreeJS . Will work in any browser and features more. An example . - pank

1 answer 1

I'll start from the reverse. An example of a solution:

 public Cylinder paintCylinder(Point3D A, Point3D B) { Point3D temp = A.subtract(B); double Y = temp.getX() != 0 || temp.getZ() != 0 ? B.getY() : B.getY() > A.getY() ? B.getY() : A.getY(); Point3D dir = A.subtract(B).crossProduct(new Point3D(0, -1, 0)); double angle = Math.acos(A.subtract(B).normalize().dotProduct(new Point3D(0, -1, 0))); double h1 = A.distance(B); Cylinder c = new Cylinder(2d, h1); c.getTransforms().addAll(new Translate(B.getX(), Y - h1 / 2d, B.getZ()), new Rotate(-Math.toDegrees(angle), 0d, h1 / 2d, 0d, new Point3D(dir.getX(), -dir.getY(), dir.getZ()))); return c; } 

As a result:

Cylinder 2 point javaFx :

Now closer to specific issues:

It happened so that it is easier to understand the rotation of an object, this is a sequential rotation along 3 coordinates (and this approach is often used). But if you figure out, it’s quite natural to rotate an object only once at one particular angle. And to be completely accurate, turn the coordinate system so that the planes turned out to be the points of the beginning and end of the turning arc. It is also possible to work with the rotation matrix and as an example of working with it this answer. Yes, of course, this approach is faster than working with a consistent rotation on three axes, but I want to note that the author of the answer itself (he is by the way and the library developer - FXyz) according to my observations matrix implementation is not
enjoys (observing the framework of open source on GitHub).

  • And this solution is still the best of all! - DimXenon
  • If the answer does not appear even more brilliant in the next 12 hours, the prize will go to you, Peter Slusar! - DimXenon
  • @DimXenon sarcasm?) But essentially if I have questions I will try to answer. I myself have been scrolling around the network for quite a long time with too little information about building 3D objects using JavaFx. To be more precise, very few ready-made libraries for the most part have to work with very basic methods and not operate something higher: / - Peter Slusar
  • Not. I'm serious. I really liked your answer. Thank! - DimXenon
  • one
    The situation can be controlled here: new Translate (B.getX (), B.getY () - h1 / 2d, B.getZ () (replacing the starting point of the construction) or at the stage of working with the axis ... I will work it out a bit later and add - Peter Slusar