Plot a circle around xyz plotted points and then get its volume?
3 views (last 30 days)
Show older comments
If I have three vectors of points that I'm plotting with:
scatter3(xVector, yVector, zVector);
How can I draw a circle that encompasses all of these points and then calculate the volume?
1 Comment
arich82
on 6 Nov 2015
Edited: arich82
on 6 Nov 2015
You don't specify whether you need the minimum bounding sphere, or just an approximation to the minimum.
Alternatively, if you just need the volume of the point cloud, you can use the second output from convhull. Note that this could be substantially different from the volume of the minimum bounding sphere if the data are e.g. distributed in a disc...
How critical are accuracy (relative to the min bounding sphere) and speed, and how many points are you working with? Is this a one-time calculation?
Answers (1)
Star Strider
on 6 Nov 2015
I combined [xVector(:) yVector(:) zVector(:)] as ‘xyz’ here. The (:) reference creates column vectors from them, and that is absolutely necessary for this code.
This will do what you want:
xyz = randi(25, 30, 3); % Consolidated: [xVector(:) yVector(:) zVector(:)]
C = mean(xyz); % Calculate Centre
R = sum(bsxfun(@minus, xyz, C).^2, 2); % Calculate Radii
[Rmax, idx] = max(R); % Calculate Maximum Radius
Volume = 4*pi*Rmax^3/3; % Volume Of Sphere
[Xs,Ys,Zs] = sphere; % Use ‘sphere’ Function
Rsph = sqrt(Rmax); % Calculate Radius Of Sphere
figure(1)
plot3(xyz(:,1), xyz(:,2), xyz(:,3), '.') % Plot Data
hold on
plot3(C(1), C(2), C(3), 'r*') % Plot Centre
plot3(xyz(idx,1), xyz(idx,2), xyz(idx,3), 'bp') % Plot Maximum Radius
Sph = mesh(Rsph*Xs+C(1), Rsph*Ys+C(2), Rsph*Zs+C(3)); % Calculate Enveloping Sphere
hold off
grid on
axis equal
set(Sph, 'FaceAlpha',0.1) % Set Sphere Transparency
legend('Data', 'Centre', 'R_{max}', 'Location','EastOutside')
This code produces this plot that you can rotate when you plot it to see that it works:

1 Comment
Felipe Guerrero Medina
on 30 Dec 2020
hi, And the same can be done for a cylinder?, in order to obtain the location of the generated points (similar to a mesh).
thanks !!
See Also
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!