Malcolm McLean said:
בת×ריך ×™×•× ×©×™×©×™, 20 ביולי 2012 10:00:55 UTC+1, מ×ת Andrew Cooper:
The problem is it's a plugs and adapeters system.
Consider this
void getcursorposition(unsigned int *x, unsigned int *y)
an x, y index into a raster is necessarily unsigned, right?
Yes, but not a cursor position. I would be very unhappy with an API
that conflated these two concepts because, as your example shows, it's
natural to represents positions as signed quantities. (In fact I'd want
a position to be represented as some kind of "point" but that's another
matter.)
Now we want to draw an octogon round the cursor. So we'll build it on
top of a function void drawpolygon(). What signature would you give
drawpolygon, and how would you write this code?
struct point { int x, y; };
void drawpolygon(struct point *pt, size_t np, bool closed);
struct point octagon[8];
unsigned int cx, cy;
getcursorposition(&cx, &cy);
for (int v = 0; v < 8; v++) {
octagon[v].x = cx;
octagon[v].y = cy;
move_point_polar(&octagon[v], 360/8 * v, radius);
}
drawpolygon(octagon, 8, true);
Having another prototype for getcursorposition would not make very much
difference, though I'd probably "correct" the API like this:
static inline void getcursorposition_as_point(struct point *pt)
{
// Why is there not a function to do this already?
unsigned int x, y;
getcursorposition(&x, &y);
pt->x = x;
pt->y = y;
}
struct point center, octagon[8];
getcursorposition_as_point(¢er);
for (int v = 0; v < 8; v++) {
octagon[v] = center;
move_point_polar(&octagon[v], 360/8 * v, radius);
}
drawpolygon(octagon, 8, true);
How would you write it with a "better" prototype for getcursorposition?