C++ program to check if two images match after rotation and translation


Suppose, there are two n * n pixel square images first and second. The pixels can either be black or white. The images are given in a matrix representation where if a pixel is black it is represented as 'x' and if it is white, it is represented as '.'. We have to check the second image matches with the first image after 90° rotations and translations. If it does we return true, otherwise, we return false.

So, if the input is like n = 4, first = {"..x.", "x.x.", "x.xx", "xx.."}, second = {"..xx", "x.xx", ".x.x", "..x."}, then the output will be False.

To solve this, we will follow these steps−

Define a function find(), this will take an array of pairs x, an array of pairs y,
   d1 := first value of y[0] - first value of x[0]
   d2 := second value of y[1] - second value of x[1]
   for initialize i := 1, when i < size of x, update (increase i by 1), do:
      if first value of y[i] - first value of x[i] is not equal to d1 or second value of y[i] - second value of x[i] is not equal to d2, then:
         return false
   return true
Define a function rotate(), this will take n, an array of pairs a, an array of pairs b,
   for initialize i := 0, when i < size of b, update (increase i by 1), do:
      b[i] := make_pair(second value of b[i], n - first value of b[i] - 1)
Define two arrays a, b that can contain integer pairs
for initialize i := 0, when i < n, update (increase i by 1), do:
   s := first[i]
   for initialize j := 0, when j < n, update (increase j by 1), do:
      if s[j] is same as 'x', then:
         insert pair(i, j) at the end of a
for initialize i := 0, when i < n, update (increase i by 1), do:
   s := second[i]
   for initialize j := 0, when j < n, update (increase j by 1), do:
      if s[j] is same as 'x', then:
         insert pair(i, j) at the end of b
if size of a is not equal to size of b, then:
   return false
if size of a is same as 0, then:
   return true
check := false
sort the array a
for initialize i := 0, when i < 4, update (increase i by 1), do:
   sort the array b
   if find(a, b), then:
      check := true
   rotate(n, a, b)
if check is true, then:
   return true
Otherwise
   return false

Example

Let us see the following implementation to get better understanding −

#include <bits/stdc++.h>
using namespace std;

bool find(vector<pair<int, int>> x, vector<pair<int, int>> y){
   int d1 = y[0].first - x[0].first;
   int d2 = y[1].second - x[1].second;
   for(int i = 1; i < x.size(); i++){
      if(y[i].first - x[i].first != d1 || y[i].second - x[i].second != d2){
         return false;
      }
   }
   return true;
}
void rotate(int n, vector<pair<int, int>> a, vector<pair<int, int>> b){
   for(int i = 0; i < b.size(); i++){
      b[i] = make_pair(b[i].second, n - b[i].first - 1);
   }
}
bool solve(int n, vector<string> first, vector<string> second){
   vector<pair<int, int>> a, b;
   for(int i = 0; i < n; i++){
      string s = first[i];
      for(int j = 0; j < n; j++){
         if(s[j] == 'x'){ 
            a.push_back(make_pair(i, j));
         }
      }
   }
   for(int i = 0; i < n; i++){
      string s = second[i];
      for(int j = 0; j < n; j++){
          if(s[j] == 'x'){ b.push_back(make_pair(i,j));
      }
   }
}
if(a.size() != b.size()){
   return false;
}
if(a.size() == 0){
   return true;
}
bool check = false;
sort(a.begin(),a.end());
for(int i = 0; i < 4; i++){ 
   sort(b.begin(),b.end());
   if(find(a,b)){
      check = true;
   }
   rotate(n, a, b);
}
if(check){
   return true;
}else{
   return false;
  }
}
int main() { 
   int n = 4; vector<string> first = {"..x.", "x.x.", "x.xx", "xx.."}, second = {"..xx", "x.xx", ".x.x", "..x."};
   cout<< solve(n, first, second);
   return 0;
}

Input

4, {"..x.", "x.x.", "x.xx", "xx.."}, {"..xx", "x.xx", ".x.x", "..x."}

Output

0

Updated on: 02-Mar-2022

156 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements