Thread: copying a 4x4 matrix inside a 5x5

  1. #1
    Registered User
    Join Date
    Nov 2009
    Posts
    10

    copying a 4x4 matrix inside a 5x5

    Hi friends. I am trying to place a 4x4 matrix(piece) inside a 5X5 (card). This is done however only when all the positions into which the piece is going to be copied are free. Consider the following arrays of chars:
    Code:
    char CARD[sizeof_card][sizeof_card] = {
    		{'x', 'x', ' ', 'x','x'},
    		{' ', ' ', ' ', ' ','x'},
    		{'x', ' ', ' ', ' ','x'},
    		{' ', ' ', ' ', ' ','x'},
    		{'x', ' ', ' ', ' ','x'},
    	};
    	
    char piece[sizeof_piece][sizeof_piece] = { 
    		{' ',' ','g',' '},
    		{'g','g','g',' '},
    		{' ','g',' ',' '},
    		{' ',' ',' ',' '},
    	};
    and the function that returns (int) the number of all possible positions where "piece" can be placed with or without rotation(counter-cw). For example it can be located at 1st position :[0][0][how_often_rotates = 0], 2nd position : [1][1] [how_often_rotates = 0], 3rd position: [1][0][how_often_rotates = 0], 4th position (possible) : [1][0][how_often_rotates = 1]. A struct holds the data and here is how it looks like: (from sample)
    14 // number of all possible positions "piece" can be located .
    nightmare here
    0, 0, 0 // piece rotated 0x counter-cw
    1, 1, 0 // piece rotated 0x counter-cw
    :
    1, 0, 1 // piece rotated 1x
    1, 1, 1 // piece rotated 1x
    :

    Code:
    typedef struct tag_possible_xy_rot_positions /* I have problems incrementing total_num_of_possible_positions in struct */
    { 
     int total_num_of_possible_positions; 
     int **p2p2_positions_rotations; 
    } POSSIBLE_XY_ROTATE_POSITIONS;
    Code:
    #include"someheader.h"
    #define BLANK 32
    //variables
    typedef struct info{
    	int height;
    	int width;
    }INFO;
    
    dim_piece(char (*piece)[4], INFO *p2_piece_coordinates, int sizeof_piece)
    	{
    	int i, j;
    	for(i=0;i<sizeof_piece;i++)
    		{
    		for(j=0;j<sizeof_piece;j++)
    			{
    			if((int)piece[i][j] != BLANK)
    				{
    				if(p2_piece_coordinates->width <= j)
    					{
    					p2_piece_coordinates-> width= j + 1;
    					}
    				if(p2_piece_coordinates->height <= i)
    					{
    					p2_piece_coordinates->height = i + 1;
    					}
    				}
    			}
    		
    		}
    	return 0;
    }
    Code:
    /* Return value of where_the_piece_fits(
             card,                      // card[5][5] 
             piece                      // piece [4][4] 
             sizeof_card             // 5 
             sizeof_piece);         // 4 
     */
    Code:
    void where_the_piece_fits(char (*card)[5], char (*piece)[4], int sizeof_card, int sizeof_piece)
    {
    	INFO piece_cooardinates = { 0 , 0 };
    	INFO *p2_piece_coordinates= &piece_coordinates;
    	// Dim(height, width) of piece
    	dim_piece(piece, p2_piece_coordinates,sizeof_piece);
    }
    I'd appreciate if you could help me to get my function : where_the_piece_fits() and the struct tag_possible_xy_rot_positions somehow working. Thank you all in advance.

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    You'll have to check, for each of the starting places, whether any of the not-space characters in your piece are either outside the card or on top of another not-space character.

    You'll also have to work out how to rotate the piece, but if you sit down with a piece of paper and actually do it you'll probably see how that works pretty quickly.

    For my incrementing needs, I usually use "++". Make sure you declare a variable of that type somewhere, and you're going to have to decide how to store the rotation data (three ints? Some struct? Etc).

  3. #3
    Registered User
    Join Date
    Nov 2009
    Posts
    10
    could you be more specific on three ints (for what exactly?), I have written a code for rotation however, how should I use the rotation's data (why 3 ints?) in order to check if the rotated piece fits into a position and hence to increment the total_num_of_positions?. Using strcmp() for two 2D arrays of same size might possibly work but how do I compare piece (4x4) with (5x5)? you meant comparing (in my case) the char 'g' from piece with ' ' from card? thanks

  4. #4
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    You claim you want an x-position, a y-position, and a number of rotations. If my counting is correct that would appear to be three pieces of data, each of which is an int.

  5. #5
    Registered User
    Join Date
    Nov 2009
    Posts
    10
    That's up there. "int **p2p2_positions_rotations;" for x and y ( thought 3 extra ints). how do I work around this struct and the func where_piece_fits? any help is appreciated

  6. #6
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    If you want to store the information as a faux two-dimensional array, then that's fine -- but you'll have to set it up, at some point. Also you'll need to keep track of how much memory you have stored and realloc as necessary to get more (the "easy" way here is to only malloc as much memory as you need at the moment, and every time you find another fit to realloc more memory and fill the new slot).

    Strcmp is not involved here at all. You need to check, IF a particular part of your piece is not a space THEN BOTH that part is not outside the card AND the corresponding part on the card is a space.

  7. #7
    Registered User
    Join Date
    Nov 2009
    Posts
    10
    I am still struggling. To do what you said I need to compare these 2 matrices(piece[4][4] and card[5][5] )
    how many for loops do I need to perform the comparison ?
    I used a double for loop but it doesn't work.
    further help is appreciated.

  8. #8
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    You'll need a double for loop just for all the different starting places that your piece matrix can be in. To do the actual comparison you'll want another double for loop to walk through your piece array.

  9. #9
    Registered User
    Join Date
    Nov 2009
    Posts
    10
    well, the problem is not solved yet. Here is what I have done for finding the positions of x and y where the piece fits.
    Code:
    	int i, j, temp = y;
    	for(i=0;i<4; i++)
    	{
    		for(j=0; j<4 ; j++)
    		{
    			if(card[x][y] != ' ' && piece[i][j] != ' ')
    			{
    				return 0;
    			}
    			y++;
    			
    	}	
    	x++; 
    	temp = y++;
    	return 1;
    
    }
    return 0;
    }
    how do I save the positions in **p2p2_positions_rotations ?
    thanks

  10. #10
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Well, for one you'll need to fix the code you've got -- y just keeps going up and up and up and up and up and up and up and up and up. Also, "return" means you never get to check any other positions, so unless you're looping this inside something else it's upper-left-or-bust.

    Once you've got one that works, you can save the information in p2p2_positions_rotations just like you save any other variable, with "=". You'll need to make sure that enough memory has been allocated; and then you can store in [0], [1], [2]; [3], [4], [5]; etc.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. more matrix problems
    By Aisthesis in forum C++ Programming
    Replies: 10
    Last Post: 03-05-2010, 03:29 AM
  2. *operator overloading: scalar matrix multiplication
    By gemini_shooter in forum C++ Programming
    Replies: 4
    Last Post: 06-08-2009, 01:14 PM
  3. Still battling with Copy Control
    By Mario F. in forum C++ Programming
    Replies: 9
    Last Post: 06-23-2006, 08:04 AM
  4. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 06:39 AM
  5. Matrix and vector operations on computers
    By DavidP in forum A Brief History of Cprogramming.com
    Replies: 11
    Last Post: 05-11-2004, 06:36 AM

Tags for this Thread