Change half of my plotted shape a different color
10 views (last 30 days)
Show older comments
I made a star in mat lab using the corditnates
x = [0 7 4 10 3 0 -3 -10 -4 -7];
y = [0 -5 4 9 9 16 9 9 4 -5];
I then have it filled in, but is there a way to have the upper and lower halfs of the star be diferent colors. Right now i have it one solid color using the fill method.
Answers (1)
Voss
on 8 Feb 2023
x = [0 7 4 10 3 0 -3 -10 -4 -7];
y = [0 -5 4 9 9 16 9 9 4 -5];
I shift the order of the points in x and y so the top part of the star is first and the bottom part is second:
x = circshift(x,-3)
y = circshift(y,-3)
(Of course, you could just redefine x and y:)
% x = [10 3 0 -3 -10 -4 -7 0 7 4];
% y = [ 9 9 16 9 9 4 -5 0 -5 4];
Define some "halfway" y-coordinate where the color will change:
y_half = 5.5; % change this to change where the "halfway" point is (anywhere from 4 to 9 will work)
Figure out the x-coordinates of the two points along the star whose y-coordinate is y_half:
x_half_left = interp1(y([end 1]),x([end 1]),y_half)
x_half_right = interp1(y(end/2+[0 1]),x(end/2+[0 1]),y_half)
Define two new pairs of vectors defining the top and bottom parts:
y_top = [y_half y(1:end/2) y_half];
y_bottom = [y_half y(end/2+1:end) y_half];
x_top = [x_half_left x(1:end/2) x_half_right];
x_bottom = [x_half_right x(end/2+1:end) x_half_left];
Create the patches (you could also create one patch with two faces if you want):
patch(x_top,y_top,'r')
patch(x_bottom,y_bottom,'g')
0 Comments
See Also
Categories
Find more on Polygons 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!