[x,y,z] = peaks(100);
C = contourcs(x(1,:),y(:,1),z, [2 2]);
xc = C(1).X;
yc = C(1).Y;
dx = 0.1;
dy = 0.1;
nl = 60;
xe = floor(min(xc)./dx)*dx + (0:(nl-1))*dx;
ye = sort(ceil(max(yc)./dy)*dy - (0:(nl-1))*dy);
x1 = xe;
y1 = ones(1,nl).*ye(end);
x2 = ones(1,nl).*xe(1);
y2 = ye(end:-1:1);
xl = [x1; x2];
yl = [y1; y2];
[xc, yc] = poly2cw(xc, yc);
[xb, yb] = bufferm2('xy', xc, yc, 0.1, 'in');
seg = zeros(0,2);
dirr = true;
for ii = 1:nl
[isin, inseg] = lineinpolygon(xl(1,ii), yl(1,ii), ...
xl(2,ii), yl(2,ii), xb, yb);
if isin
if dirr
seg = [seg; inseg(1:end-1,:)];
else
seg = [seg; inseg(end-1:-1:1,:)];
end
dirr = ~dirr;
end
end
plot(xc, yc, 'r', seg(:,1), seg(:,2), 'b');
1 Comment
Direct link to this comment
https://in.mathworks.com/matlabcentral/answers/158900-plotting-zigzag-in-a-2d-contour#comment_1148168
Direct link to this comment
https://in.mathworks.com/matlabcentral/answers/158900-plotting-zigzag-in-a-2d-contour#comment_1148168
Sign in to comment.