问题
How to draw 3d graphs in JavaFX using a mathematical equation,
basically 2 variable functions, for example: z=2xy
and other 3D figures?
Is there any way to do it in JavaFX or do I need another Java library for that.
回答1:
As @Roland points out, JavaFX 3D API doesn't include other than the basic elements, like TriangleMesh, which you can use to create complex shapes, like 3D graphs.
In fact, plotting 2D functions f=f(x,y)
is be a very good use case for understanding how TriangleMesh
works.
Basically you will need:
A function
The function can be expressed using built-in Function
functional interface:
Function<Point2D,Number> function2D;
so for any pair of coordinates (x,y) it will return a value:
double value = function2D.apply(new Point2D(x,y)).doubleValue();
Grid or range of coordinates
If you think of a rectangular grid and a given number of the divisions, you will have a way to get all the plotting (x,y)
points, and with the function, you will have the third coordinate z
to generate the 3D points required for the mesh.
TriangleMesh triangleMesh = new TriangleMesh();
triangleMesh.getPoints().setAll(x0,y0,z0, x1,y1,z1, ...);
You will need to provide the texture coordinates if you want to have an image or a density map as a texture, or just an empty set of coordinates for now:
triangleMesh.getTexCoords().setAll(0,0);
Finally, you need to provide the faces, which are triangles. You just need to get the indices of the vertices for every triangle in your grid, like in this sample, using 0 for the texture indices in this case:
triangleMesh.getFaces().setAll(0,0,20,0,21,0,...);
And you will have your mesh, ready to be rendered in a scene.
Third party libraries
You can have a look at FXyz library, where you will find SurfacePlotMesh, that will do exactly as described above, including texture coordinates. The FXyz Sampler is an application to visualize most of the possibilities on this library. This is a sample of a plotted function:
For other 3D shapes, have a look to the rest of the 3D complex shapes in the library.
And you can have a look to VRL Studio, which includes a great 3D function plotter, among other things.
回答2:
I wrote a JavaFX demo application (embedded in swing) which can plot 3d points. You can create/calculate some 3d points with your function and than just draw it. Check out the StarterFrame class, where the points are generated. More points means a more detailed plot. Maybe this helps you to write something on your own. Otherwise i would recommend a library.
https://github.com/adihubba/javafx-3d-surface-chart
回答3:
JavaFX doesn't have a built-in mechanism to draw 3d graphs. You have to use a 3rd party library.
来源:https://stackoverflow.com/questions/31755757/drawing-3d-surfaces-in-javafx