EDITED : Generate sphere whose center is not at origin
3 次查看(过去 30 天)
显示 更早的评论
How do I generate a sphere with center at [x = 4.26, y = 1.76, z = 1.62] with a radius of 0.25 units? The sphere needs to have 101 points. Please see the attached Sphere_101.fig for reference. The output should look like the figure produced by figure 'Sphere_101.png. Below this figure, is my code. When I run the code, the points are randomly arranged on the sphere instead of being neatly spaced. Can anyone please help?
I = imread('Sphere_101.png');
imshow(I);
%Center of sphere
recPosition = [4.26 1.76 1.62];
rng(0,'twister');
radius = 0.25;
%Number of points
nPoints = 100;
%Create array
test_xyz = zeros(101,3);
test_xyz(1,1) = recPosition(1);
test_xyz(1,2) = recPosition(2);
test_xyz(1,3) = recPosition(3);
radius = zeros(nPoints,1)+radius ;
azimuth = rand(size(radius)) * 2*pi;
elevation = (rand(size(radius)) .* pi)-pi/2;
[x,y,z] = sph2cart(azimuth,elevation,radius);
test_xyz(2:end,1) = test_xyz(1,1)+x;
test_xyz(2:end,2) = test_xyz(1,2)+y;
test_xyz(2:end,3) = test_xyz(1,3)+z;
figure
scatter3(test_xyz(:,1),test_xyz(:,2),test_xyz(:,3));
1 个评论
Steven Lord
2024-7-29
How exactly do you want to select the 101 points? I'd personally scale and translate the output from the sphere function if you want them "nicely arranged".
[x, y, z] = sphere;
% Scale -- I'll let you determine the correct scaling factor for your
% application.
x2 = 0.5*x;
y2 = 0.5*y;
z2 = 0.5*z;
% translate
x3 = x2 + 1;
y3 = y2 - 2;
z3 = z2 + 4;
figure
surf(x, y, z);
title('Unscaled, untranslated')
figure
surf(x2, y2, z2);
title('Scaled')
figure
surf(x3, y3, z3)
title('Scaled and translated')
回答(1 个)
Star Strider
2024-7-29
I made a few adjustments to your original code, adding (or changing) only these:
p1 = 1:51;
p2 = 52:101;
figure
scatter3(test_xyz(p1,1),test_xyz(p1,2),test_xyz(p1,3), 10, 'b', 'filled');
hold on
scatter3(test_xyz(p2,1),test_xyz(p2,2),test_xyz(p2,3), 10, 'r', 'filled');
hold off
axis('equal')
Try this —
I = imread('Sphere.png');
imshow(I);
%Center of sphere
recPosition = [4.26 1.76 1.62];
rng(0,'twister');
radius = 0.25;
%Number of points
nPoints = 100;
%Create array
test_xyz = zeros(101,3);
test_xyz(1,1) = recPosition(1);
test_xyz(1,2) = recPosition(2);
test_xyz(1,3) = recPosition(3);
radius = zeros(nPoints,1)+radius ;
azimuth = rand(size(radius)) * 2*pi;
elevation = (rand(size(radius)) .* pi)-pi/2;
[x,y,z] = sph2cart(azimuth,elevation,radius);
test_xyz(2:end,1) = test_xyz(1,1)+x;
test_xyz(2:end,2) = test_xyz(1,2)+y;
test_xyz(2:end,3) = test_xyz(1,3)+z;
p1 = 1:51;
p2 = 52:101;
figure
scatter3(test_xyz(p1,1),test_xyz(p1,2),test_xyz(p1,3), 10, 'b', 'filled');
hold on
scatter3(test_xyz(p2,1),test_xyz(p2,2),test_xyz(p2,3), 10, 'r', 'filled');
hold off
axis('equal')
I am not certain what you intend by ‘first 51’ and ‘last 50’, so I interpreted those literally to create ‘p1’ and ‘p2’.
.
2 个评论
Star Strider
2024-7-30
If you want them regularly-spaced rather than random, just use the speere function and be done with it —
I = imread('Sphere_101.png');
imshow(I);
%Center of sphere
recPosition = [4.26 1.76 1.62];
% rng(0,'twister');
% radius = 0.25;
%
% %Number of points
% nPoints = 100;
%
% %Create array
% test_xyz = zeros(101,3);
%
% test_xyz(1,1) = recPosition(1);
% test_xyz(1,2) = recPosition(2);
% test_xyz(1,3) = recPosition(3);
%
% radius = zeros(nPoints,1)+radius ;
% % azimuth = rand(size(radius)) * 2*pi;
% % elevation = (rand(size(radius)) .* pi)-pi/2;
% azimuth = linspace(1E-4, 2*pi, numel(radius)).';
% elevation = linspace(pi/2, 1E-4, numel(radius)).';
%
% [x,y,z] = sph2cart(azimuth,elevation,radius);
%
% test_xyz(2:end,1) = test_xyz(1,1)+x;
% test_xyz(2:end,2) = test_xyz(1,2)+y;
% test_xyz(2:end,3) = test_xyz(1,3)+z;
[X,Y,Z] = sphere(100);
X = X + recPosition(1);
Y = Y + recPosition(2);
Z = Z + recPosition(3);
p1 = 1:51;
p2 = 52:101;
figure
scatter3(X(p1,:), Y(p1,:), Z(p1,:), 10, 'b', 'filled');
hold on
scatter3(X(p2,:), Y(p2,:), Z(p2,:), 10, 'r', 'filled');
hold off
axis('equal')
.
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Surface and Mesh Plots 的更多信息
产品
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!