Calculating the position of points in a circle

AlgorithmMathTrigonometryGeometry

Algorithm Problem Overview


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 :)

Algorithm Solutions


Solution 1 - Algorithm

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;

Solution 2 - Algorithm

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.

Solution 3 - Algorithm

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

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!

Solution 4 - Algorithm

Placing a number in a circular path

// variable

let number = 12; // how many number to be placed
let size = 260; // size of circle i.e. w = h = 260
let cx= size/2; // center of x(in a circle)
let cy = size/2; // center of y(in a circle)
let r = size/2; // radius of a circle

for(let i=1; i<=number; i++) {
  let ang = i*(Math.PI/(number/2));
  let left = cx + (r*Math.cos(ang));
  let top = cy + (r*Math.sin(ang));
  console.log("top: ", top, ", left: ", left);
}

Solution 5 - Algorithm

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
}

Solution 6 - Algorithm

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

Solution 7 - Algorithm

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 = r*cos(t), y = r*sin(t).

Solution 8 - Algorithm

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)

Solution 9 - Algorithm

Here is how I found out a point on a circle with javascript, calculating the angle (degree) from the top of the circle.

  const centreX = 50; // centre x of circle
  const centreY = 50; // centre y of circle
  const r = 20; // radius
  const angleDeg = 45; // degree in angle from top
  const radians = angleDeg * (Math.PI/180);
  const pointY = centreY - (Math.cos(radians) * r); // specific point y on the circle for the angle
  const pointX = centreX + (Math.sin(radians) * r); // specific point x on the circle for the angle

Solution 10 - Algorithm

Here is an R version based on the @Pirijan 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)

Solution 11 - Algorithm

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)

Solution 12 - Algorithm

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

Solution 13 - Algorithm

Based on the answer above from Daniel, here's my take using 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]))

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionJoeBrownView Question on Stackoverflow
Solution 1 - AlgorithmBrian DriscollView Answer on Stackoverflow
Solution 2 - AlgorithmGareth McCaughanView Answer on Stackoverflow
Solution 3 - AlgorithmDaniel LidströmView Answer on Stackoverflow
Solution 4 - AlgorithmmaSC0d3RView Answer on Stackoverflow
Solution 5 - AlgorithmscottyabView Answer on Stackoverflow
Solution 6 - AlgorithmbitmousView Answer on Stackoverflow
Solution 7 - AlgorithmAdarshaView Answer on Stackoverflow
Solution 8 - AlgorithmPirijanView Answer on Stackoverflow
Solution 9 - AlgorithmNoby FujiokaView Answer on Stackoverflow
Solution 10 - AlgorithmjstaView Answer on Stackoverflow
Solution 11 - AlgorithmChrisView Answer on Stackoverflow
Solution 12 - AlgorithmGewureView Answer on Stackoverflow
Solution 13 - AlgorithmPratikView Answer on Stackoverflow