matlab 对球体的截面着色,某些区域以未指定的颜色结束

ax6ht2ek  于 2022-11-24  发布在  Matlab
关注(0)|答案(1)|浏览(288)

我试图创建一个球形直方图,我发现了一个插件,创建一个球形直方图,但它使用等面积四边形,并为我的目的,我想保留线上发现的传统球创建MATLAB,使他们可以匹配纬度和经度线。
我找到了一种方法,通过设置要着色区域的最小/最大方位角和仰角值,来给球体的给定子集着色。为了尝试和测试给球体的每个单元着色,我尝试将方位角和仰角的总Angular 除以给定的n值,然后循环为每个单元分配随机颜色。这对大约2/3的单元有效。但是,存在未指定颜色的随机方位角/高程剖面,表示曲面存在某种类型的问题()函数。我认为球体的计数n只有20可能会导致舍入误差,这将是主要的影响因素,但是对于50 x50的细胞球体也发现了成比例的类似大小的间隙。我最好的猜测是某种舍入精度误差,由于给定的边界与实际细胞匹配太远,导致给定的细胞区域跳过分配颜色,本质上,给定的边界在两条线之间。对于给定的大小为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=20的球体

7eumitmz

7eumitmz1#

您在球坐标/笛卡尔坐标之间进行的转换比所需的要多,随后进行的浮点比较也比所需的要多。
实际上,您只是在xyz数组中的所有2x2索引块中循环。
更简单的方法是直接创建要使用的索引数组,然后提取现有表面坐标的值。

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

相关问题