フィルターのクリア

Vertices of regular n-gon.

9 ビュー (過去 30 日間)
Jim Oste
Jim Oste 2015 年 2 月 13 日
回答済み: Steven Lord 2017 年 11 月 8 日
I want to find the perimeter of a regular inscribed polygon given N sides. I have a function that will calculate the distance from a set of coordinates. I have the following code to find the coordinates of the vertices for a regular n-gon;
x = cos(n.*(2*pi)./N);
y = sin(n.*(2*pi)./N);
I just don't know to store individual coordinates to pass through my function to find the distance between each vertex.

採用された回答

John D'Errico
John D'Errico 2015 年 2 月 13 日
編集済み: John D'Errico 2015 年 2 月 13 日
Do it vectorized. Learn to use vectors.
t = linspace(0,1,N);
You can view t as the ratio n/N here, stored as a vector.
x = cos(t.*2*pi);
y = sin(t.*2*pi);
d = sum(sqrt(diff(x).^2 + diff(y).^2));
So, for N = 10, this yields
d =
6.15636257986204
Not too far from 2*pi. Increase N to 1000,
d =
6.28317495105715
2*pi
ans =
6.28318530717959
You can see it does well enough. It should approach 2*pi asymptotically from below as N goes to infinity.
  2 件のコメント
nanying
nanying 2017 年 11 月 7 日
Just want to mention that d = sum(sqrt(diff(x).^2 + diff(y).^2)) only sum N-1*terms which is 9 in your case.
John D'Errico
John D'Errico 2017 年 11 月 7 日
@nanying - what is your point? The first and last vertices in the polygon as created are the same. So if you wanted to point out that to create a true N-gon, thus a regular polygon with N sides, you actually needed to use N+1 in the code above, that would have been a valid comment. The perimeter length of the polygon is correct though.

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

その他の回答 (1 件)

Steven Lord
Steven Lord 2017 年 11 月 8 日
If you're using release R2017b or later, you can use the nsidedpoly function.

カテゴリ

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