TimTimTim
TimTimTim

Reputation: 93

How do I draw an half Circle in opengl

I use this method which works perfectly to draw a full circle(might be typos in code text I wrote from memory):

drawCircle(GLAutodrawble drawble){
  GL gl = drawble.getGL();
  gl.glTranslatef(0.0f,0.0f,-7.0f);
  gl.glBeginf(GL_LINE_LOOP);
  gl.glColorf(0.0f,0.0f,0.0);
  final dobule  PI = 3.141592654;
  double angle = 0.0;
  int points = 100;
  for(int i =0; i < points;i++){
    angle = 2 * PI * i / points;
    gl.glVertexf((float)Math.cos(angle),(float)Math.sin(angle));
  }
  gl.glScalef(1.0f,1.0f,0.0f);
  gl.glEnd();
}

I want to use the same priciples to make method to make a half circle, I don't get my head around what I should do with the cos sin stuff. Can anyone take a look and help me.

Thanks goes to all that thakes a look at the problem!

Upvotes: 4

Views: 11538

Answers (3)

Clickmit Wg
Clickmit Wg

Reputation: 553

Drawing circle is like the drawing a lines , connecting them. And the points should too close to each other to create a smooth curve. you can use the following code to draw a half circle in opengl.

 float PI = 3.14
 float step=5.0;// How far is the next point i.e it should be small value
 glBegin(GL_LINE_STRIP)
 for(float angle=0.0f,angle<=180; angle+=step)
 {
    float rad  = PI*angle/180;
   x  = centerX+radius*cos(rad);
   y  = centerY+radius*sin(rad);
   glVertex(x,y,0.0f);

}
glEnd();

Upvotes: 0

Smi
Smi

Reputation: 14346

Change this line:

angle = 2 * PI * i / points;

to this:

angle = 1 * PI * i / points;

Upvotes: 2

giorashc
giorashc

Reputation: 13713

Replace :

angle = 2 * PI * i / points;

With :

angle = PI * i / points;

Notice I removed the 2 multiplier as 2*PI is 360 (in degrees) which is a full circle. PI (180 degrees) is half a circle

Upvotes: 5

Related Questions