drawing boxes

This is a discussion on drawing boxes within the C Programming forums, part of the General Programming Boards category; Hello I need some help with drawing boxes. I can draw triangles, but boxes i am having trouble with. I ...

  1. #1
    Registered User
    Join Date
    Apr 2002
    Posts
    19

    Question drawing boxes

    Hello

    I need some help with drawing boxes. I can draw triangles, but boxes i am having trouble with.

    I need to draw a box like this ...

    ####
    # #
    ####

    It needs to be hollow, not like a triangle ...

    *
    **
    ***

    Does anybody know an algorithm to draw a box?

    eg height = 3
    width = 5

    output

    #####
    # #
    #####

    Anybody have any hints?

    thanku

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    > Anybody have any hints?

    Yeah. The easiest is to use the search button and find all the other people doing homework that have asked the same thing...

    draw the top row (x number of stars)
    for x-2 times, draw: 1 star, x-2 spaces, 1 star
    draw the bottom row (x number of stars)

    Very simple stuff

    Quzah.
    Hope is the first step on the road to disappointment.

  3. #3
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    Declare two boundary variables, rows and cols. When cols has the value 0 or the maximum number of columns then you print the filler character, in this case '#'. When rows has the value 0 or the maximum number of rows, fill the entire row. Simple.
    Code:
    The numbers are column numbers:
    Row 1: 01234567
    Row 2: 0      7
    Row 3: 0      7
    Row 4: 0      7
    Row 5: 01234567
    Enjoy

    -Prelude
    My best code is written with the delete key.

  4. #4
    Registered User
    Join Date
    Mar 2002
    Posts
    95
    heres a function I wrote ages ago to draw a box of whatever size, and before ANYONE says its not compatible because of cprintf, they can save there breath, I needed colour, i needed it to work on a ibm and this is the easiest, quickest and simplest way, also used ibm ascii characters for the border but change them as you wish and change it to a printf if you like:

    Code:
    void border(int x1,int y1,int x2,int y2)
    {
    	int temppos;
    
    	gotoxy(x1,y1);
    	cprintf("");
    	for (temppos=x1;x2-temppos>1;temppos++)
    		cprintf("");
    	cprintf("");
    	for (temppos=y1+1;y2-temppos>0;temppos++)
    	{
    		gotoxy(x1,temppos);
    		cprintf("");
    		gotoxy(x2,temppos);
    		cprintf("");
    	}
    	gotoxy(x1,y2);
    	cprintf("");
    	for (temppos=x1;x2-temppos>1;temppos++)
    		cprintf("");
    	cprintf("");
    	gotoxy(x1,y1);
    }
    acsii chars dont show up in windows though.

  5. #5
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    >and before ANYONE says its not compatible
    I considered my usual portability rant, but I decided that you obviously know better. So I won't bother anymore as long as you let everyone know that it may not work on all systems. Now, if you use undefined behavior, expect me to start ranting.

    -Prelude
    My best code is written with the delete key.

  6. #6
    Registered User
    Join Date
    Mar 2002
    Posts
    95

    Smile

    haha, fair enough

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Displaying ascii boxes
    By rocketman03 in forum C Programming
    Replies: 8
    Last Post: 12-10-2008, 08:41 PM
  2. Slow drawing code
    By tjpanda in forum Windows Programming
    Replies: 5
    Last Post: 05-09-2008, 06:09 PM
  3. drawing column label boxes
    By vrek in forum Windows Programming
    Replies: 11
    Last Post: 05-24-2007, 01:25 PM
  4. Really hard recursion with boxes, help?
    By aciarlillo in forum C++ Programming
    Replies: 3
    Last Post: 10-13-2005, 08:22 AM
  5. drawing minimaps and radar screens.
    By Eber Kain in forum Game Programming
    Replies: 4
    Last Post: 03-08-2002, 11:44 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21