Points on Circumference

Circle boundary points

Given a circle with a centre point at cx, cy, with a radius cr, this function returns a list of coordinates that make the pixel positions, or integer positions, of the edge of the circle (the circles circumference).

View example

This is known as Bresenhams Circle Algorithm, and is a form of midpoint-circle algorithm that is particularly efficient as it only calculates an 8th of the circles circumference and simply mirrors and flips the generated coordinates 8 times, massively reducing computational costs.


function pointsOnCircumference(cx, cy, cr)
{
	var list = new Array();
	
	var x = cr;
	var y = 0;
	var o2 = Math.floor(1 - x);

	while(y <= x)
	{
		list.push([ x + cx,  y + cy]);
		list.push([ y + cx,  x + cy]);
		list.push([-x + cx,  y + cy]);
		list.push([-y + cx,  x + cy]);
		list.push([-x + cx, -y + cy]);
		list.push([-y + cx, -x + cy]);
		list.push([ x + cx, -y + cy]);
		list.push([ y + cx, -x + cy]);

		y+= 1;

		if(o2 <= 0) { o2+= (2 * y) + 1; }
		else
		{
			x-= 1;
			o2+= (2 * (y - x)) + 1;
		}
	}

	return list;
}

Page loaded in 0.013 second(s).