Calculating the position of points in a circle

匿名 (未验证) 提交于 2019-12-03 08:36:05

问题:

I'm having a bit of a mind blank on this at the moment. I've got a problem where I need to calculate the position of points around a central point, assuming they're all equidistant from the center and from each other.

The number of points is variable so it's DrawCirclePoints(int x) I'm sure there's a simple solution, but for the life of me, I just can't see it :)

回答1:

A point at angle theta on the circle whose centre is (x0,y0) and whose radius is r is (x0 + r cos theta, y0 + r sin theta). Now choose theta values evenly spaced between 0 and 2pi.



回答2:

Given a radius length r and an angle t in radians and a circle's center (h,k), you can calculate the coordinates of a point on the circumference as follows (this is pseudo-code, you'll have to adapt it to your language):

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


回答3:

Here's a solution using C#:

void DrawCirclePoints(int points, double radius, Point center) {     double slice = 2 * Math.PI / points;     for (int i = 0; i 

Sample output from 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} 

Good luck!



回答4:

Using one of the above answers as a base, here's the Java/Android example:

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 } 


回答5:

I had to do this on the web, so here's a coffeescript version of @scottyab's answer above:

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) 


回答6:

PHP Solution:

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;$igetX() + $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; } 


回答7:

For the sake of completion, what you describe as "position of points around a central point(assuming they're all equidistant from the center)" is nothing but "Polar Coordinates". And you are asking for way to Convert between polar and Cartesian coordinates which is given as x = rcos(t), y = rsin(t).



回答8:

Here is an R version based on the @Pirijan answer above.

points 


回答9:

The angle between each of your points is going to be 2Pi/x so you can say that for points n= 0 to x-1 the angle from a defined 0 point is 2nPi/x.

Assuming your first point is at (r,0) (where r is the distance from the centre point) then the positions relative to the central point will be:

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


回答10:

Working Solution in 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; } } 


标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!