Filling area with pattern
63 views (last 30 days)
Show older comments
Hello all,
I would like to know the how to fill the area with some pattern [CrossedLines, DiagonalLines, FDiagonalLines, HorizontalLines, Solid, VerticalLines, or XCrossedLines]. I already use this code to fill the area with color:
a=[1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24]
sh2=stairs(1:24,a, '*','color',[0 1 0],'MarkerEdgeColor','r', 'MarkerSize',4,'LineWidth',1.3);
x = [sh2.XData(1),repelem(sh2.XData(2:end),2)]; % Although it works fine for me but I don't what is [XData(2:end),2] means, it would be good if you explain a bit.
y = [repelem(sh2.YData(1:end-1),2),sh2.YData(end)];
viu=fill([x,fliplr(x)],[y,bottom*ones(size(y))],[0 1 0])
viu.FaceAlpha = 0.2;
0 Comments
Accepted Answer
Adam Danz
on 10 Dec 2019
Edited: Adam Danz
on 11 Dec 2019
Hi shane, filling with a pattern isn't trivial so I recommend the functions listed in the file exchange. However, here's a fairly easy way to add a polka dot pattern to the area being filled.
The coordinates used to fill form a polygon. This makes an array of random dots that span the size of your polygon and then it removes any dots that are outside of the polygon.
See inline comments for details.
% Here are the coordinates of your polygon used in fill()
polyX = [x,fliplr(x)];
polyY = [y,bottom*ones(size(y))];
viu=fill(polyX,polyY,[0 1 0])
viu.FaceAlpha = 0.2;
% Create array of polkadot coordinates that span the size of the polygon
[dotsX,dotsY] = ndgrid(...
min(polyX): 1 : max(polyX), ...
min(polyY): 1 : max(polyY));
% Determine which coordinates are on or in your polygon
[in,on] = inpolygon(dotsX,dotsY,polyX,polyY);
% extract coordinates in or on polygon
inX = dotsX(in|on);
inY = dotsY(in|on);
% Plot polkadots
hold on
h = plot(inX,inY,'k.','MarkerSize',4,'MarkerFaceColor','k');
4 Comments
Adam Danz
on 11 Dec 2019
In this line from my code below, the middle value determines the interval between dots along the x axis and y axis
[dotsX,dotsY] = ndgrid(...
min(polyX): 1 : max(polyX), ... % for x axis
min(polyY): 1 : max(polyY)); % for y axis
%...............^
You could adjust that number for each axis.
Alternatively you could try something like this below where the last input to linspace() determines the number of dots along the x and y axes.
[dotsX,dotsY] = ndgrid(...
linspace(min(polyX), max(polyX), 10), ... % for x axis
linspace(min(polyY), max(polyY), 10); % for y axis
%.....................................^
More Answers (0)
See Also
Categories
Find more on Graphics Object Programming 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!