Calculation of the position of points in a circle

At the moment I have a little mind. I have a problem when I need to calculate the position of points around a central point, assuming that they are all equally spaced from the center and from each other.

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

+75
math algorithm geometry trigonometry circle
Mar 14 '11 at 15:45
source share
11 answers

A point with an angle tta on a circle whose center (x0,y0) and whose radius r is (x0 + r cos theta, y0 + r sin theta) . Now select theta values ​​evenly distributed between 0 and 2pi.

+63
Mar 14 '11 at 15:48
source share

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

 float x = r*cos(t) + h; float y = r*sin(t) + k; 
+77
Mar 14 '11 at 15:50
source share

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

Example 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

+48
Mar 14 2018-11-11T00:
source share

Using one of the above answers as a base, here's a 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 } 
+9
Jul 02 '14 at 19:24
source share

I needed to do this on the Internet, so here is the scottyab version for coffee shop below:

 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) 
+3
Dec 02 '15 at 4:00
source share

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;$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; } 
+2
Jan 26 '14 at 21:19
source share

For the sake of completion, what you describe as “the position of the points around the center point (provided that they are all equally distant from the center)” is nothing but the “polar coordinates”. And you ask for a way to convert between polar and Cartesian coordinates, which are given as x = r*cos(t) , y = r*sin(t) .

+2
Jan 29 '14 at 14:46
source share

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

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

 rCos(2nPi/x),rSin(2nPi/x) 
+1
Mar 14 2018-11-11T00:
source share

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; } } 
+1
Jan 09 '17 at 13:03 on
source share

Here is a version of R based on @Pirijan's answer above.

 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) 
+1
Jun 16 '17 at 16:20
source share

Based on the answer above from Daniel, here I take using Python3.

 import numpy shape = [] def circlepoints(points,radius,center): 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])) 
0
Jul 19 '18 at 11:16
source share



All Articles