How do I change the colour in each sector depending on the angle?

2 views (last 30 days)
I created 6 sectors via linspace and patch function. However I can't figure out how to fill in each sector depending on the heading. e.g from sample code, when heading 90 degree that sector should be '5' or yellow, the other sectors towards the left/right should be getting more and more 'blue' or towards the value '1'. It will be great if I can define the starting value for each sector or reducing as it move towards the left/right of the heading.
Many thanks for the help in advance.
x0=0
y0=0
r=5
angleDEG=90
angleRAD=angleDEG/180*pi
HSpread=45
HSpreadRAD=HSpread/180*pi
t = linspace(angleRAD-HSpreadRAD,angleRAD+HSpreadRAD,6); %(begining angle, end angle, resolution)
x = [x0 (x0+r*cos(t)) x0];
y = [y0 (y0+r*sin(t)) y0];
c = linspace(1,5,length(y));
figure;
a=patch(x,y,c)
a.FaceAlpha = 0.3;
colorbar
axis equal
Currently this is what I got (pic insert):

Accepted Answer

DGM
DGM on 3 Aug 2021
Edited: DGM on 3 Aug 2021
I don't know about how to wrangle patch() to do it right. It probably can, but I don't have that much hair to spare. This is a version using surf() instead. Anyone is free to trump this with a solution using patch().
% parameters
x0 = 0;
y0 = 0;
rmax = 5;
th0 = 45; % start angle
thInc = 90; % included angle
rsteps = 10;
thsteps = 6;
ang = [th0 th0+thInc]/180*pi;
r = linspace(0,rmax,rsteps);
t = linspace(ang(1),ang(2),thsteps).';
x = x0 + r.*cos(t);
y = y0 + r.*sin(t);
z = ones(thsteps,rsteps).*0; % just plot everything in x-y plane
c = repmat(8*(0.5-abs((t-min(ang))/range(ang)-0.5)) + 1,[1 rsteps]);
a = surf(x,y,z,c);
a.FaceAlpha = 0.5;
% do the border
xl = [x0 x0+rmax*cos(t.') x0];
yl = [y0 y0+rmax*sin(t.') y0];
line(xl,yl,'color','k')
shading interp
colorbar
caxis([1 5])
axis equal
view(2)
  6 Comments
DGM
DGM on 5 Aug 2021
I don't know. That looks pretty good to me. If it works well for your needs, I'd roll with it.
Julian Tan
Julian Tan on 5 Aug 2021
Thanks for the inital valuable insight. Learn great deal from your codes.
All is good excpet the angle need to be trimmed (equal angle)/2, due to the way the polygons were drawn and filled. The above can see 'tilted' slgihtly (equal angle)/2 to the rgiht. Once that is done, it is perfect.
AngleDEG_trim=(HSpreadDEG)/(thsteps-1)
NewAngleDEG_trim=angleDEG-AngleDEG_trim

Sign in to comment.

More Answers (0)

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!