how can I sweep a triangle area using two loops

2 ビュー (過去 30 日間)
mohammad mortezaie
mohammad mortezaie 2021 年 6 月 20 日
編集済み: Scott MacKenzie 2021 年 6 月 20 日
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?
  3 件のコメント
mohammad mortezaie
mohammad mortezaie 2021 年 6 月 20 日
This is code:
acc=1.42;
a=sqrt(3)*acc;
c=3.3;
gama0=3.16;
gama1=0.39;
gama2=-0.020;
gama3=0.315;
gama4=0.044;
gama5=-0.04;
V=0.;
iform=complex(0.0,1.0);
iden= eye([6 6]);
delta=0.0015;
eta=0.05;
for i=1:201
e=-0.5+4*(i-1)*delta;
Density=0;
for j=1:101
k_x=2*(j-1)*pi/(100*sqrt(3)*a);
for l=1:101
k_y=-(l-1)*k_x/(100*sqrt(3))+2*pi*(l-1)/(3*a*100);
f0=exp(-iform*acc*k_x)+2*cos(sqrt(3)*k_y*acc)*exp(iform*k_x*acc);
f1=exp(iform*k_x*c);
f3=f0*exp(iform*c*k_x);
H6=[-V gama0*f0 gama4*f3 gama3*f3 gama2*f1 0;
gama0*conj(f0) -V gama1*f1 gama4*f3 0 gama5*f1;
gama4*conj(f3) gama1*conj(f1) 0 gama0*f0 gama4*conj(f3) gama1*f1;
o vv gama3*conj(f3) gama4*conj(f3) gama0*conj(f0) 0 gama3*conj(f3) gama4*conj(f3);
gama2*conj(f1) 0 gama4*f3 gama3*f3 V gama0*f0 ;
0 gama5*conj(f1) gama1*conj(f1) gama4*f3 gama0*conj(f0) V];
G=(e*iden+eta*iform*iden)-eig(H6).*iden;
Green=inv(G);
Den=trace(Green);
Density=Density-imag(Den)/pi;
end
end
for k=1:101
k_x=2*(k-1)*pi/(100*sqrt(3)*a);
for m=1:101
k_y=-(m-1)*k_x/(100*sqrt(3))+2*pi*(m-1)/(100*3*a)+2*pi/(3*a);
f0=exp(-iform*acc*k_x)+2*cos(sqrt(3)*k_y*acc)*exp(iform*k_x*acc);
f1=exp(iform*k_x*c);
f3=f0*exp(iform*c*k_x);
H6=[-V gama0*f0 gama4*f3 gama3*f3 gama2*f1 0;
gama0*conj(f0) -V gama1*f1 gama4*f3 0 gama5*f1;
gama4*conj(f3) gama1*conj(f1) 0 gama0*f0 gama4*conj(f3) gama1*f1;
gama3*conj(f3) gama4*conj(f3) gama0*conj(f0) 0 gama3*conj(f3) gama4*conj(f3);
gama2*conj(f1) 0 gama4*f3 gama3*f3 V gama0*f0 ;
0 gama5*conj(f1) gama1*conj(f1) gama4*f3 gama0*conj(f0) V];
G=(e*iden+eta*iform*iden)-eig(H6).*iden;
Green=inv(G);
Den=trace(Green);
Density=Density-imag(Den)/pi;
end
end
i
T(i,1)=e;
T(i,2)=Density/8000;
end
plot(T(:,1),T(:,2))
Yes from points i mean (x,y) coordinate.
mohammad mortezaie
mohammad mortezaie 2021 年 6 月 20 日
this is triangle that I mean. Strip area is needed to be sweep or broom.

サインインしてコメントする。

回答 (1 件)

Scott MacKenzie
Scott MacKenzie 2021 年 6 月 20 日
編集済み: Scott MacKenzie 2021 年 6 月 20 日
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:
% your triangle points
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
  4 件のコメント
Scott MacKenzie
Scott MacKenzie 2021 年 6 月 20 日
編集済み: Scott MacKenzie 2021 年 6 月 20 日
I'm not sure what you mean by "all points" or "all appropriate y coordinates". The number of points inside the triangle is infinite. I included a variable delta for setting up ndgrid. The lower the value of delta the greater the number of points inside the triangle. Some stats here are...
Delta Number of Points inside triangle
1 781
0.1 76186
0.01 7599720
mohammad mortezaie
mohammad mortezaie 2021 年 6 月 20 日
Thank you so much

サインインしてコメントする。

カテゴリ

Help Center および File ExchangeCreating and Concatenating Matrices についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by