problem with patch fill color

Below is a small excerpt of a function I wrote to display a ring sector with patch.
However, when I call it like follows: "draw_ring_sector(5, 7, 0, 70, [1 0 0])", the ring boundaries look OK, but the fill color does not follow the inner ring, giving the following result, which is clearly not what I want. Is this a known issue? Is it a bug, or am I overlooking something obvious?
function draw_ring_sector(r_min, r_max, el_min, el_max, line_color)
%This function draws a ring sector
%create the inner and outer line coordinates
theta1 = linspace(el_min*pi/180, el_max*pi/180);
x_inner = r_min*cos(theta1);
y_inner = r_min*sin(theta1);
theta2 = linspace(el_max*pi/180, el_min*pi/180);
x_outer = r_max*cos(theta2);
y_outer = r_max*sin(theta2);
%create the lower and upper line coordinates
lower_line_x = linspace(x_inner(1), x_outer(end));
lower_line_y = linspace(y_inner(1), y_outer(end));
upper_line_x = linspace(x_inner(end), x_outer(1));
upper_line_y = linspace(y_inner(end), y_outer(1));
%put it all together and display with patch
x = [x_inner upper_line_x x_outer lower_line_x];
y = [y_inner upper_line_y y_outer lower_line_y];
vert = [x; y]';
fac = cumsum(ones(1, length(x)));
patch('Faces', fac, 'Vertices', vert, 'FaceColor', line_color);
axis equal
end

Answers (1)

Stephen23
Stephen23 on 27 Apr 2026 at 10:42
Edited: Stephen23 on 27 Apr 2026 at 10:45
After x_outer ends at r_max·cos(el_min), your lower_line_x starts at x_inner(1) = r_min·cos(el_min), creating an implicit jump across the gap, with the line itself going in the wrong direction (inner -> outer instead of outer -> inner). MATLAB's patch then implicitly closes the polygon from lower_line_x(end) == x_outer(end)) back to x_inner(1), so the lower radial boundary is effectively traced twice in opposite directions, creating a degenerate self-intersecting polygon that confuses the scanline fill algorithm.
Solution: change these lines:
lower_line_x = linspace(x_inner(1), x_outer(end));
lower_line_y = linspace(y_inner(1), y_outer(end));
to these:
lower_line_x = linspace(x_outer(end), x_inner(1));
lower_line_y = linspace(y_outer(end), y_inner(1));
You can simplify the whole function considerably:
draw_ring_sector(5, 7, 0, 70, [1,0,0])
function draw_ring_sector(r_min, r_max, el_min, el_max, line_color)
theta1 = linspace(el_min*pi/180, el_max*pi/180);
theta2 = linspace(el_max*pi/180, el_min*pi/180);
x = [r_min*cos(theta1), r_max*cos(theta2)];
y = [r_min*sin(theta1), r_max*sin(theta2)];
patch(x, y, line_color);
axis equal;
end

2 Comments

Harm
Harm on 27 Apr 2026 at 10:58
OK, thanks a lot. I was wondering about something like that, but apparently overlooked the obvious.
Stephen23
Stephen23 on 27 Apr 2026 at 14:09
@Harm: I hope that it helped! Please remember to accept the answer if it helped you :)

Sign in to comment.

Products

Release

R2023b

Asked:

on 27 Apr 2026 at 10:21

Commented:

on 27 Apr 2026 at 14:09

Community Treasure Hunt

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

Start Hunting!