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

雨燕双飞 提交于 2020-11-30 02:34:51

问题


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.


回答1:


Just get a random angle:

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

Then

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

Done.



来源:https://stackoverflow.com/questions/9879258/how-can-i-generate-random-points-on-a-circles-circumference-in-javascript

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