Thread: Newbie Question: How to do circles with C

  1. #16
    Registered User joed's Avatar
    Join Date
    Mar 2004
    Posts
    59
    This should be right, if not, lemme know. I tried to shorten in a bit. Supposedly this can be modified to do ovals but I've not tried it.

    Code:
    void circle(int x, int y, int r)
    {
    	r++;
    	
    	int target = 0;
    	int a = r;
    	int b = 0;
    	int i;
    	
    	while(a >= b) {
    		b = sqrt(r * r - a * a);
    		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--;
    	}
    }

  2. #17
    Registered User
    Join Date
    Apr 2006
    Posts
    10
    Quote Originally Posted by joed
    This should be right, if not, lemme know. I tried to shorten in a bit. Supposedly this can be modified to do ovals but I've not tried it.

    Code:
    void circle(int x, int y, int r)
    {
    	r++;
    	
    	int target = 0;
    	int a = r;
    	int b = 0;
    	int i;
    	
    	while(a >= b) {
    		b = sqrt(r * r - a * a);
    		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--;
    	}
    }
    I've tried it several times and it seems fine
    Thanks

    P.S. Do you also know how to make ovals? It would be helpful too.

  3. #18
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,663
    Yes, you goto www.mathworld.com and look up the relevent parametric equations.
    Then you put that into the code we've shown you how to do.
    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.

  4. #19
    Registered User
    Join Date
    Apr 2006
    Posts
    10
    Quote Originally Posted by Salem
    Yes, you goto www.mathworld.com and look up the relevent parametric equations.
    Then you put that into the code we've shown you how to do.
    What I really wanted wasnīt an oval but an ellipse.
    I tried that site and I found the formula but I don't have the knowladge to make any sort of ellipses .

  5. #20
    Registered User
    Join Date
    Mar 2006
    Posts
    725
    Just replace the circle formula in your code to the elipse formula. Is there any specific part of the equation you don't understand?
    Code:
    #include <stdio.h>
    
    void J(char*a){int f,i=0,c='1';for(;a[i]!='0';++i)if(i==81){
    puts(a);return;}for(;c<='9';++c){for(f=0;f<9;++f)if(a[i-i%27+i%9
    /3*3+f/3*9+f%3]==c||a[i%9+f*9]==c||a[i-i%9+f]==c)goto e;a[i]=c;J(a);a[i]
    ='0';e:;}}int main(int c,char**v){int t=0;if(c>1){for(;v[1][
    t];++t);if(t==81){J(v[1]);return 0;}}puts("sudoku [0-9]{81}");return 1;}

  6. #21
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,663
    Circle
    x = r * cos( theta );
    y = r * sin( theta );

    Ellipse
    x = r1 * cos( theta );
    y = r2 * sin( theta );

    A circle is just an ellipse with r1 == r2

    See, you latched onto some quick hack which did what you want without having any clue about what it really meant.
    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.

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, 06:45 PM
  4. a stupid question from a newbie
    By newcomer in forum C++ Programming
    Replies: 4
    Last Post: 01-11-2003, 04:38 PM
  5. newbie class templates question
    By daysleeper in forum C++ Programming
    Replies: 2
    Last Post: 09-18-2001, 09:50 AM