0

hi,

I am trying to draw a circle without using cos,sin;

Mainly by the formula x^2+y^2 = r^2;

this is my code , but for some reason its not working. any help?
note: I thought this code would draw the upper halve fo the portion.

float xCor,yCor;
xCor=yCor =0.0;

float five,two;
five = 5.0;
tow = 2.0;

for(float xc=0.0, yc = 0.0; xc <= 5; xc+=0.1, yc+=0.1)
		{
			xCor = sqrt( pow(five,two) - pow(yc,two) );
			yCor = sqrt( pow(five,two) - pow(xc,two) );

			glVertex2f(xCor,yCor);
		}
3
Contributors
4
Replies
6
Views
8 Years
Discussion Span
Last Post by MattEvans
-1

Why the hell are you writing "two = 2.0"? That's retarded.

The problem with your code should be obvious if you stepped through the calculations by hand.

Votes + Comments
two? I see tow = 2.0, next time please be more careful honey!
0

the equation you wrote is a 2d equality, via rearangement you can make an equation in terms of either 'x' or 'y', e.g.:

x = sqrt ( r^2 - y^2 )

So, you only need to vary 'y', plug-in any such value of 'y' and you'll get one of the two values of x.

as rashakil said, you really don't need to name five and two. if five is meant to be the radius, name it radius, since that's a sensible name.

as for using floating-point pow ( x, y ) when 'y' is 2 or 2.0... don't do that. it tends to run much slower than just doing ( x * x ). i usually have this defined somwhere:

template < typename T > inline T square ( T x )
{ return ( x * x ); }
0

Varying the y or x did not work either?

    for(float xc=0.0, yc = 0.0; xc <= 5; xc+=0.1,yc+=0.1 )
    {
        //xCor = sqrt( pow(five,two) - pow(yc,two) );
        yCor = (double)sqrt( pow(five,two) - pow(xc,two) );

        glVertex2f(xc,yCor);
    }

    glEnd();

Edited by mike_2000_17: Fixed formatting

0

Works ok for me... Try this:

const double radius = 5.0;

  glDisable ( GL_LIGHTING );
  glColor3d ( 1.0, 1.0, 1.0 );

  glBegin ( GL_LINE_STRIP );

  for ( double y = -radius; y <= radius; y+= 0.1 ) {
    double x = sqrt ( ( radius * radius ) - ( y * y ) );
    glVertex2d ( x, y );
  }

  glEnd ( );
This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.