Hello , i am trying to understand how periodic boundary condition works.Of course, i understand what it is and how it works,just i am trying to implement it in c++ and i have some problems.( for now i want to make it work in 1d but i want it for 2d also).
Lets say for example i have this line :
-2 -1 0 1 2 The size(length) of the line is 4.
|-----|-----|----|-----|
Ok , now for example i want to create a function which take as inputs the position of the 'particle' ,the step that it does and the size of the line.
I create for example this table:
initial position | step | final position
--------------------------------------------
0 | 2 | 2 <size we are ok here , the particle will go to '2'
1 | 2 | 3 <size
here the particle must go to the end of line (2) and then begin from start (-2) and go finally to -2.
2 | 4 | 6 >size here it must go finally to position 1.
I can see that if the final position is for example >size/2 (the second example above) or <-size/2 ,
i must do a calculation (i can't figure what!) in order to start from the beginning?
Any help?
Also, i know that this works with the modulo operator also , but right now i can't figure.I must understand this first.
Thank you!