Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <Rcpp.h>
- using namespace Rcpp;
- //rotate/flip a quadrant appropriately
- // [[Rcpp::export]]
- void rot(int n, int *x, int *y, int rx, int ry) {
- if (ry == 0) {
- if (rx == 1) {
- *x = n-1 - *x;
- *y = n-1 - *y;
- }
- //Swap x and y
- int t = *x;
- *x = *y;
- *y = t;
- }
- }
- //convert (x,y) to d
- // [[Rcpp::export]]
- int xy2d (int n, int x, int y) {
- int rx, ry, s, d=0;
- for (s=n/2; s>0; s/=2) {
- rx = (x & s) > 0;
- ry = (y & s) > 0;
- d += s * s * ((3 * rx) ^ ry);
- rot(s, &x, &y, rx, ry);
- }
- return d;
- }
- //convert d to (x,y)
- // [[Rcpp::export]]
- void d2xy(int n, int d, int *x, int *y) {
- int rx, ry, s, t=d;
- *x = *y = 0;
- for (s=1; s<n; s*=2) {
- rx = 1 & (t/2);
- ry = 1 & (t ^ rx);
- rot(s, x, y, rx, ry);
- *x += s * rx;
- *y += s * ry;
- t /= 4;
- }
- }
- // You can include R code blocks in C++ files processed with sourceCpp
- // (useful for testing and development). The R code will be automatically
- // run after the compilation.
- //
- /*** R
- xy2d(5, 27, 33)
- */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement