Facebook | Phone Screen | Draw a Circle
19606

Got this question few month ago on Facebook phone/virtual whiteboard tech screen.
The problem below is word to word as it was given:

Let us say that we already have an Api drawPoint(int x, int y), which can draw a point at position(x,y) on screen.
I would like you to implement drawCircle(int a, int b, int r) which should draw a circle whose center is (a,b) and radius is r


2/19/2020 - adding two possible working solutions in C++:

// based on: r*r = (x-a)*(x-a) + (y-b)*(y-b)
void drawCircle(int a, int b, int r)
{
	const int r_sqr = r * r;
	for (int x = 0; x <= r; ++x)
	{
		int y = sqrt(r_sqr - x*x);
		drawPoint(a + x, b + y);
		drawPoint(a + x, b - y);
		drawPoint(a - x, b + y);
		drawPoint(a - x, b - y);
	}
}
// based on: x = a + r * cos t; y = b + r * sin t
void drawCircle(int a, int b, int r)
{
	for (int angle = 0; angle < 360; ++angle)
	{
		int x = a + (r * cos(angle));
		int y = b + (r * sin(angle));
		drawPoint(x, y);
	}
}
Comments (31)