Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Coloring sectors in MATLAB

Tags:

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 points 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
like image 697
Maxwell Avatar asked Feb 25 '26 00:02

Maxwell


1 Answers

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
like image 83
Jonas Avatar answered Feb 26 '26 13:02

Jonas



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!