Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I generate random points on a circles circumference in javascript

I am trying to write a function that will randomly return an (x,y) co-ordinates around a given circumference so if I have a point that's at (0,0) (being the center of the div) how can I write a function that randomly places other entities that appear among the outer edge of a circle.

All I need is the equation i know it has something to do with getting the distance from the center to the circumference edge just no idea how to calculate it and randomize it so it looks good.

like image 983
Mouseroot Avatar asked Mar 26 '12 20:03

Mouseroot


People also ask

How do you get a random point on the circumference of a circle?

The simplest method would be to generate a random angle θ∈[0,2π]. Then convert from polar to Cartesian with (x,y)=(rcosθ,rsinθ). Note that if your random number generator returns a number k∈[0,1] then θ=2πk.

How do you generate random points in Python?

In python, there's an inbuilt method, “random. uniform()” which performs this task with ease and uses just one word. This method is defined in the “random” module. It Returns the generated floating-point random number between the lower limit and upper limit.


1 Answers

Just get a random angle:

var angle = Math.random()*Math.PI*2; 

Then

x = Math.cos(angle)*radius; y = Math.sin(angle)*radius; 

Done.

like image 143
Niet the Dark Absol Avatar answered Sep 24 '22 03:09

Niet the Dark Absol