フィルターのクリア

I need to call up a vertex from polyshape command

3 ビュー (過去 30 日間)
Bruce Griffin
Bruce Griffin 2024 年 2 月 29 日
移動済み: Steven Lord 2024 年 2 月 29 日
Him im looking for a simple command or code to call up a vertex from a polyshape array.
Specifically I want to draw a line inside a polyshape that comes from one vertex to another vertex. I want to call these vertexs randomly then have Matlabe draw a line between them. my starting code is the following
pgon=polyshape([0,5*cosd(18),5*cosd(-54),-5*cosd(-54),-5*cosd(18)],[5,5*sind(18),5*sind(-54),5*sind(-54),5*sind(18)]);
plot(pgon)

採用された回答

William Rose
William Rose 2024 年 2 月 29 日
If you have a polygon pgon, of unknown size, get the number of vertices:
pgon=polyshape(cosd(0:40:320),sind(0:40:320));
plot(pgon); hold on; axis equal; axis tight
N=length(pgon.Vertices);
fprintf('Number of vertices=%d.\n',N)
Number of vertices=9.
Use N to select vertices randomly:
vpair=randi(N,[1,2]); %return 2 random numers between 1 and N inclusive
Draw a line beteween the 2 vertices
plot(pgon.Vertices(vpair,1),pgon.Vertices(vpair,2),'-r')
The randomly chosen pair could be the same vertex twice, or adjacent vertices. You could add code to prevent this.
Good luck.
  3 件のコメント
William Rose
William Rose 2024 年 2 月 29 日
Putting it all together:
pgon=polyshape(cosd(0:20:340),sind(0:20:340));
plot(pgon); hold on; axis equal; axis tight
N=length(pgon.Vertices);
M=8; % number of lines to draw between randomly chosen vertices
for i=1:M
delta=1;
while delta<2,
vpair=randi(N,[1,2]);
delta=mod(abs(vpair(2)-vpair(1)),N);
end
plot(pgon.Vertices(vpair,1),pgon.Vertices(vpair,2),'-r')
end
OK.
Bruce Griffin
Bruce Griffin 2024 年 2 月 29 日
移動済み: Steven Lord 2024 年 2 月 29 日
Thank you very much. I appreciate your responce.

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

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeElementary Polygons についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by