### Tags: circles, create, diagram, draws, matlab, moving, multiple, particles, plot3, plotting, programming, reallygive, simulation, spheres

# plotting multiple 3D spheres in the same diagram

On Programmer » Matlab

4,970 words with 2 Comments; publish: Tue, 06 May 2008 21:07:00 GMT; (20093.75, « »)

I am trying to create a simulation of particles moving in 3D. I've

been using plot3 but this only draws circles, which does not really

give an accurate 3D picture because there is no perspective (points

"far away" are drawn the same size as "closer" ones). Ideally I'd

like to plot each particle as a sphere at position (x,y,z) to get

some depth perception and also so that the particles increase in size

as you zoom in. I know that matlab can draw individual spheres but

can this be extended to plot many spheres in the same diagram?

Thanks

*http://matlab.questionfor.info/q_matlab_46024.html*

All Comments

Leave a comment...

- 2 Comments
- Fiona Goddard wrote:
>

> I am trying to create a simulation of particles moving in 3D. I've

> been using plot3 but this only draws circles, which does not really

> give an accurate 3D picture because there is no perspective (points

> "far away" are drawn the same size as "closer" ones). Ideally I'd

> like to plot each particle as a sphere at position (x,y,z) to get

> some depth perception and also so that the particles increase in

> size

> as you zoom in. I know that matlab can draw individual spheres but

> can this be extended to plot many spheres in the same diagram?

> Thanks

Fiona;

I was playing with code (below) to spatially visualize data in 3D

space a while back, and it sounds like something you might find

usefull. I chose cubes instead of spheres because they are much

easier to deal with, primarily because there is much less rendering

effort needed for cubes. If you are not looking for 'real-time'

rendering, spheres should work as well.

What the code does: Essentially, it puts a cube at each point in

space, then colors the cube according to the data value at that point

in space. You could play with the cube size (vs. color), I suppose.

Here's the code snippet FWIW.

- Ted

% Create some data:

tempMeas(10,10,10) = 0;

for i = 1:10

for j = 1:10

for k = 1:10

tempMeas(i,j,k) = i + j + k + 10*rand;

end

end

end

x_min = 1;

y_min = 1;

z_min = 1;

x_max = 10;

y_max = 10;

z_max = 10;

t_min = min(min(min(tempMeas)));

t_max = max(max(max(tempMeas)));

t_range = t_max - t_min;

% Define cube size

size_cube = 0.2; % could be based on X Y or Z range(s)

% Get a figure going...

view(3)

axis([0 10 0 10 0 10])

lighting phong

for i = 1:10 % 10 ft(m)

for j = 1:10 %10 ft(m)

for k = 1:10

% Get a coord/measurement from data

x_pos = i;

y_pos = j;

z_pos = k;

temp = tempMeas(i,j,k);

% Figure out the color ...

norm_color = (temp - t_min)/t_range;

if norm_color < 0.25

r_val = 0;

b_val = 1;

g_val = (norm_color)/0.25;

elseif norm_color < 0.50 && norm_color >= 0.25

r_val = 0;

b_val = 1-(norm_color-0.25)/0.25;;

g_val = 1;

elseif norm_color < 0.75 && norm_color >= 0.50

r_val = (norm_color-0.50)/0.25;

b_val = 0;

g_val = 1;

else

r_val = 1;

b_val = 0;

g_val = 1- (norm_color-0.75)/0.25;

end

vertex_matrix = [x_pos-size_cube/2 y_pos-size_cube/2

z_pos-size_cube/2;

x_pos+size_cube/2 y_pos-size_cube/2

z_pos-size_cube/2;

x_pos+size_cube/2 y_pos+size_cube/2

z_pos-size_cube/2;

x_pos-size_cube/2 y_pos+size_cube/2

z_pos-size_cube/2;

x_pos-size_cube/2 y_pos-size_cube/2

z_pos+size_cube/2;

x_pos+size_cube/2 y_pos-size_cube/2

z_pos+size_cube/2;

x_pos+size_cube/2 y_pos+size_cube/2

z_pos+size_cube/2;

x_pos-size_cube/2 y_pos+size_cube/2

z_pos+size_cube/2];

faces_matrix = [1 2 6 5;

2 3 7 6;

3 4 8 7;

4 1 5 8;

1 2 3 4;

5 6 7 8];

patch('Vertices',vertex_matrix,'Faces',f

aces_matrix,...

'FaceVertexCData',[r_val g_val

b_val],'FaceColor','flat',...

'EdgeColor',[r_val g_val b_val]);

end

end

end

#1; Tue, 06 May 2008 21:08:00 GMT

- Fiona Goddard wrote:
- Fiona Goddard wrote:
>

> I am trying to create a simulation of particles moving in 3D. I've

> been using plot3 but this only draws circles, which does not really

> give an accurate 3D picture because there is no perspective (points

> "far away" are drawn the same size as "closer" ones). Ideally I'd

> like to plot each particle as a sphere at position (x,y,z) to get

> some depth perception and also so that the particles increase in

> size

> as you zoom in. I know that matlab can draw individual spheres but

> can this be extended to plot many spheres in the same diagram?

> Thanks

Take a look at bubbleplot3 in the file exchange, its available here:

<http://www.mathworks.com/matlabcent...bjectId=1093919>

HTH

PB

#2; Tue, 06 May 2008 21:09:00 GMT

- Fiona Goddard wrote: