在圆中计算点的位置

104

目前我有点脑子不太灵光。 我有一个问题,需要计算中心点周围的点的位置,假设它们与中心点和彼此之间都是等距的。

这个点的数量是可变的,所以是DrawCirclePoints(int x) 我相信有一个简单的解决方案,但我就是想不出来 :)


1
大家都给出了很棒的答案,反应超级快,所以我选择了第一个回答者 :) 他们都很优秀 :) - JoeBrown
13个回答

1

每个点之间的夹角将会是 2Pi/x,因此你可以说对于点 n= 0 to x-1,相对于定义的0点的角度为 2nPi/x

假设你的第一个点位于 (r,0)(其中 r 是距离中心点的距离),那么相对于中心点的位置将会是:

rCos(2nPi/x),rSin(2nPi/x)

1

根据Daniel的回答,以下是我使用Python3的方法。

import numpy


def circlepoints(points,radius,center):
    shape = []
    slice = 2 * 3.14 / points
    for i in range(points):
        angle = slice * i
        new_x = center[0] + radius*numpy.cos(angle)
        new_y = center[1] + radius*numpy.sin(angle)

        p = (new_x,new_y)
        shape.append(p)

    return shape

print(circlepoints(100,20,[0,0]))

1

Java中的工作解决方案:

import java.awt.event.*;
import java.awt.Robot;

public class CircleMouse {

/* circle stuff */
final static int RADIUS = 100;
final static int XSTART = 500;
final static int YSTART = 500;
final static int DELAYMS = 1;
final static int ROUNDS = 5;

public static void main(String args[]) {

    long startT = System.currentTimeMillis();
    Robot bot = null;

    try {
        bot = new Robot();
    } catch (Exception failed) {
        System.err.println("Failed instantiating Robot: " + failed);
    }
    int mask = InputEvent.BUTTON1_DOWN_MASK;

    int howMany = 360 * ROUNDS;
    while (howMany > 0) {
        int x = getX(howMany);
        int y = getY(howMany);
        bot.mouseMove(x, y);
        bot.delay(DELAYMS);
        System.out.println("x:" + x + " y:" + y);
        howMany--;
    }

    long endT = System.currentTimeMillis();
    System.out.println("Duration: " + (endT - startT));

}

/**
 * 
 * @param angle
 *            in degree
 * @return
 */
private static int getX(int angle) {
    double radians = Math.toRadians(angle);
    Double x = RADIUS * Math.cos(radians) + XSTART;
    int result = x.intValue();

    return result;
}

/**
 * 
 * @param angle
 *            in degree
 * @return
 */
private static int getY(int angle) {
    double radians = Math.toRadians(angle);
    Double y = RADIUS * Math.sin(radians) + YSTART;
    int result = y.intValue();

    return result;
}
}

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接