Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Drawing 3d surfaces in JavaFX

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.

like image 976
user3478710 Avatar asked Oct 19 '25 08:10

user3478710


1 Answers

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:

faces

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:

FXyz

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.

like image 105
José Pereda Avatar answered Oct 22 '25 05:10

José Pereda



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!