#include <iostream>
#include <vector>
#include <complex>
using namespace std;
pair< int ,int > dir( int d) {
d % = 4 ;
return make_pair( d/ 2 ? d% 2 * 2 - 1 : 0 ,d/ 2 ? 0 : d% 2 * 2 - 1 ) ;
}
pair< int ,int > iota( int d) {
d % = 4 ;
return make_pair( d% 2 ? 0 : 1 - d/ 2 * 2 ,d% 2 ? 1 - d/ 2 * 2 : 0 ) ;
}
template < class T>
complex< T> exp ( complex< T> b, int e) {
complex< T> o( 1 ,0 ) ;
while ( e) {
if ( e% 2 ) o * = b;
b * = b;
e / = 2 ;
}
return o;
}
int main( ) {
vector< string> v = { "left" ,"right" ,"up" ,"down" } ;
cout << "i\t j" << endl;
for ( int i= 0 ; i< 4 ; i++ )
cout << dir( i) .first << '\t ' << dir( i) .second << "\t d:" << i<< '\t ' << v[ i] << endl;
// second approach
vector< int > dirs= { 0 ,1 ,0 ,- 1 ,0 } ;
v = { "right" ,"down" ,"left" ,"up" } ;
cout << endl<< "i\t j" << endl;
for ( int i= 0 ; i< 4 ; i++ )
cout << dirs[ i] << '\t ' << dirs[ i+ 1 ] << "\t d:" << i<< '\t ' << v[ i] << endl;
v = { "right" ,"up" ,"left" ,"down" } ;
cout << endl<< "x\t y" << endl; // plane change and hence sense of direction
for ( int i= 0 ; i< 4 ; i++ )
cout << iota( i) .first << '\t ' << iota( i) .second << "\t i^" << i<< '\t ' << v[ i] << endl;
complex< int > i( 0 ,1 ) ;
cout << endl<< "(x,y)" << endl; // notation and sense of direction change
for ( int d= 0 ; d< 4 ; d++ )
cout << exp ( i,d) << "\t i^" << d<< '\t ' << v[ d] << endl;
return 0 ;
}
I2luY2x1ZGUgPGlvc3RyZWFtPgojaW5jbHVkZSA8dmVjdG9yPgojaW5jbHVkZSA8Y29tcGxleD4KdXNpbmcgbmFtZXNwYWNlIHN0ZDsKcGFpcjxpbnQsaW50PiBkaXIoaW50IGQpewoJZCAlPSA0OwoJcmV0dXJuIG1ha2VfcGFpcihkLzI/ZCUyKjItMTowLGQ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