游戏开发者联盟

c++二维数组旋转

QQ图片20200501101250

    std::vector<std::vector<ubyte>> clockwise90(
        const std::vector<std::vector<ubyte>>& arr) {
      std::vector<std::vector<ubyte>> ret;
      ret.resize(arr[0].size());
      for (auto& row : ret) {
        row.resize(arr.size());
      }

      iint lastr = ret.size() - 1;
      iint lastc = ret[0].size() - 1;
      for (iint c = 0; c < ret[0].size(); c++) {
        for (iint r = 0; r < ret.size(); r++) {
          ret[r][c] = arr[lastc - c][r];
        }
      }
      return std::move(ret);
    }
    std::vector<std::vector<ubyte>> clockwise180(
        const std::vector<std::vector<ubyte>>& arr) {
      auto ret = arr;
      iint lastr = ret.size() - 1;
      iint lastc = ret[0].size() - 1;

      for (iint r = 0; r < ret.size(); r++) {
        for (iint c = 0; c < ret[0].size(); c++) {
          ret[r][c] = arr[lastr - r][lastc - c];
        }
      }
      return std::move(ret);
    }
    std::vector<std::vector<ubyte>> clockwise270(
        const std::vector<std::vector<ubyte>>& arr) {
      std::vector<std::vector<ubyte>> ret;
      ret.resize(arr[0].size());
      for (auto& row : ret) {
        row.resize(arr.size());
      }

      iint lastr = ret.size() - 1;
      iint lastc = ret[0].size() - 1;
      for (iint c = 0; c < ret[0].size(); c++) {
        for (iint r = 0; r < ret.size(); r++) {
          ret[r][c] = arr[c][lastr - r];
        }
      }
      return std::move(ret);
    }