Home > Back-end >  Sphere region coloring based on azimuth and elevation input, not index
Sphere region coloring based on azimuth and elevation input, not index

Time:11-23

I am trying to create a spherical histogram, but my data points are latitude and longitude values so I need to be able to color a given cell region of a sphere based on its position, rather than just randomly going through indices. I have received a similar answer to my problem of certain azimuth and elevations being skipped here: enter image description here

Here is the previous solution code for reference:

n = 20;
[x,y,z] = sphere(n);

r = 1;
surf(r.*x,r.*y,r.*z,'FaceColor', 'white', 'FaceAlpha',0); %// Base sphere
hold on;

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; 
        % 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

Using the solution code from the linked post and holding the countAz to be constant and only the elevation to change, results in this:

enter image description here

And vice versa, holding the CountE constant and only allowing azimuth to change: enter image description here

It seems only allowing the azimuth to change and holding elevation constant instead has the opposite effect and goes through to color every elevation level of a given azmiuth index, and when holding the azimuth constant, there is a spiral effect even though theoretically the azimuth should not be changing.

Is there a way of going through the elevation and azimuth ranges in a more systematic way that doesn't result in cells being left blank? Being able to loop through one elevation range for every cell bounded by two azimuth values, and then continuing on down to the next elevation level is what I have in mind, but currently the index system just seems to not follow the expected order causing the spiraling or changing the elevation when it should be constant, making it hard to assign inputted latitude and longitude values to corresponding azimuth and elevation values for each inner for loop.

CodePudding user response:

Thats because the answer from your other post has the linear indices idx wrong. (EDIT: not anymore, its been fixed)

I could try to fix that, but as you yourself don't seem to understand what they do (understandably so) I think this is the case where a more verbose yet clear code is of benefit. I hope with this piece of code you understand what those indices were trying to do:

n = 20;
[x,y,z] = sphere(n);

r = 1;
surf(r.*x,r.*y,r.*z,'FaceColor', 'white', 'FaceAlpha',0); %// Base sphere
hold on;

for countAz = 1:1:n
    for countE = 1:1:n
        % Get a 2x2 patch of points. Each row of the points is an elevation. Points are the intersection of the black grid.
        x2=[x(countE,countAz),x(countE,countAz 1),x(countE 1,countAz),x(countE 1,countAz 1)];
        y2=[y(countE,countAz),y(countE,countAz 1),y(countE 1,countAz),y(countE 1,countAz 1)];
        z2=[z(countE,countAz),z(countE,countAz 1),z(countE 1,countAz),z(countE 1,countAz 1)];
        x2 = reshape(x2,2,2);
        y2 = reshape(y2,2,2);
        z2 = reshape(z2,2,2);
                        
        random_color = rand(1,3);
        surf(r*x2,r*y2,r*z2,'FaceColor',random_color, 'FaceAlpha',1);
    end
end
  • Related