Effacer les filtres
Effacer les filtres

How to adapt the color of points on a sphere if they are on frontside or backside of the sphere ?

3 vues (au cours des 30 derniers jours)
Hi,
I'm working on Matlab R2008a (without toolbox...)
i'd like to know how to change the color of points located at the surface of a sphere given that they are on the frontside or the backside of this sphere.
Usually, i've got several points which cover all the surface of the sphere. With "facealpha" i can see the points located on the backside of the sphere, but with the same color. So the distinction of points is difficult.
The better for me should be to have the "front" points in red and the "backside" points in bleu, and this for any arbitrary initial orientation of the sphere
Is that possible ? Thanks for your attention

Réponse acceptée

arich82
arich82 le 7 Mai 2014
Modifié(e) : arich82 le 7 Mai 2014
I don't remember which callback functions were available in R2008a, but the following should get you started. Essentially, it queries the current camera position of the plot, then takes the dot product of the vector pointing to each data point and the camera position vector. Since the camera position vector is pointing straight at you (and the dot product represents a projection of one vector onto another), any point with a positive dot product points towards you, and any with a negative points away.
Note that you'd have to call the fix_color portion of the script below every time you rotate the axes; it might be wise to implement it as a call back function, or using linked-data (if that's available in 2008a).
You might also consider using a similar approach to plot a tranparent patch cutting through the sphere; the plane could easily be defined by the point-normal equation, where the point would be the origin [0, 0, 0] and the normal would be the camera position vector.
Let me know if this helps.
n = 500;
% generate dummy data
% [x, y, z] = sphere(n); % can't remember if this is an old function...
r = ones(n, 1);
phi = pi*rand(n, 1);
theta = 2*pi*rand(n, 1);
x = r.*sin(phi).*cos(theta);
y = r.*sin(phi).*sin(theta);
z = r.*cos(phi);
hf = figure('WindowStyle', 'docked');
ha = axes;
hp = scatter3(x(:), y(:), z(:), 20, 'filled');
axis(ha, 'equal');
%function fix_color(hp)
% could save this in a separate file,
% or as callback
% might consider making the default input
% hp = get(gca, 'Children');
ha = get(hp, 'Parent');
pos = get(ha, 'CameraPosition');
x = get(hp, 'XData');
y = get(hp, 'YData');
z = get(hp, 'ZData');
% if dot product of data point and camera position vector is positive,
% data point lies on front half of sphere;
% otherwise, data point lies on back half
mask = [x(:), y(:), z(:)]*pos(:) > 0;
front_color = [1, 0, 0]; % red
back_color = [0, 0, 1]; % blue
c(mask, :) = front_color(ones(nnz(mask), 1), :); % use 'sum' if 'nnz' fails
c(~mask, :) = back_color(ones(nnz(~mask), 1), :);
set(hp, 'CData', c);
%end function fix_color

Plus de réponses (1)

Pierre-Yves
Pierre-Yves le 12 Mai 2014
Thank you arich82 ! I'm still trying to understand all the code lines but it works well. I'll try to adapt your code to mine. If i have some difficulties to do it i will ask you if you want.

Catégories

En savoir plus sur Data Distribution Plots dans Help Center et File Exchange

Tags

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by