2017-02-06 4 views
0

Je veux dessiner un cercle divisé en tiers et remplir chaque tiers avec une couleur RVB différente, dans MATLAB. J'ai commencé en dessinant un cercle et en remplissant tout d'une seule couleur. Ensuite, j'ai divisé ce cercle en 3 zones égales. Maintenant j'essaye de donner une couleur différente à chaque 'tranche' du cercle. Est-ce que quelqu'un peut m'aider?Tracer un cercle divisé en tiers avec des couleurs différentes each_ MATLAB

Merci d'avance.

Voici mon code:

% FilledCircle3 : function that takes 5 inputs 
% 
% Call the function: 
% FilledCircle3(x0,Row,Radius,N,Color) 
% 
% Inputs Types 
% ------------ 
% x0 - Integer, Float 
% y0 - Integer, Float 
% Radius - Integer, Float 
% N  - Integer 
% Color - Character String 
% 
% Notes on N: The more N increases, the more accurate is the circle 
%    The standard value for N is 256 
% 
% Notes on Color: 
% 'b'  blue   
% 'g'  green   
% 'r'  red   
% 'c'  cyan    
% 'm'  magenta  
% 'y'  yellow  



function []=FilledCircle3(x0,y0,Radius,N,Color) 

if(N<=1) 
    error('N must be greater than 1'); 
end 

if (Color ~='b') && (Color ~='g') && (Color ~= 'r') && (Color ~='c') && (Color ~='m') && (Color ~='y') 
    error('This is not an available color'); 
end 
hold on 
axis equal 
t=(0:N)*2*pi/N; %t=-pi:0.01:pi 
x=Radius*cos(t)+x0; 
y=Radius*sin(t)+y0; 
plot(x,y,Color, fill(x,y,Color)); 
hold on 

% Divide circle into 3 sectors 
n=3 
tet=linspace(-pi,pi,n+1) 
xi=r*cos(tet)+x0 
yi=r*sin(tet)+y0 
for k=1:numel(xi) 
plot([x0 xi(k)],[y0 yi(k)]) 
hold on 
end 

Répondre

0

Vous devez utiliser fill pour créer des formes remplies.

thetas = linspace(-pi, pi, n+1); 

% Specify any colors that you want 
colors = hsv(n); 

for k = 1:n 
    tt = linspace(thetas(k), thetas(k+1)); 
    xi = r * cos(tt) + x0; 
    yi = r * sin(tt) + y0; 

    fill([xi(:); x0], [yi(:); y0], colors(k,:)); 

    hold on 
end 
+0

Merci! Y at-il une raison pour laquelle vous avez choisi hsv colors @Suever? – Mraquel

+0

@Mraquel Les couleurs que j'ai choisies sont arbitraires. Vous pouvez utiliser toutes les couleurs que vous voulez. – Suever