# how can I sweep a triangle area using two loops

1 view (last 30 days)
mohammad mortezaie on 20 Jun 2021
Edited: Scott MacKenzie on 20 Jun 2021
Hello,
I want to sweep all points inside a triangle by coordinates (0,0), (2*pi/(3*a),0) and (0,4*pi/(3*a)) and by deviding this into two triangles and calculating line equations i wrote this code
for j=1:101
x=2*(j-1)*pi/(100*sqrt(3)*a);
for l=1:101
y=-(l-1)*x/(100*sqrt(3))+2*pi*(l-1)/(3*a*100);
for k=1:101
x=2*(k-1)*pi/(100*sqrt(3)*a);
for m=1:101
y=-(m-1)*x/(100*sqrt(3))+2*pi*(m-1)/(100*3*a)+2*pi/(3*a);
But this code doesn't cover some y points inside the triangle. What can i do for solving this problem?
mohammad mortezaie on 20 Jun 2021
this is triangle that I mean. Strip area is needed to be sweep or broom.

Scott MacKenzie on 20 Jun 2021
Edited: Scott MacKenzie on 20 Jun 2021
No need for loops. The tests are built in to the inpolygon function when you provide matrices as input. Assuming you want to find points inside the triangle, given a "sweep" grid of x-y test points (integers or reals), then I think this achieves what you are after:
a = 0.1;
xt = [0, 2*pi / (sqrt(3)*a), 0, 0];
yt = [0, 2*pi/(3*a), 4*pi/(3*a), 0];
% plot triangle with margin around edges
margin = 5;
x1 = round(min(xt)- margin);
x2 = round(max(xt)+ margin);
y1 = round(min(yt)- margin);
y2 = round(max(yt)+ margin);
axis([x1 x2 y1 y2]);
hold on;
plot(xt,yt);
% make a grid of points to find points inside and outside triangle
delta = 1; % granularity of grid (adjust as desired)
[X,Y] = ndgrid(x1:delta:x2,y1:delta:y2);
% test the points (logical matrix 'in' identifies points in/out)
in = inpolygon(X,Y,xt,yt);
% output number of points inside triangle
sum(sum(in))
% plot the points inside (blue) and outside (light blue)
plot(X(in),Y(in),'.b') % points inside
plot(X(~in),Y(~in),'.', 'color', [.7 .7 1]) % points outside mohammad mortezaie on 20 Jun 2021
Thank you so much