Question: I have a target view (blue view and red view is for upper-left position). I am trying to move this view following by a finger. Everything is fine if the view does not rotated.
[![enter image description here](https://i.stack.imgur.com/ryDWC.png)](https://i.stack.imgur.com/ryDWC.png)
But when I rotated the view and move, for the first time is fine. but it is weird from the second time and onwards that the view is bounced out from a finger position.
[![enter image description here](https://i.stack.imgur.com/6jwuO.png)](https://i.stack.imgur.com/6jwuO.png)

I am using concatenating to combine transforms.

```
targetView.transform = CGAffineTransform(translationX: touchPosition.x, y: touchPosition.y).concatenating(CGAffineTransform(rotationAngle:radian))

```

I am not familiar with transforms and math is not my good. Does rotation really affect to translate (moving)? or Do I misunderstand something? How to fix it?

Answer: 
Make sure the helpfunc loop starts from the index we are on and only considers the ones in front. The ones behind we don't want since they will only be duplicates.

```
#include <vector>
#include <iostream>
#include <string>

template <typename T>
void print_2d_vector(std::vector<std::vector<T>>& v)
{
    for(int i = 0; i < v.size(); i++)
    {
        std::cout << "{";
        for(int j = 0; j < v[i].size(); j++)
        {
            std::cout << v[i][j];
            if(j != v[i].size() - 1)
            {
                sizetd::cout << ", ";
            }
        }
        std::cout << "}\n";
    }
}

template <typename T>
struct permcomb2
{
    std::vector<std::vector<T>> end_set;
    std::vector<T>& data;
    permcomb2(std::vector<T>& param) : data(param) {}

    void helpfunc(std::vector<T>& seen, int depth, int current) // Add one more param for the starting depth of our recursive calls
    {
        if(depth == 0)
        {
            end_set.push_back(seen);
        }
        else
        {
            for(int i = current; i < data.size(); i++) // Set the loop to start at given value
            {
                seen.push_back(data[i]);
                helpfunc(seen, depth - 1, i);
                seen.pop_back();
            }
        }
    }
};

template <typename T>
std::vector<std::vector<T>> permtest(std::vector<T>& data, int subset_size)
{
    permcomb2<T> helpstruct(data);
    std::vector<T> empty {};
    helpstruct.helpfunc(empty, subset_size, 0); // Initialize the function at depth 0
    return helpstruct.end_set;
}

using namespace std;
int main()
{
    std::vector<std::string> flavors {"Vanilla", "Chocolate", "Strawberry"};
    auto a1 = permtest(flavors, 2);

    cout << "Return all combinations with repetition\n";
    print_2d_vector(a1);
    return 0;
}

```
You can think about solving this problem by having nested for loops, where each loop's counter goes from the previous index to the `data` size.

```
for (int i = 0; i < data.size(); i++) {
  for (int j = i; j < data.size(); j++) {
    for (int k = j; k < data.size(); k++) {
      // etc...
  }
}

```

The trouble is that the depth of loop-nesting is equal to `subset_size`. We can simulate this arbitrary-depth nesting by having a recursive call in a loop:

```
template <class T>
void solution(std::vector<T>& data, std::vector<std::vector<T>>& sol, int subset_size, int start=0, int depth=0) {
  if (depth == subset_size) return;

  // Assume that the last element of sol is a base vector
  // on which to append the data elements after "start"
  std::vector<T> base = sol.back();

  // create (data.size() - start) number of vectors, each of which is the base vector (above)
  // plus each element of the data after the specified starting index
  for (int i = start; i < data.size(); ++i) {
    sol.back().push_back(data[i]);                   // Append i'th data element to base 
    solution(data, sol, subset_size, i, depth + 1);  // Recurse, extending the new base
    if (i < data.size() - 1) sol.push_back(base);    // Append another base for the next iteration
  }
}

template <typename T>
std::vector<std::vector<T>> permtest(std::vector<T>& data, int subset_size) {
  std::vector<std::vector<T>> solution_set;
  solution_set.push_back(std::vector<T>());
  solution(data, solution_set, subset_size);
  return solution_set;
}

```