Identifying a square in a square

Good evening.

I am learning c++ and im currently at a halt in this project.


I am trying to make a function that would be able to spot a certain sub-square in a square.

It starts as:
1) Input a one dimensional vector with nxn number of elements representing an nxn square and a number representing the subqaure needed
2) A subquare in this is equal to the size of sqrtn x sqrtn such that there are n number of sub-squares in one square. e.g a 9by9 square has 9 smaller squares in it and a 16by16 has 4 smaller squares in it.
3)Save the elements in the sub square given in an output vector using push_back.


My only issue is that I am finding it hard to be able to locate sub-squares. I looked at turning the one dimensional input vector into a 2D array and then creating some sort of algorithm based on row column and sub square number to try to pull out the index of these elements and put them into the output vector but this does not seem to be efficient nor did it seem possible.


For example if you had a 4 by 4 square the index of the 4 sub square would be
0,0 0,1 1,0 1,1
then 0,2 0,3 1,2 1,3
then 2,0 2,1 3,0 3,1
then 2,2 2,3 3,2 3,3

Does not seem to have an arithmetic or geometric sequence unless I am mistaken


So I started thinking about trying to identify them using the border of the square but this may be far fetched.

Does anyone have have any ideas what would be a good way to go about this?

Many thanks.
Last edited on
closed account (367kGNh0)
Have you used if statements and conditionals?
I have yes.
Your explanation is far from clear, @imperialcplusplus. This may (or may not) be what you want.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
#include <iostream>
#include <iomanip>
#include <string>
#include <vector>
#include <cmath>
using namespace std;

using matrix = vector< vector<int> >;

//----------------------------------------------------------

void output( string title, matrix M )
{
   if ( title != "" ) cout << title << '\n';
   for ( auto &row : M )
   {
      for ( auto e : row ) cout << setw( 2 ) << e << " ";
      cout << '\n';
   }
   cout << '\n';
}

//----------------------------------------------------------

int main()
{
   const int N = 4;
   int rootN = sqrt( N );

   matrix M = { {  0,  1,  2,  3 },
                {  4,  5,  6,  7 },
                {  8,  9, 10, 11 },
                { 12, 13, 14, 15 } };

   output( "Initial matrix:", M );

   for ( int sub = 0; sub < N; sub++ )
   {
      matrix m( rootN, vector<int>( rootN, 0 ) );          // submatrix (initialised to 0)
      int i0 = ( sub / rootN ) * rootN;                    // (i0,j0) = top-left position in original matrix
      int j0 = ( sub % rootN ) * rootN;            
      for ( int i = 0; i < rootN; i++ )                    // (i,j) = index in submatrix
      {
         for ( int j = 0; j < rootN; j++ ) m[i][j] = M[i0+i][j0+j];  // (i0+i,j0+j) = index in original matrix
      }
      output( "Submatrix " + to_string( sub ) + ":", m );
   }
}


Initial matrix:
 0  1  2  3 
 4  5  6  7 
 8  9 10 11 
12 13 14 15 

Submatrix 0:
 0  1 
 4  5 

Submatrix 1:
 2  3 
 6  7 

Submatrix 2:
 8  9 
12 13 

Submatrix 3:
10 11 
14 15 

To define a square you need only two points. e.g. Lower Left, Upper Right.

Perhaps something like this:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
#include <iostream>
using namespace std;

class Point
{   
public:
    int x;
    int y;

    Point () : x(0), y(0)
    {}
    Point(int _x, int _y) : x(_x), y(_y)
    {};
};

class Square
{   Point ll;
    Point ur;
public:
    Square(Point _ll, Point _ur) : ll(_ll), ur(_ur)
    {}
    bool is_inside(const Square & sq)
    {
        if (sq.ll.x >= ll.x && sq.ll.x <= ur.x
            && sq.ll.y >= ll.y && sq.ll.y <= ur.y
            && sq.ur.x <= ur.x && sq.ur.x >= ll.x
            && sq.ur.y <= ur.y && sq.ur.y >= ll.y)
            return true;
        else
            return false;
};

int main ()
{   Square outer(Point(0, 0), Point(4, 4));
    Square inner(Point(0, 0), Point(1, 1));

    if (outer.is_inside(inner))
        cout << "Inner square is inside outer square" << endl;
    else
        cout << "Inner square is NOT inside outer square" << endl;
)


Topic archived. No new replies allowed.