Newbie Question: How to do circles with C

This is a discussion on Newbie Question: How to do circles with C within the C Programming forums, part of the General Programming Boards category; I need help making a circle using only ASCII characters in a bidimensional array. Thanks in advance...

  1. #1
    Registered User
    Join Date
    Apr 2006
    Posts
    10

    Newbie Question: How to do circles with C

    I need help making a circle using only ASCII characters in a bidimensional array.
    Thanks in advance

  2. #2
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,484
    What sort of help?
    - defining a 2D array?
    - printing it
    - putting some symbol at specific positions in the grid
    - the function for the perimeter of a circle?
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  3. #3
    Registered User
    Join Date
    Apr 2006
    Posts
    10
    I have everything working except the function for the perimeter of the circle.

  4. #4
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,484
    x = r * cos( theta );
    y = r * sin( theta );
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  5. #5
    Registered User
    Join Date
    Apr 2006
    Posts
    10
    oooooooooooXXXXXX
    ooooooooooXooooooX
    oooooooooXooooooooX
    oooooooooXooooooooX
    oooooooooXooooooooX
    oooooooooXooooooooX
    oooooooooXooooooooX
    oooooooooXooooooooX
    ooooooooooXooooooX
    oooooooooooXXXXXX

    I used those and I got this. It is a circle but some time ago I saw an other way to get a better circle(don't remember where). Is there any algorithm to make a better circle?
    Thanks for the help anyway
    Last edited by Johanson; 04-03-2006 at 10:16 AM.

  6. #6
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,484
    Post your code, that seems rather too octagonal.

    Use [code][/code] tags when posting code (and your fixed-width output).
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  7. #7
    Registered User
    Join Date
    Apr 2006
    Posts
    10
    Code:
    void TestCircle(DRAWING draw,int a,int b,int r) /* DRAWING is a type of bidemensional arrays of the type char */
    { int x,y;
      double theta=0;
      
      for(;theta!=360.0 ;theta+=1)
      {
        x = a + r * cos( theta );
        y = b + r * sin( theta );
        des[x][y]='X';
      }
    }

  8. #8
    Supermassive black hole cboard_member's Avatar
    Join Date
    Jul 2005
    Posts
    1,709
    Just some cleanups

    Code:
    void TestCircle(DRAWING draw,int a,int b,int r) /* DRAWING is a type of bidemensional arrays of the type char */
    { 
      int x,y;  /* put nothing on the same line as a brace */
      double theta=0;
      
      for(;theta!=360.0 ;theta++)  /* ++ is the same as += 1 */
      {
        x = a + r * cos( theta );
        y = b + r * sin( theta );
        des[x][y]='X';
      }
    }
    I was bored. Again.
    EDIT: And you have a space button for a reason. Jeeeeeesus.
    Last edited by cboard_member; 04-03-2006 at 12:27 PM.
    Good class architecture is not like a Swiss Army Knife; it should be more like a well balanced throwing knife.

    - Mike McShaffry

  9. #9
    Mad OnionKnight's Avatar
    Join Date
    Jan 2005
    Location
    Umeň, Sweden
    Posts
    555
    The problem is probably that you are outputting the circle on a console screen where the height of a character is about double the length of the width so you need to change that 2:1 ratio to 1:1 to get a circle instead of an oval.

  10. #10
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,484
    > for(;theta!=360.0 ;theta+=1)
    Additionally, the math trigonometry functions take radians, not degrees.
    There are 2PI radians in a full circle, which means you go round about 60 times more than necessary.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  11. #11
    Registered User
    Join Date
    Mar 2006
    Posts
    725
    @ahluka:
    using operator++() on double theta is not really a good idea.

    You might want to do (theta < 2*PI; theta += 0.1) to get some accuracy.

    Console screen text is typically 8x4 pixels per character. One quick fix would be to create des[][] with half the width, then double it later. This wold however create a possibly-annoying "twin character syndrome".

  12. #12
    Super Moderator VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,596
    #define PI 3.14159
    #define DEGTORAD(x) ( (x) * (PI) / (180.0) )

  13. #13
    Registered User joed's Avatar
    Join Date
    Mar 2004
    Posts
    59
    Just for fun, here's an easy way to draw circles, adapted from "8088 Macro Assembler Programming" by Dan Rollins. Could be shortened/optimized a bit:

    Code:
    void circle(int x, int y, int r)
    {
    	int target = 0;
    	int a = r;
    	int b = 0;
    	int dec, count;
    	
    	while(a > b) {
    		b = (r * r - a * a);
    		count = 0;
    		dec = 1;
    		while(b >= 0) {
    			b -= dec;
    			dec += 2;
    			count++;
    		}
    		b = count;
    		SWAP(target, b);
    		while(b < target) {
    			plot(x + a, y + b);
    			plot(x - a, y + b);
    			plot(x - a, y - b);
    			plot(x + a, y - b);
    			plot(x + b, y + a);
    			plot(x - b, y + a);
    			plot(x - b, y - a);
    			plot(x + b, y - a);
    			b++;
    		}
    		a--;
    	}
    }

  14. #14
    Registered User
    Join Date
    Apr 2006
    Posts
    10
    Quote Originally Posted by joed
    Just for fun, here's an easy way to draw circles, adapted from "8088 Macro Assembler Programming" by Dan Rollins. Could be shortened/optimized a bit:

    Code:
    void circle(int x, int y, int r)
    {
    	int target = 0;
    	int a = r;
    	int b = 0;
    	int dec, count;
    	
    	while(a > b) {
    		b = (r * r - a * a);
    		count = 0;
    		dec = 1;
    		while(b >= 0) {
    			b -= dec;
    			dec += 2;
    			count++;
    		}
    		b = count;
    		SWAP(target, b);
    		while(b < target) {
    			plot(x + a, y + b);
    			plot(x - a, y + b);
    			plot(x - a, y - b);
    			plot(x + a, y - b);
    			plot(x + b, y + a);
    			plot(x - b, y + a);
    			plot(x - b, y - a);
    			plot(x + b, y - a);
    			b++;
    		}
    		a--;
    	}
    }
    Thans a lot. This is exactly what I wanted

    Edit:

    The only problem is that when the radius=2, it won't work correctly. It shows this:

    ooXoo
    ooooo
    XoooX
    ooooo
    ooXoo
    Last edited by Johanson; 04-08-2006 at 07:07 AM.

  15. #15
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,046
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Stupid Newbie question
    By TimL in forum C++ Programming
    Replies: 4
    Last Post: 07-22-2008, 04:43 AM
  2. Newbie with Very Newbie Question
    By Jedi_Mediator in forum C++ Programming
    Replies: 18
    Last Post: 07-01-2008, 08:00 AM
  3. C prog newbie question
    By Draginzuzu in forum C Programming
    Replies: 1
    Last Post: 02-03-2003, 05:45 PM
  4. a stupid question from a newbie
    By newcomer in forum C++ Programming
    Replies: 4
    Last Post: 01-11-2003, 03:38 PM
  5. newbie class templates question
    By daysleeper in forum C++ Programming
    Replies: 2
    Last Post: 09-18-2001, 09:50 AM

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