开发者

Coloring sectors in MATLAB

I am trying to animate a ball bouncing but having trouble creating a basic multicolor ball i can then rotate as a whole in each frame. I have 512 points on the circumference of the ball split into 8 sectors, each a separate color. So far I have 2 matrices that are 8x64, representing the x and the y coordinates of p开发者_如何学JAVAoints along the circumference of the ball, each of the rows being the its own sector.

I want to know how to fill these "ranges" along the circle such that it will look like a beach ball, creating a function to do this with the two x and y coordinate matrices as inputs. Your help would be greatly appreciated!

Basic skeleton function:

% Expects 8xN x and y point matrices
function draw_ball(x,y)
% Draw the 8 sectors filling them with unique colors

end


You want to create a PATCH with draw_ball. The nicest way to do this would require you to have the data stored as faces and vertices, but you if you like to keep your 8xN arrays, you can instead create 8 patches that describe the ball.

This way, your function would look like this:

function pH = drawBall(x,y)

%# count sectors
nSectors = size(x,1);

%# create a colormap
ballColors = jet(nSectors);

%# set hold-state of current axes to 'on'
set(gca,'nextPlot','add')

%# initialize array of plot handles
pH = zeros(nSectors,1);

%# add [0,0] to every sector
x = [x,zeros(nSectors,1)];
y = [y,zeros(nSectors,1)];

%# plot patches
for s = 1:nSectors
   %# plot sectors with black lines. If there shouldn't be lines, put 'none' instead of 'k' 
   pH(s) = patch(x(s,:),y(s,:),ballColors(s,:),'EdgeColor','k');
end


The function could begin by transforming the (x,y) coordinate system (Cartesian) into the polar coordinate system, where an angle of each point is available. The associated matlab function is cart2pol

After tranforming to polar, you could use floor to split the points into 8 sectors... something along the line of floor(polar_anle_in_radians/(2*pi)*8)

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜