Calculating the position of points in a circle
Solution 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.
Solution 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;
Solution 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 < 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
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
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
}
JoeBrown
Updated on July 08, 2022Comments
-
JoeBrown almost 2 years
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 :) -
Melsi about 11 yearsExcellent! Worked great for me, I already translated it to php-cairo and works great!
-
Andreas almost 10 yearsYou have flipped cos and sin functions should be sin for x and cos for y. Not the other way around.
-
Brian Driscoll almost 10 yearsMy degree in mathematics, as well as every other answer here, say you are incorrect.
-
Andreas almost 10 yearsHm.. well on the swedish wikipedia it says sin is x axis I know this is not secure source but then I used sin on x and cos on y my cube started moving in the right direction. Even my math teacher pointed out that I flipped them. Can you think of any other reason why my cube would move in a strange pattern away from the target location and then I flipped them it moves to it's position?
-
Andreas almost 10 yearsThis is the code I wrote maybe you could tell why it works with them flipped? jsfiddle.net/Lf5sZ
-
Brian Driscoll almost 10 years@Andreas Without looking at your code I would guess that you have flipped something around somewhere, or some user input is not behaving as you expect.
-
Andreas almost 10 yearsLooks like since the cord is moving from top left to bottom right instead of bottom left to top right things got flipped.
-
Brian Driscoll almost 10 yearsIn screen coordinates the positive y-axis is reversed, so that makes sense.
-
Ruyonga Dan over 8 yearsam looking to do the same kind of task, however mine is depedent on the Triggertrap/SeekArc · GitHub , when a user moves the thumb , i want to place an image to indicate that selected progress of the person....all i have tried give me the points a bit off and the not a perfec
-
nirvanaswap over 7 yearsThe classic question- is value of pi 3.14 or 180? (i.e is the angle in deg or radian?)
-
Gareth McCaughan over 7 yearsDefinitely radians. If you use degrees you need angles between 0 and 360 instead.
-
Gareth McCaughan over 7 years(The value of pi is 3.14ish regardless of how you prefer to write angles, of course. It is what it is.)
-
Jason over 7 yearsI believe the parenthesis is incorrect for
$newx
and$newy
, putting the coordinates way outside the circle radius. Try$newx = (int)($center->getX() + ($radius * cos($angle)));
and similar for$newy
. -
Patrick Hund over 3 yearsPerfect, thanks! Just what I was looking for.