Search code examples
cnested-loopsshapes

C Program, How to print shapes on a larger shape using loops?


I would like to seek some help for C programming as I have just started learning it.

I am currently working on printing several different shapes on an even bigger shape using *s.

I am using 2 for loops, the outer loop to print out the base shape/map (e.g. 10 x 5 rectangle) and the inner loop to print out another shape(e.g. a 2 x 3 rectangle) on the base shape.

Here is what I have now:

enter image description here

Using the following code:

 void createMapAndAddFeature(int x, int y, int xloc, int yloc, int feat_x_dim, int feat_y_dim)
{
    for (int i = 0; i < y; i++)
    {       
        for (int j = 0; j < x; j++)
        {
            //starting from which row
            //xloc refers to which column, yloc refers to which row. 
            //e.g. xloc = 0, plots on 1st column.  yloc = 2, plots on 3rd row
            if (yloc == i && xloc == j) {
                printf(" *"); //indicates at which coordinate to start plotting                 
            }                   
            else {
                printf(" ."); // . is the full shape
            }           
        }
        printf("\n");
    }

}

The x and y location helps me to identify where I should start plotting the shape, thus I have a * in the map.

Right now, I am stuck at the part where I need to create the shape on the map, for example, using dimensions 2 and 3, which would result in the output below: enter image description here

Does anyone know how this can be achieved?

Would I need another nested for loop? Or another "if else" statement to plot the 2x3 shape on the base shape?

Also, what can I do in order to plot multiple shapes as well? Example, another 2x2 square: enter image description here

I think I would need to have more "if else" conditions to check for existing *s in the loop and then plot the subsequent new shape so that it does overlap onto the existing ones. Please correct me if I'm wrong.

Any help and advice is greatly appreciated. Thanks in advance.


Solution

  • The easiest approach (to me) is to just iterate over all points in the space, and for each point classify it:

    • If it's on the outermost border, print a * (optional)
    • If it's on the inner feature, print a *
    • Else print a .

    Here's a quick attempt:

    #include <stdio.h>
    
    static void printFigure(int width, int height, int fx, int fy, int fwidth, int fheight)
    {
        for (int y = 0; y < height; ++y)
        {
            for (int x = 0; x < width; ++x)
            {
                char out;
    /*          if (x == 0 || y == 0 || x == width - 1 || y == height - 1)
                    out = '*';
    */          if(x >= fx && y >= fy && x < (fx + fwidth) && y < (fy + fheight))
                    out = '*';
                else
                    out = '.';
                putchar(out);
            }
            putchar('\n');
        }
    }
    
    int main(void) {
        printFigure(10, 5, 0, 2, 2, 3);
        return 0;
    }
    

    This prints:

    ..........
    ..........
    **........
    **........
    **........
    

    The commented out part implements the first (optional) idea, to mark the outer border. I initially thought that was a goal but realized I mis-read. Decided to keep the code since it's perhaps instructional.

    You can of course do a single loop and compute x and y from the "pixel index", but I don't think that's better for simple stuff like this.