patches and overlapping alpha values

2 views (last 30 days)
Matthew
Matthew on 1 Jun 2012
Answered: Ayush on 19 Jun 2025
Hello,
I wrote code that creates two patch objects which overlap each other. Basically, two circles, one (circle A) at the center of the other (circle B). Both have a face value of .35. Circle A is red and circle B is blue. However, when I set’ facealpha’ property while using the patch command, the colors add up, meaning that circle B is the alpha blue that I want, but circle A is an alpha purple (the alpha red and alpha blue added together). Is there a way to get circle A looking red again? My original plan is to "cut out" circle A then patch circle B, followed by patching circle A.
  9 Comments
Ryan
Ryan on 5 Jun 2012
The blue element is being placed on top of the other two elements. if you view just the blue circle and the non-transparent white circle, you'll note it still has a blue hue. I am unsure of how to order patches or if it is even possible.
Matthew
Matthew on 6 Jun 2012
I ended up find a color that would simulate the light blue and red I was looking for, then plotted grid lines using Xtick and Ytick to get the effect I was looking for. Thanks to everyone for their advice and help.

Sign in to comment.

Answers (1)

Ayush
Ayush on 19 Jun 2025
Hi Matthew,
When two semi-transparent patches overlap, the colors blend according to the alpha compositing rules. In your case, resulting in purple where the red (Circle A) overlaps with the blue (Circle B).
To keep Circle A red (without the purple blending effect), you'll need to make sure that Circle B does not draw underneath Circle A at all, which means effectively cutting a hole in Circle B.
Here's how you can do it in MATLAB:
You can use polygon clipping to subtract the area of Circle A from Circle B before patching them. This creates a donut-shaped Circle B that surrounds but does not underlie Circle A.
Here is a MATLAB code for your reference:
r1 = 1; % Radius of circle A (inner red)
r2 = 2; % Radius of circle B (outer blue)
center = [0, 0];
theta = linspace(0, 2*pi, 100);
% Define circle A (red) and circle B (blue)
xA = center(1) + r1 * cos(theta);
yA = center(2) + r1 * sin(theta);
polyA = polyshape(xA, yA);
xB = center(1) + r2 * cos(theta);
yB = center(2) + r2 * sin(theta);
polyB = polyshape(xB, yB);
% Now, subtract A from B to make a ring (annulus)
ring = subtract(polyB, polyA);
% Now, plot the ring (circle B with a hole)
p1 = plot(ring, 'FaceColor', 'blue', 'FaceAlpha', 0.35);
hold on;
% finally, plot circle A on top
p2 = plot(polyA, 'FaceColor', 'red', 'FaceAlpha', 0.35);
axis equal;
You can read more about "polyshape" function here:https://www.mathworks.com/help/matlab/ref/polyshape.html
Hope it helps!

Categories

Find more on Graphics Performance in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!