如何生成一组彼此等距并位于圆上的点

Sha*_*ier 4 c geometry

我试图生成一个n个点的数组,它们彼此等距并且位于C中的一个圆上.基本上,我需要能够传递一个函数,我想要生成的点数并返回一个数组分数.

unw*_*ind 5

尝试这样的事情:

void make_circle(float *output, size_t num, float radius)
{
  size_t i;

  for(i = 0; i < num; i++)
  {
    const float angle = 2 * M_PI * i / num;
    *output++ = radius * cos(angle);
    *output++ = radius * sin(angle);
  }
}
Run Code Online (Sandbox Code Playgroud)

这是未经测试的,在角度步计算中可能存在一个一个隐藏的隐藏但它应该是接近的.

当然,这假设我正确地理解了这个问题.

更新:将角度计算重新设置为不递增,以减少由于重复添加而导致的浮动精度损失.


Mik*_*tly 5

自从我完成了C/C++以来,已经很长时间了,所以我更多地了解了这一点,看看我是如何继续使用它的,但是这里有一些代码可以为你计算得分.(这是VS2010控制台应用程序)

// CirclePoints.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include "stdio.h"
#include "math.h"

int _tmain()
{
    int points = 8;
    double radius = 100;

    double step = ((3.14159265 * 2) / points);
    double x, y, current = 0;
    for (int i = 0; i < points; i++)
    {
        x = sin(current) * radius;
        y = cos(current) * radius;

        printf("point: %d x:%lf y:%lf\n", i, x, y);

        current += step;
    }

    return 0;
}
Run Code Online (Sandbox Code Playgroud)