简体   繁体   English

查找常规多边形的顶点

[英]Find regular polygon's vertices

I have some properties defined by the user, and then I want use them to automatically generate a regular polygon. 我有一些由用户定义的属性,然后我想使用它们来自动生成一个常规多边形。 The properties are centre x, centre y, radius and number of vertices. 属性是中心x,中心y,半径和顶点数。 I would like to know how to calculate the x and y coordinates of all vertices of a regular polygon. 我想知道如何计算规则多边形的所有顶点的x和y坐标。 I've already tried to do as Calculate coordinates of a regular polygon's vertices discussion. 我已经尝试做为计算正多边形多边形顶点讨论的坐标 But it always gives me thw wrong coordinates. 但这总是给我错误的坐标。 My current code is as follows (C++): 我当前的代码如下(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;

There's an error in your angle calculation. 角度计算中存在错误。

The angle between each vertex should be 2 * M_PI / vertices . 每个顶点之间的角度应为2 * M_PI / vertices

Obviously your macro: 显然,您的宏:

#define DOUBLE(a) ((a)*(a))

is incorrect. 是不正确的。

However in C++ you really shouldn't use macros for such trivial operations anyway - it should at the very most be an inline function, or just the direct formula given above. 但是,在C ++中,您实际上绝对不应该将宏用于此类琐碎的操作-它最多应该是一个内联函数,或者仅仅是上面给出的直接公式。

Try 尝试

#define DOUBLE(a) ((a) + (a))

or 要么

#define DOUBLE(a) (2 * (a))

You are defining SQUARE(a) under a false identity. 您正在使用错误的身份定义SQUARE(a)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM