How do I get moving dots to loop?

I am having trouble with my moving dot program. I have it to where the dots move to the right like they are supposed to, but how do I get them to loop back around so it looks like they are continuously going by rather than moving off the page? The block of code I need to edit to make this happen is below. I hope someone can help and thanks again in advance!

1
2
3
4
5
6
7
8
9
void advanceDots(XPoint dots[], unsigned num_dots, XWindowAttributes &w){
  
  int s = num_dots;
  for(s = 0; s < num_dots; s++){
    if(s < num_dots){
      dots[s].x++;
    }
  }
}
Last edited on
Something like this perhaps:
1
2
3
4
5
6
7
8
9
10
void advance_dots(XPoint dots[], unsigned number_of_dots/*, XWindowAttributes& attributes*/) {

	const unsigned some_constant = 10;

	for (unsigned index = 0; index < number_of_dots; ++index) {
		auto& x = dots[index].x;
		x = (++x % some_constant);
	}

}
Thank you so much for your answer but this didn't work. I was given a hint that said I need to determine when the x position is too high and the dots go off the page. Then, I need to change x back to 0 to make the dots go back to their original position.
Mind telling me why it didn't work? The code I posted more or less does what your hint describes.

Of course, you can be explicit instead of using a modulo operation (%) and C++11 features:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
void advance_dots(XPoint dots[], unsigned number_of_dots/*, XWindowAttributes& attributes*/) {

	const unsigned max_distance = 10;

	for (unsigned index = 0; index < number_of_dots; ++index) {
		
		dots[index].x++;

		if (dots[index].x >= max_distance) {
			dots[index].x = 0;
		}

	}

}
I made a minor syntax error but I got it figured out!
Topic archived. No new replies allowed.