计算圆中点的位置

目前我对此有一点空白。 我有一个问题,我需要计算中心点周围点的位置,假设它们距离中心和距离都是等距的。

点的数量是可变的,所以它是DrawCirclePoints(int x)我相信有一个简单的解决scheme,但对于我的生活,我只是看不到它:)

(x0,y0)为中心,半径为r的圆上的angular度θ为(x0 + r cos theta, y0 + r sin theta) 。 现在select在0和2pi之间均匀分布的theta值。

给定一个半径长度r和一个以弧度表示的angular度t和一个圆心(h,k) ,你可以按如下方式计算圆周上一个点的坐标(这是伪代码,你必须适应你的语言):

 float x = r*cos(t) + h; float y = r*sin(t) + k; 

这是一个使用C#的解决scheme:

 void DrawCirclePoints(int points, double radius, Point center) { double slice = 2 * Math.PI / points; for (int i = 0; i < points; i++) { double angle = slice * i; int newX = (int)(center.X + radius * Math.Cos(angle)); int newY = (int)(center.Y + radius * Math.Sin(angle)); Point p = new Point(newX, newY); Console.WriteLine(p); } } 

DrawCirclePoints(8, 10, new Point(0,0));示例输出DrawCirclePoints(8, 10, new Point(0,0));

 {X=10,Y=0} {X=7,Y=7} {X=0,Y=10} {X=-7,Y=7} {X=-10,Y=0} {X=-7,Y=-7} {X=0,Y=-10} {X=7,Y=-7} 

祝你好运!

使用上面的答案作为基础之一,这里是Java / Android的例子:

 protected void onDraw(Canvas canvas) { super.onDraw(canvas); RectF bounds = new RectF(canvas.getClipBounds()); float centerX = bounds.centerX(); float centerY = bounds.centerY(); float angleDeg = 90f; float radius = 20f float xPos = radius * (float)Math.cos(Math.toRadians(angleDeg)) + centerX; float yPos = radius * (float)Math.sin(Math.toRadians(angleDeg)) + centerY; //draw my point at xPos/yPos } 

我不得不在网上做这个,所以这里是一个coffeescript版本的@ scottyab上面的答案:

 points = 8 radius = 10 center = {x: 0, y: 0} drawCirclePoints = (points, radius, center) -> slice = 2 * Math.PI / points for i in [0...points] angle = slice * i newX = center.x + radius * Math.cos(angle) newY = center.y + radius * Math.sin(angle) point = {x: newX, y: newY} console.log point drawCirclePoints(points, radius, center) 

PHP解决scheme:

 class point{ private $x = 0; private $y = 0; public function setX($xpos){ $this->x = $xpos; } public function setY($ypos){ $this->y = $ypos; } public function getX(){ return $this->x; } public function getY(){ return $this->y; } public function printX(){ echo $this->x; } public function printY(){ echo $this->y; } } 
 function drawCirclePoints($points, $radius, &$center){ $pointarray = array(); $slice = (2*pi())/$points; for($i=0;$i<$points;$i++){ $angle = $slice*$i; $newx = (int)(($center->getX() + $radius) * cos($angle)); $newy = (int)(($center->getY() + $radius) * sin($angle)); $point = new point(); $point->setX($newx); $point->setY($newy); array_push($pointarray,$point); } return $pointarray; } 

为了完成,你所描述的“围绕一个中心点的位置(假设它们离中心等距离)”只不过是“极坐标”。 你要求的方式是在极坐标和笛卡尔坐标之间进行转换,这个坐标是x = rcos(t),y = rsin(t)。

你的每个点之间的angular度将是2Pi/x所以你可以说,对于点n= 0 to x-1 ,定义的0点的angular度是2nPi/x

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

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

Java中的工作解决scheme:

 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; } } 

这里是基于上面的@Pirijan答案的R版本。

 points <- 8 radius <- 10 center_x <- 5 center_y <- 5 drawCirclePoints <- function(points, radius, center_x, center_y) { slice <- 2 * pi / points angle <- slice * seq(0, points, by = 1) newX <- center_x + radius * cos(angle) newY <- center_y + radius * sin(angle) plot(newX, newY) } drawCirclePoints(points, radius, center_x, center_y)