I just had to come up with this myself, code is for counter clockwise but you can change that simply by changing the directions array.
int x = 0;
int y = 0;
int c = width * height;
int directions[4] = { 0, 1, 0, -1};
int distances[2] = { height, width-1};
int di = 0;
int dx = directions[ di];
int dy = directions[ di+1];
int dis = distances[ di];
for( int i=0; i