Alternating directions

Posted by on February 23, 2011

Just a quick thought :)
If you want to alternate between -1 and 1 in a for-loop, say for the purpose of changing direction or something similar, and you don’t want to add an extra variable to manually alternate in each run-through, this works just fine:

for (int i=0; i < something; i++){
   dir = ((i % 2) * 2) - 1;
   // dir is now good for multiplying onto vectors or whatever.

103 Responses to Alternating directions

Leave a Reply