jeudi 27 octobre 2016

How to generate new random circles based on distance from head circle in JavaScript?

I have drawn a circle on a random position within a range on a canvas in JavaScript.

I want to generate 2 more circles on a random position on a specific distance (200) from the center of the 1st circle.

There is a way to generate new coordinates until the distance is equal to 200, but there should be a better solution, right?

How can I do the following?

enter image description here

Code:

var x = getRandomInt(200, 800)
var y = getRandomInt(200, 400)
var r = 60
drawCircle (x, y, r)

function getRandomInt(min, max) {
    return Math.floor(Math.random() * (max - min + 1)) + min
}

function drawCircle(x, y, r) {  
    context.beginPath()
    context.arc(x, y, r, 0, 2 * Math.PI, false)
    context.fillStyle = "#fc3358"
    context.fill()
    context.closePath()
}




Aucun commentaire:

Enregistrer un commentaire