Thread: Drawing a circle with ASCII character

  1. #1
    Registered User
    Join Date
    Mar 2008
    Location
    Slovenia
    Posts
    36

    Drawing a circle with ASCII character

    Hello,

    i have to draw a circle with ASCII character. So far i got to this and now i'am stuck. Could somebody help me please? The "." is just to give me a better overview, in the end i will replace that with " ".

    Code:
    #include <stdio.h>
    
    void line (int r);
    void vertical (int r);
    
    
    int main()
    {
    	int r;
    	
    	printf("radius: ");
    	scanf("%d", &r);
    	
    	line(r);
    	printf("\n");
    	vertical(r-1);
    	printf("\n");
    	line(r);
    	
    	return 0;
    }
    
    void line(int r)
    {
    	int i,j;
    	
    	for ( i=1; i<=r-1; i++){
    		if( (r-i)>=2 )
    			printf(".");
    		else{
    			for(j=0;j<3;j++)
    				printf("x");
    		}
    	}
    }
    
    void vertical(int r)
    {
    	int i,j,k;
    	
    	for ( i=1; i<=r-1; i++){
    		if( (r-i)>=2 )
    			printf(".\n");
    		else{
    			for(j=0;j<3;j++)
    				printf("x\n");
    			for(k=0; k<r-2; k++){
    				printf("\n");
    				printf(".");
    			}
    		}
    		
    	}	
    }
    Thank you in advance.
    bnk

  2. #2
    Dr Dipshi++ mike_g's Avatar
    Join Date
    Oct 2006
    Location
    On me hyperplane
    Posts
    1,218
    Heres some code for drawing a filled circle, I'm pretty sure its the same algo used in MS paint as it leaves a pointy out pixel. Anyway nevermind about that, the good thing about this algo is that it draws your circle in scanlines, which is ideal for console output. You might want to add some offset for x, and use putchar instead of wirtepixel, that should work.
    Code:
    for(y=-radius; y<radius; y++)
    {
        half_row_width=sqrt(radius*radius-y*y);
        for(x=-half_row_width; x< half_row_width; x++)
            WritePixel(centre_x+x, centre_y+y, colour);
    }

  3. #3
    Registered User
    Join Date
    Mar 2008
    Location
    Slovenia
    Posts
    36
    Thank you. I will play with this a bit.

  4. #4
    Banned
    Join Date
    Nov 2007
    Posts
    678
    mike: the code is very easy to follow. thanks.
    and i only knew how to draw outline circles, which is more difficult the way i do, but this is very easy.
    Thank you. I will play with this a bit.
    Me too!

  5. #5
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    Draw a circle with ASCII:

    Code:
    printf("O");

  6. #6
    Registered User guesst's Avatar
    Join Date
    Feb 2008
    Location
    Lehi, UT
    Posts
    179
    Quote Originally Posted by brewbuck View Post
    Draw a circle with ASCII:

    Code:
    printf("O");
    Hehe.

    Is your code for drawing a circle in ASCII taking into account the fact that ASCII characters are rectangles? If you don't scale they Y appropriately you may be looking at an oval.
    Type-ins are back! Visit Cymon's Games at http://www.cymonsgames.com for a new game every week!

  7. #7
    Registered User
    Join Date
    Mar 2008
    Location
    Slovenia
    Posts
    36
    Hi again. I did play around with it a bit and this is what i did. What i forgot to tell you i cant use any other library then stdio.h.

    Code:
    #include <stdio.h>
    
    int main()
    {
    	int i,j,radius;
    
    	printf("Radius is: ");
    	scanf("%d", &radius);
    
    	for (j=0; j<2*radius; j++)
    	{
    		for(i=1; i<2*radius; i++)
    		{
    			if(i>radius-2 && i<radius+2)
    				printf("x");
    			else
    				printf(".");
    		}
    		printf("\n");
    	}
    
    	return 0;
    }
    Thats what i got. I think i have to do something with the if statement for x so that i would print the character at the right place on the y coordinate. Could somebody give me any pointers ?!

    guesst wrote

    Is your code for drawing a circle in ASCII taking into account the fact that ASCII characters are rectangles? If you don't scale they Y appropriately you may be looking at an oval.
    You are absolutly right, but i dont have to draw a perfect circle.


    Thank you.

  8. #8
    Dr Dipshi++ mike_g's Avatar
    Join Date
    Oct 2006
    Location
    On me hyperplane
    Posts
    1,218
    You could draw a bresenham circle using only the functions in stdio. The algorithm draws cirles in four corners. So in your case you would have to draw the top two first at the same time, then the bottom two. You should be able to find an example if you google it.

  9. #9
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    Quote Originally Posted by mike_g View Post
    You could draw a bresenham circle using only the functions in stdio. The algorithm draws cirles in four corners. So in your case you would have to draw the top two first at the same time, then the bottom two. You should be able to find an example if you google it.
    An even more optimal algorithm only computes 1/8th of the circle, and rotates/reflects it to produce the other 7/8ths. I can't remember what it's called -- perhaps it's also called Bresenham, though a slightly different method.

  10. #10
    Registered User
    Join Date
    Mar 2008
    Location
    Slovenia
    Posts
    36
    I think i've did it. I just looked at my fist post and i have to say i'am really srry but i didn't write what i have to do right. I dont have to draw a circle but an circular line. Srry english is not my maternal language. So now let's get back to the problem.

    In the If statement i have to implement the pythagoras theorem: x^2+y^2=r^2 and every point that is over or under the vaule I'am going to putchar( ) everything else is goint to be an (x) or something else.

    Thank you all for helping.

  11. #11
    Dr Dipshi++ mike_g's Avatar
    Join Date
    Oct 2006
    Location
    On me hyperplane
    Posts
    1,218
    brewbuck: oh yeah, it was 8ths wasent it. I forgot, been a while since I last looked at it.

    bnkslo: yeah the only problem is that to get the square root you need math.h, unless you write your own function to do this.

  12. #12
    Registered User
    Join Date
    Mar 2008
    Location
    Slovenia
    Posts
    36
    Mike you are right. I just wrote this:

    Code:
    int main()
    {
    	int i,j,radij;
    	
    	printf("Put in radij: ");
    	scanf("%d", &radij);
    	
    	if(radij>0 && radij<80)
    	{
    		for (j=0; j<=2*radij; j++)
    		{
    			for (i=0; i<=2*radij; i++)
    			{
    				if ((j*j+i*i<=2*radij) && (j*j+i*i>=2*radij))
    					printf("x");
    				else
    					printf(".");
    			}	
    			printf("\n");
    		}
    	}
    	else
    	      printf("Wrong radij!");
    	
    	return 0;
    }
    and this is what came out:


    ...........
    ...x.......
    ...........
    .x.........
    ...........
    ...........
    ...........
    ...........
    ...........
    ...........
    ...........
    If I understod right you mean i have to write a function so that i have a point from where I'am goint to calculate the circular line? Like a center of the circle?

  13. #13
    Registered User
    Join Date
    Mar 2008
    Location
    Slovenia
    Posts
    36
    I already saw some totaly dum mistake like 2*r=r^2 I mean and some other miner mistakes here and there...

  14. #14
    Dr Dipshi++ mike_g's Avatar
    Join Date
    Oct 2006
    Location
    On me hyperplane
    Posts
    1,218
    Well first off if you dont get the square root the circle will end up being very big and in most cases too big to display properly in the console.

    For each scanline you could get your x offset before you start plotting your circle dots. Then draw that amount of blanks, then draw as many x's as needed followed by a newline.

    Edit; the -rad to +rad bit is important. It wont work correctly from 0 to rad*2.
    Last edited by mike_g; 04-03-2008 at 12:50 PM.

  15. #15
    Registered User
    Join Date
    Mar 2008
    Location
    Slovenia
    Posts
    36
    Mike you were right the magic word was offset i have to treat it like the circle mid point is not in the top left corner.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Game Pointer Trouble?
    By Drahcir in forum C Programming
    Replies: 8
    Last Post: 02-04-2006, 02:53 AM
  2. Replies: 3
    Last Post: 01-25-2006, 08:04 PM
  3. syntax error when defining vectors
    By starkhorn in forum C++ Programming
    Replies: 5
    Last Post: 09-22-2004, 12:46 PM
  4. Character handling help
    By vandalay in forum C Programming
    Replies: 18
    Last Post: 03-29-2004, 05:32 PM
  5. Replies: 1
    Last Post: 07-31-2002, 10:49 AM