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);
}``````

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.

commented: two? I see tow = 2.0, next time please be more careful honey! -4

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 ); }``````

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();
``````

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 ( );``````
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.