Random points in pie (SO)

by navinleon

HTML

<canvas id=canvas width=600 height=600></canvas>

CSS

body {background:#777}

JavaScript

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

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

	ctx.fillStyle = '#ffffff';

while(count) {
	var x = Math.random() * canvas.width;
    var y = Math.random() * canvas.height;
    var xDiff = cx - x;
    var yDiff = cy - y;
    if(Math.sqrt(xDiff*xDiff+yDiff*yDiff)<radius)
    {
        ctx.fillRect(x, y, 2, 2);
        count--;
    }    
}