Advertisement
Guest User

Untitled

a guest
Mar 19th, 2019
75
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.01 KB | None | 0 0
  1. #include <Rcpp.h>
  2. using namespace Rcpp;
  3.  
  4. //rotate/flip a quadrant appropriately
  5. // [[Rcpp::export]]
  6. void rot(int n, int *x, int *y, int rx, int ry) {
  7. if (ry == 0) {
  8. if (rx == 1) {
  9. *x = n-1 - *x;
  10. *y = n-1 - *y;
  11. }
  12.  
  13. //Swap x and y
  14. int t = *x;
  15. *x = *y;
  16. *y = t;
  17. }
  18. }
  19.  
  20. //convert (x,y) to d
  21. // [[Rcpp::export]]
  22. int xy2d (int n, int x, int y) {
  23. int rx, ry, s, d=0;
  24. for (s=n/2; s>0; s/=2) {
  25. rx = (x & s) > 0;
  26. ry = (y & s) > 0;
  27. d += s * s * ((3 * rx) ^ ry);
  28. rot(s, &x, &y, rx, ry);
  29. }
  30. return d;
  31. }
  32.  
  33. //convert d to (x,y)
  34. // [[Rcpp::export]]
  35. void d2xy(int n, int d, int *x, int *y) {
  36. int rx, ry, s, t=d;
  37. *x = *y = 0;
  38. for (s=1; s<n; s*=2) {
  39. rx = 1 & (t/2);
  40. ry = 1 & (t ^ rx);
  41. rot(s, x, y, rx, ry);
  42. *x += s * rx;
  43. *y += s * ry;
  44. t /= 4;
  45. }
  46. }
  47.  
  48.  
  49. // You can include R code blocks in C++ files processed with sourceCpp
  50. // (useful for testing and development). The R code will be automatically
  51. // run after the compilation.
  52. //
  53.  
  54. /*** R
  55. xy2d(5, 27, 33)
  56. */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement