2010-11-19 118 views
3

我正在嘗試使彈起的球彈起,但創建一個基本的多色球時遇到了困難,然後我可以在每個框架中作爲一個整體進行旋轉。我在球的圓周上有512個點,分成8個扇區,每個扇區都有獨立的顏色。到目前爲止,我有2個矩陣是8x64,代表沿球的圓周點的x和y座標,每一行都是它自己的扇區。在MATLAB中着色扇區

我想知道如何在圓上填充這些「範圍」,使它看起來像一個沙灘球,創建一個函數以兩個x和y座標矩陣作爲輸入。您的幫助將不勝感激!

基本骨架功能:

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

end 

回答

0

的功能可以通過轉換(X,Y)開始座標系(笛卡爾)成極座標系統中,其中每個點的角度是可用的。相關的MATLAB函數是cart2pol

tranforming極性後,你可以沿着你想創建一個PATCHdraw_ballfloor(polar_anle_in_radians/(2*pi)*8)

2

行使用地板的點分成8個扇區......東西。要做到這一點最好的方法是要求將數據存儲爲面和頂點,但如果您想保留8xN陣列,則可以創建8個用於描述球的補丁。

這樣一來,你的函數應該是這樣的:

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