Points on Arc Circumference

Arc edge coordinates

With this method we can find all the points that fall along the circumference of an arc, given the centre point, the radius, and the start and end angles of the arc in radians.

View example

This function works by using Bresenhams circle algorithm to find all points on the circle circumference, and then computes the angle between the centre and each point to see if that point falls within the arcs start and end angles.

The function is called with the following arguments: cx, cy, cr (the circle of which the arc is formed center X and Y coordinate, and its radius), angleStart and angleEnd (the start and end points of the arc in radians).


function pointsInArc(cx, cy, cr, angleStart, angleEnd)
{
	var list	= pointsOnCircumference(cx, cy, cr);
	var arc	= new Array();
	
	for(i in list)
	{
		var a = getAngle(cx, cy, list[i][0], list[i][1]);

		if(angleStart < 0 && (a >= (Math.PI*2 + angleStart))) { arc.push(list[i]); }
		else if(angleEnd > (Math.PI*2) && a <= (angleEnd - (Math.PI*2))) { arc.push(list[i]); }
		else if(a>=angleStart && a<=angleEnd) { arc.push(list[i]); }
	}
	
	return arc;
}

The return value is a list of coordinates in 2D space that form this arcs circumference.

This function makes use of the getAngle function:


function getAngle(x, y, x2, y2)
{
	var a = Math.atan2(y2 - y, x2 - x);
	return a < 0 ? a + (Math.PI * 2) : a;
}

It also makes use of Bresenhams circle algorithm:


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.015 second(s).