MATLAB Answers

Plotting zigzag in a 2D contour

16 views (last 30 days)
Gary
Gary on 16 Oct 2014
Commented: Kelly Kearney on 15 Dec 2020
Hello,as title, I have a 2D contour. Now I want to plot a zigzag geometry to infill the contour.But I have no idea how to do it. Can anyone give a hint? Thanks.

  1 Comment

Jayaprakash P
Jayaprakash P on 20 Nov 2020
Hi Kelly,
I am also working on the same problem for additive manufacturing applications. I have used the lineinpolygon.m, inpolygons.m, bufferm2, contourcs funtions. But could not succeed. Could you please help me for the same.

Sign in to comment.

Answers (3)

Kelly Kearney
Kelly Kearney on 17 Oct 2014
Edited: Kelly Kearney on 17 Oct 2014
Hmm, more fun.
Not exactly an out-of-the-box solution... I dug into my own toolbox of polygon stuff for this one, and that in turn uses a bunch of Mapping Toolbox polygon functions. But if you go fetch all of that ( lineinpolygon.m, inpolygons.m, bufferm2, contourcs ), this should work:
% Create a contour
[x,y,z] = peaks(100);
C = contourcs(x(1,:),y(:,1),z, [2 2]);
xc = C(1).X;
yc = C(1).Y;
% Diagonal lines
dx = 0.1;
dy = 0.1;
nl = 60; % Could probably calculate this, but I'm lazy
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];
% Zigzag the lines
[xc, yc] = poly2cw(xc, yc);
[xb, yb] = bufferm2('xy', xc, yc, 0.1, 'in'); % If you want some space between zigzag and edge
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');

  3 Comments

Jayaprakash P
Jayaprakash P on 20 Nov 2020
Hi Kelly,
I am also working on the same problem for additive manufacturing applications. I have used the lineinpolygon.m, inpolygons.m, bufferm2, contourcs funtions. But could not succeed. Could you please help me for the same.
Jayaprakash P
Jayaprakash P on 20 Nov 2020
Could you please share your Matlab code.
Kelly Kearney
Kelly Kearney on 15 Dec 2020
Those subfunctions can now be found under my GitHub: lineinpolygon, inpolygons, bufferm2 (and contourcs, by Kesh Ikuma, is on the FEX). This answer is a bit outdated now -- the newer polyshape objects might be a better way to go -- but this example will probably still run.

Sign in to comment.


Kelly Kearney
Kelly Kearney on 16 Oct 2014
You mean hatching, or similar? If so, there are several entries on the FEX that do that. An overview of a few of them can be found in this blog entry.

  0 Comments

Sign in to comment.


Gary
Gary on 16 Oct 2014
Edited: Gary on 16 Oct 2014
More like this.

  1 Comment

Adhirath Naruka
Adhirath Naruka on 26 Nov 2020
Evening mam, I am a college student and me and my group have been working on a similar problem however we are not able to integrate the given code with our contour plotting. Any help on the matter will be greatly appreciated.

Sign in to comment.

Tags

Community Treasure Hunt

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

Start Hunting!