简体   繁体   中英

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!

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

Change this line:

angle = 2 * PI * i / points;

to this:

angle = 1 * PI * i / points;

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM