jeudi 17 septembre 2015

Simplest way to plot points randomly inside a circle

I have a basic JSfiddle where by I want to have random points plotted inside my circle.

But I do not know how to limit the points to be inside the circle.

This is what i currently have:

var ctx = canvas.getContext('2d'),
    count = 1000, // number of random  points
    cx = 150,
    cy = 150,
    radius = 148;

    ctx.beginPath();
    ctx.moveTo(cx, cy);
    ctx.arc(canvas.width/2, canvas.height/2, radius, 0, 2*Math.PI);
    ctx.closePath();
    ctx.fillStyle = '#00000';
    ctx.fill();

// create random points
    ctx.fillStyle = '#ffffff';

while(count) {
    // randomise x:y

    ctx.fillRect(x + canvas.width/2, y + canvas.height/2, 2, 2);
    count--;
}

How would i go about generating random x:y co-ordinates to plot random points inside the circle?

My current fiddle: http://ift.tt/1Mu1rBc




Aucun commentaire:

Enregistrer un commentaire