Thread: Half of the circle

  1. #1
    Computer guy
    Join Date
    Sep 2005
    Location
    I'm lost!!!
    Posts
    200

    Half of the circle

    So i ran into a problem with circle. I could draw a whole circle easily, but when i tried half of the circle, it doesn't go in the way i wanted.

    in my draw circle function, i set the vectors' coordinate base on the information of the class. Then use "gl triangle fan" to draw the circle:
    Code:
    vect2* BuildBar()
    	{
    		//Pi() is the function return Pi's value
    		Vertices = new vect2[numVertices+2];
    		for(int j=0; j<numVertices+2; j++)
    		{
    			double a =  2*Pi() * (j/double(numVertices));
    			Vertices[j] = vect2(cos(a), sin(a)) * (R); //R is the radius
    		}
    		return Vertices;
    	}
    void Render()
    	{
    
    		glLoadIdentity();		
    		glTranslatef(Pos.x, Pos.y, 0);
    		glBegin(GL_TRIANGLE_FAN);
    			for(int j=0; j<numVertices+2; j++)
    			{					
    				glVertex2d(Vertices[j].x, Vertices[j].y);
    			}			
    		glEnd();
    
    	}
    and this is how it looks like:
    http://www.albumtown.com/data/953ecc...66_p935793.gif

    But my intention is just only part of the circle. I was trying to get something like this:
    http://www.albumtown.com/data/953ecc...66_p935796.gif


    so i change my build function and tried to get that half circle:
    Code:
    vect2* BuildBar()
    	{
    		/**/
    		Vertices = new vect2[numVertices+2];
    		for(int j=0; j<numVertices+2; j++)
    		{
    			double a =  Pi()/1.5 * (j/double(numVertices));	
    		                Vertices[j] = vect2(cos(a), sin(a)) * (R);
    		}
    		return Vertices;
    	}
    and i get this:
    http://www.albumtown.com/data/953ecc...66_p935794.gif

    No matter what way i rotate, the whole thing just won't stay straight like the second picture. So, what should i set for the sin() and cos() in order to straight up the circle like the second picture?
    Last edited by hdragon; 03-07-2006 at 01:37 PM.
    Hello, testing testing. Everthing is running perfectly...for now

  2. #2
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    What did you do to your function?
    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.

  3. #3
    Computer guy
    Join Date
    Sep 2005
    Location
    I'm lost!!!
    Posts
    200
    Sorry, the first one suppose to be:
    Code:
    double a =  2*Pi() * (j/double(numVertices));
    Hello, testing testing. Everthing is running perfectly...for now

  4. #4
    ---
    Join Date
    May 2004
    Posts
    1,379
    This is a little non-related, but why do you have a function returning pi? Why not a constant?

  5. #5
    Computer guy
    Join Date
    Sep 2005
    Location
    I'm lost!!!
    Posts
    200
    Quote Originally Posted by sand_man
    This is a little non-related, but why do you have a function returning pi? Why not a constant?
    Well, either way i still need a Pi value. It doesn't matter that Pi is a function or constant, as long as there is a value lol.
    Hello, testing testing. Everthing is running perfectly...for now

  6. #6
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    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.

  7. #7
    ---
    Join Date
    May 2004
    Posts
    1,379
    Quote Originally Posted by hdragon
    Well, either way i still need a Pi value. It doesn't matter that Pi is a function or constant, as long as there is a value lol.
    Yeah I understand but a function is just an unneeded slowdown.

  8. #8
    Absent Minded Programmer
    Join Date
    May 2005
    Posts
    968
    True, there may be no difference, but the point remains..

    Using a function there is just plain silly!

    Just do this

    #Define PI = 3.14############....

    Simple as that :d
    Sometimes I forget what I am doing when I enter a room, actually, quite often.

  9. #9
    Registered User VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,607
    Calling a function necessitates stack overhead that a common variable does not have. Therefore calling a function to do what a simple variable can accomplish is wasteful.

  10. #10
    Absent Minded Programmer
    Join Date
    May 2005
    Posts
    968
    Either way, no one has still answered his question, which I believe is a bit more important that the tiny bit of overhead he is getting from the function call, or is it just me?

    Sorry I don't know the answer :d
    Sometimes I forget what I am doing when I enter a room, actually, quite often.

  11. #11
    Registered User VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,607
    Vertices[j] = vect2(cos(a), sin(a)) * (R); //R is the radius
    This is your problem.

    A circle is (r cos(a),r sin(a))

    Why are you only using the cosine for X and not factoring in the radius?

  12. #12
    S Sang-drax's Avatar
    Join Date
    May 2002
    Location
    Göteborg, Sweden
    Posts
    2,072
    Bubba I think you misread his code. He is doing exactly that, but using () around R for some reason.
    Last edited by Sang-drax : Tomorrow at 02:21 AM. Reason: Time travelling

  13. #13
    Registered User VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,607
    Ahh I see it now.

    He is doing this:

    Vect2.x*=r;
    Vect2.y*=r;

  14. #14
    S Sang-drax's Avatar
    Join Date
    May 2002
    Location
    Göteborg, Sweden
    Posts
    2,072
    By the way, your result isn't strange at all if you consider that the axes aren't oriented the same way on a computer as they are in your math book. This should work:
    Code:
    double a =  -0.5*Pi() * (j/double(numVertices));
    Vertices[j] = vect2(cos(a), sin(a)) * (R);
    Last edited by Sang-drax : Tomorrow at 02:21 AM. Reason: Time travelling

  15. #15
    Computer guy
    Join Date
    Sep 2005
    Location
    I'm lost!!!
    Posts
    200
    Hey sang-drax, your code works fine but it still doesn't give me the half circle with the curve straight up. Anyways, thank you all though. I figured instead using the sin() and cos() to draw part of the circle, i'm gonna define the coordinates manually. Then, i would draw a square box and put texture on. Therefore, i will have a physical, and renderable object work at the same time. The renderable object will look better since it doesn't have to worry about other stuff (collision,...).
    Hello, testing testing. Everthing is running perfectly...for now

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. circle problem
    By gunghomiller in forum C++ Programming
    Replies: 10
    Last Post: 07-14-2007, 06:40 PM
  2. Half life Problem which I am right and the teacher is wrong
    By Shadow12345 in forum A Brief History of Cprogramming.com
    Replies: 13
    Last Post: 11-06-2002, 04:28 PM
  3. circle filling
    By DavidP in forum A Brief History of Cprogramming.com
    Replies: 4
    Last Post: 06-08-2002, 05:37 AM
  4. The glass is half full
    By dirkduck in forum A Brief History of Cprogramming.com
    Replies: 11
    Last Post: 03-30-2002, 03:08 PM
  5. point lies in circle?
    By cozman in forum Game Programming
    Replies: 3
    Last Post: 12-20-2001, 04:39 PM