Mathematics Stack Exchange is a question and answer site for people studying math at any level and professionals in related fields. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

With an arbitrary starting location, that can be any point along the edge of a rectangle, how would you iterate over every point in a clockwise order? So for example if the starting point happens to be (1, 0) like in the image below, how would you iterate over all the points, shown as green squares, around the rectangle. I can probably do a simple brute force method by figuring out which edge of the rectangle the starting point is on and moving the position along that edge until the max x or y is reached and then follow along the next edge until the starting point is reached again, but I'm just curious if there is a more elegant solution.

I forgot to add that the data I have to work with is the top-left position of the rectangle, its size, and the starting point along the edge.

enter image description here

share|cite|improve this question
How are you given the data of the "rectangle"? The coordinates of its four corners? A list of the lattice point it intersects? the coordinates of two opposite corners? – Vhailor Mar 8 '12 at 1:39
@Vhailor That's a good point. I forgot to include that. I'll edit the post. – Shenjoku Mar 8 '12 at 2:18

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Browse other questions tagged or ask your own question.