gpt4 book ai didi

matlab - 球体的着色部分,一些区域以未分配的颜色结束

转载 作者:行者123 更新时间:2023-12-05 04:20:21 24 4
gpt4 key购买 nike

我正在尝试创建一个球形直方图,我发现了一个创建球形直方图的插件,但是它使用等面积四边形,出于我的目的,我想保留在使用 MATLAB 创建的传统球体上找到的线,以便它们可以匹配纬度和经度线。

我找到了一种方法,通过设置我想要着色的区域的最小/最大方位角和高程值来为球体的给定子集着色,并尝试和测试为球体的每个单元格着色,我尝试将方位角和仰角总角度由给定的 n 值和循环分配随机颜色给每个单元格。这适用于大约 2/3 的单元格,但是有随机方位角/仰角部分没有分配颜色,这可能表明 surf() 函数存在某种类型的问题。我认为球体的计数 n 仅为 20 可能会导致舍入误差,这将是造成这种情况的主要因素,但对于 50x50 单元球体也发现了比例相似的间隙。我最好的猜测是某种舍入精度错误导致给定的单元格区域跳过分配颜色,因为给定的边界与实际单元格相差太远,本质上是给定的边界在两条线之间。对于给定的大小为 n 的球体,如何根据方位角和仰角范围遍历每个单元格?



n = 20;

%// Change your ranges here
minAzimuth = -180;
maxAzimuth = 180;
minElevation = 0;
maxElevation = 180;

%// Compute angles - assuming that you have already run the code for sphere
[x,y,z] = sphere(n);
theta = acosd(z);
phi = atan2d(y, x);

%%%%%// Begin highlighting logic
ind = (phi >= minAzimuth & phi <= maxAzimuth) & ...
(theta >= minElevation & theta <= maxElevation); % // Find those indices
x2 = x; y2 = y; z2 = z; %// Make a copy of the sphere co-ordinates
x2(~ind) = NaN; y2(~ind) = NaN; z2(~ind) = NaN; %// Set those out of bounds to NaN

%%%%%// Draw our original sphere and then the region we want on top
r = 1;
surf(r.*x,r.*y,r.*z,'FaceColor', 'white', 'FaceAlpha',0); %// Base sphere
hold on;
%surf(r.*x2,r.*y2,r.*z2,'FaceColor','red', 'FaceAlpha',0.5); %// Highlighted portion
%// Adjust viewing angle for better view



for countAz = 1:1:n
current_minAzimuth = -180 + (countAz-1) * (360/n);
current_maxAzimuth = -180 + (countAz) * (360/n);
for countE = 1:1:n
current_minElevation = 0 + (countE-1) * (180/n);
current_maxElevation = 0 + (countE) * (180/n);

theta = acosd(z);
phi = atan2d(y, x);

%%%%%// Begin highlighting logic
ind = (phi >= current_minAzimuth & phi <= current_maxAzimuth) & ...
(theta >= current_minElevation & theta <= current_maxElevation); % // Find those indices
x2 = x; y2 = y; z2 = z; %// Make a copy of the sphere co-ordinates
x2(~ind) = NaN; y2(~ind) = NaN; z2(~ind) = NaN;

random_color = rand(1,3);

surf(r.*x2,r.*y2,r.*z2,'FaceColor',random_color, 'FaceAlpha',1);

end

end

axis equal;
view(40,40);


这是一个 n=50 的球体: n=50 sphere

这是一个 n=20 的球体

n=20 sphere

最佳答案

您进行的球坐标/笛卡尔坐标之间的转换比您需要的多,随后进行的浮点比较也比需要的多。

您实际上只是在遍历 xyz 数组中的所有 2x2 索引 block 。

直接创建您尝试使用的索引数组然后提取现有表面坐标的这些值会更简单。

for countAz = 1:1:n
for countE = 1:1:n
% Linear index for a 2x2 patch of the matrices
idx = countAz + [0,1,n+(1:2)] + (countE-1)*(n+1);
% Pull out the coordinates, reshape for surf
x2 = x(idx); x2 = reshape(x2,2,2);
y2 = y(idx); y2 = reshape(y2,2,2);
z2 = z(idx); z2 = reshape(z2,2,2);

random_color = rand(1,3);
surf(r*x2,r*y2,r*z2,'FaceColor',random_color, 'FaceAlpha',1);
end
end

surface

关于matlab - 球体的着色部分,一些区域以未分配的颜色结束,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/74526309/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com