patches and overlapping alpha values
2 次查看(过去 30 天)
显示 更早的评论
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 个评论
Ryan
2012-6-5
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.
回答(1 个)
Ayush
2025-6-19
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!
0 个评论
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Graphics Performance 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!