Domanda di colloquio di Valve Corporation

Implement a function in C to draw a circle using the single point drawing function drawPt(x,y).

Risposte di colloquio

Anonimo

6 nov 2010

Not a hard question, just don't take it at face value. They're looking for efficient elegant code. Remember that you don't have to compute every point in the circle, just 1/8th of the arc and then permutate those coordinates across (+/-x, +/-y) and (+/-y, +/-x). Use Pythagorean's theorem to generate the y coord from the x coord but also remember that the radius doesn't change so precompute it once rather than each time through your loop. I think the bottom line is don't just give them what they ask for at face value, give them the most elegant and efficient code you can think of.

4

Anonimo

10 dic 2015

The key question I had is, "how many vertices do we draw for a given circle?" In person, I would probably ask how to handle this, but in a general case you can take it as a parameter. I've done this below as the "resolution" variable. I also assume the units are all doubles. void drawCircle(double x, double y, double radius, int resolution) { // Start at zero radians, and increment this many degrees each time. double degreeInc = 2*PI / (double) resolution; for(double deg = 0; deg < 2*PI; deg += degreeInc) { drawPt(x + radius*cos(deg), y + radius*cos(deg)); } }