How to generate a torus structure in MATLAB similar to Minecraft builds?

36 views (last 30 days)
I am trying to use MATLAB to generate and visualize a torus shaped structure. Mathematically, I know a torus can be parameterized as:
x=(R+rcosv)cosu, y=(R+rcosv)sinu, z=rsinv
where RRR is the major radius and rrr is the minor radius.
However, my goal is to make it look more like a block-based Minecraft torus build, where the shape is represented by discrete voxels or grid points rather than a smooth surface.
For reference, I found this Minecraft Torus Generator, which shows how different torus radii translate into block-based rings, but I would like to recreate the same effect programmatically in MATLAB.
My questions are:
  1. What is the best approach in MATLAB to discretize the torus equation into a voxel/block grid representation?
  2. Should I use meshgrid and thresholding, or is there a more efficient way to represent and render such a structure?
  3. Once I have the data points, is voxelPlot or isosurface a good visualization method to achieve a Minecraft-style look?
Any insights or example code would be greatly appreciated!

Answers (1)

Mathieu NOE
Mathieu NOE on 26 Sep 2025
hello Sarah
maybe this ?
to create the initail mesh I used this fex submission : Mesh/Voxel spheres, ellipsoids, toroids, and test objects - File Exchange - MATLAB Central but you can of course use your own code. This fex is quite interesting in case you want more complex shapes or combination of them
my result sor far
meshed taurus:
voxelized taurus :
demo code :
% Taurus parameters
R=[45];
r=[6];
stepSize=1;
centres=[0 0 0];
deform=[1 1 1];
rotation=[-45, 0, 45];
% defining only major (R) and minor (r) radii builds a series of
% concentric toroid rings at [0 0 0]
FV=multiMeshToroidCreator(R, r, centres, deform, rotation, stepSize);
figure; axis equal;
patch(FV, 'FaceColor', 'b', 'edgecolor', 'r');
% Convert the mesh to a voxelvolume
% Define a 3D grid
a = ceil((R + r)*2.2) ;
d = 3; % define your "block" size
[x, y, z] = ndgrid(-a:d:a, -a:d:a, -a:d:a); % Adjust grid size as needed
gridPoints = [x(:), y(:), z(:)];
% Check if points are inside the mesh
inMesh = inpolyhedron(FV.faces, FV.vertices, gridPoints);
% fex : https://fr.mathworks.com/matlabcentral/fileexchange/37856-inpolyhedron-are-points-inside-a-triangulated-volume
% Reshape to voxel grid
voxelGrid = reshape(inMesh, size(x));
% Show iso surface of result
figure, axis equal;
patch(isosurface(voxelGrid,0.1), 'Facecolor', [1 0 0]);

Tags

Products

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!