These pieces of code
if(x1<PIby2) x1=x1;
else if(x1< -PIby2) x1=Math.PI+x1;
else x1=Math.PI-x1;
I suspect you want each angle within +/- PI/2. That would be
if(x1>PIby2) x1=x1-Math.PI;
else if(x1< -PIby2) x1=Math.PI+x1;
Also, it would be better to have an array rather than eight variables so you didn't have to write the same code eight times!
I do not know the Poincaré index but that looks like a mistake and it will invalidate the results.