Wednesday, January 2, 2013

Creating more Interesting shapes in ncurses programs

Ok guys, I think after following my previous posts on ncurses, you are eager to try out some more colorful and interesting programs of ncurses to come up with few more shapes. Let me give you some homework first.

think of building some shapes like a house, Christmas tree, simple rocket etc using ncurses. Before starting, let me explain you the easiest way to do them all.

First of all, think out of the box. I am not asking you to draw perfect shapes but just the sketches of them. OK, think in this way, what if you think they are made of little blocks as we have already discussed. learn how to print squares  triangles, rectangles and so on, then think how you can join them and build some interesting shapes as I asked you to try above.


Printing a Christmas tree in ncurses


It's just a matter of printing three triangles and a rectangle, right. See the output first and then try your own. If you really couldn't figure it out, then go through the code :) 

you need to notice one more point in here. Since I already told you that it is easy if you can separately print your sub shapes to print the entire shape, more efficient way is to print these sub shapes in different functions and calling them in order to complete the entire shape.

If you have more than one rectangles to print in the same picture, you don't need two different functions but you can simply implement one function with four parameters to state the starting column, starting row, height and the width of the triangle you need to print. Call this function to print any number of rectangles passing values to print it in different positions and sizes.

I have commented the first source code for your ease. Others go similarly.





#include<ncurses.h>                                                                 //including ncurses library

void printTriangle(int startrow,int startcol,int height);                //function declaration to print a triangle 
void printRectangle(int startrow,int startcol,int height,int width); //function declaration to print a rectangle

int main()
{
initscr();                                                                       //initializing the screen
start_color();                                                               //starting color mode

init_pair(1,1,2);                                                           //initializing the color pairs
init_pair(2,3,7);
init_pair(3,4,2);
init_pair(4,7,4);

attrset(COLOR_PAIR(1));                                        //using the first color pair as attribute set
printTriangle(5,15,3);                                                 //printing triangle 1 

attrset(COLOR_PAIR(2));                                       //using the second color pair as attribute set
printTriangle(9,15,4);                                                 //printing triangle 2 

attrset(COLOR_PAIR(3));                                      //using the third color pair as attribute set
printTriangle(14,15,5);                                              //printing triangle 3 

attrset(COLOR_PAIR(4));                                     //using the fourth color pair as attribute set
printRectangle(20,12,1,6);                                       //printing rectangle

getch();
endwin();
return 0;
}
//----------------------implementation of printing a triangle

void printTriangle(int startrow,int startcol,int height)
{
int x=startcol;

for(int r=startrow;r<=startrow+height;r++)
{
for(int c=startcol;c<=x;c++)
{
move(r,c);
printw("*");
}
x++;
startcol--;
}

}

//-----------------------implementation of printing a rectangle

void printRectangle(int startrow,int startcol,int height,int width)
{
for(int r=startrow;r<=startrow+height;r++)
{
for(int c=startcol;c<=startcol+width;c++)
{
move(r,c);
printw("*");
}
}

}


Printing a house in ncurses.





Source code:

 #include<ncurses.h>                                                                       
void printTriangle(int startrow,int startcol,int height);
void printRectangle(int startrow,int startcol,int height,int width);

int main()
{
initscr();
start_color();

init_pair(1,3,1);
init_pair(2,4,2);
init_pair(3,7,4);


attrset(COLOR_PAIR(1));
printTriangle(3,15,4);


attrset(COLOR_PAIR(2));
printRectangle(8,10,7,10);

attrset(COLOR_PAIR(3));
        printRectangle(11,12,4,2);


getch();
endwin();
return 0;
}
//----------------------------------------------------------------------------------

void printTriangle(int startrow,int startcol,int height)
{
int x=startcol;

for(int r=startrow;r<=startrow+height;r++)
{
for(int c=startcol;c<=x;c++)
{
move(r,c);
printw("*");
}
x++;
startcol--;
}

}

//----------------------------------------------------------------------------------
void printRectangle(int startrow,int startcol,int height,int width)
{
for(int r=startrow;r<=startrow+height;r++)
{
for(int c=startcol;c<=startcol+width;c++)
{
move(r,c);
printw("*");
}
}

}


you must note one more important point in the above code. Whatever the task that you have implemented inside your will not be executed unless you call that specific function some where inside your main method. Also, note that you have a door in the house above. You have print the bigger rectangle( in green ) and then the rectangle for the door (in blue) should be printed. 

What I wanted to tell you is, the order that you call your functions is very important. if you call the function for the green rectangle first and then the blue rectangle printing function second, then you can see as above but if you call them in the other order (blue one first and then the green) , you will see only the green box because the smaller blue rectangle would be hidden behind the bigger green rectangle.


 Printing a rocket in ncurses






Source code:

 #include<ncurses.h>                                                                         
void printTriangle(int startrow,int startcol,int height);
void printRectangle(int startrow,int startcol,int height,int width);

int main()
{
initscr();
start_color();

init_pair(1,7,1);
init_pair(2,1,3);
init_pair(3,4,2);
init_pair(4,4,3);

attrset(COLOR_PAIR(1));
printTriangle(2,16,2);

attrset(COLOR_PAIR(4));
printTriangle(17,16,2);

attrset(COLOR_PAIR(2));
        printRectangle(7,10,1,3);

attrset(COLOR_PAIR(2));
        printRectangle(7,19,1,3);


attrset(COLOR_PAIR(3));
printRectangle(5,14,11,4);

getch();
endwin();
return 0;
}
//----------------------------------------------------------------------------------

void printTriangle(int startrow,int startcol,int height)
{
int x=startcol;

for(int r=startrow;r<=startrow+height;r++)
{
for(int c=startcol;c<=x;c++)
{
move(r,c);
printw("*");
}
x++;
startcol--;
}

}

//----------------------------------------------------------------------------------
void printRectangle(int startrow,int startcol,int height,int width)
{
for(int r=startrow;r<=startrow+height;r++)
{
for(int c=startcol;c<=startcol+width;c++)
{
move(r,c);
printw("*");
}
}

}


Printing number 1 in other way around in ncurses





  Source code:

#include<ncurses.h>                                                                          
void printTriangle(int startrow,int startcol,int height);                            
void printRectangle(int startrow,int startcol,int height,int width);       

int main()
{
initscr();
start_color();

init_pair(1,4,7);
init_pair(2,4,2);
init_pair(3,2,3);


attrset(COLOR_PAIR(1));
printTriangle(5,18,3);


attrset(COLOR_PAIR(2));
printRectangle(5,15,9,2);

attrset(COLOR_PAIR(3));
        printRectangle(15,13,1,6);

getch();
endwin();
return 0;
}
//----------------------------------------------------------------------------------

void printTriangle(int startrow,int startcol,int height)
{
int x=startcol;

for(int r=startrow;r<=startrow+height;r++)
{
for(int c=startcol;c<=x;c++)
{
move(r,c);
printw("*");
}
x++;

}

}

//----------------------------------------------------------------------------------
void printRectangle(int startrow,int startcol,int height,int width)
{
for(int r=startrow;r<=startrow+height;r++)
{
for(int c=startcol;c<=startcol+width;c++)
{
move(r,c);
printw("*");
}
}

}



Hope you really enjoyed these kind of programming very well.See you in another interesting ncurses programming post.



2 comments: