![](/img/trans.png)
[英]Find regular polygon vertices by any given number of sides and then center it inside an square
[英]Find regular polygon's vertices
我有一些由用戶定義的屬性,然后我想使用它們來自動生成一個常規多邊形。 屬性是中心x,中心y,半徑和頂點數。 我想知道如何計算規則多邊形的所有頂點的x和y坐標。 我已經嘗試做為計算正多邊形多邊形頂點討論的坐標 。 但這總是給我錯誤的坐標。 我當前的代碼如下(C ++):
#define DOUBLE(a) ((a)*(a))
...
if(radius <= 0 || vertices < 3)
return NULL;
Polygon* poly = new Polygon;
double angle = DOUBLE(M_PI) / vertices;
for(long i = 0; i < vertices; i++)
{
double a = (angle * i);
poly->add(centerX + radius * cos(a), centerY + radius * sin(a));
}
return poly;
角度計算中存在錯誤。
每個頂點之間的角度應為2 * M_PI / vertices
。
顯然,您的宏:
#define DOUBLE(a) ((a)*(a))
是不正確的。
但是,在C ++中,您實際上絕對不應該將宏用於此類瑣碎的操作-它最多應該是一個內聯函數,或者僅僅是上面給出的直接公式。
嘗試
#define DOUBLE(a) ((a) + (a))
要么
#define DOUBLE(a) (2 * (a))
您正在使用錯誤的身份定義SQUARE(a)。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.