Advertisement
sci4me

drawCircle

Aug 6th, 2016
672
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
D 0.77 KB | None | 0 0
  1. void drawCircle(bool filled = true)(float cx, float cy, float radius, int num_segments) {
  2.     float theta = 2 * PI / cast(float) num_segments;
  3.     float c = cos(theta);
  4.     float s = sin(theta);
  5.     float t;
  6.     float x = radius;
  7.     float y = 0;
  8.  
  9.     static if(filled) {
  10.         float lastX;
  11.         float lastY;
  12.  
  13.         glBegin(GL_TRIANGLES);
  14.         for(int i = 0; i <= num_segments; i++) {
  15.             t = x;
  16.             x = c * x - s * y;
  17.             y = s * t + c * y;
  18.  
  19.             if(i > 0) {
  20.                 glVertex2f(cx + lastX, cy + lastY);
  21.                 glVertex2f(cx + x, cy + y);
  22.                 glVertex2f(cx, cy);
  23.             }
  24.  
  25.             lastX = x;
  26.             lastY = y;
  27.         }
  28.         glEnd();
  29.     } else {
  30.         glBegin(GL_LINE_LOOP);
  31.         for(int i = 0; i < num_segments; i++) {
  32.             glVertex2f(cx + x, cy + y);
  33.  
  34.             t = x;
  35.             x = c * x - s * y;
  36.             y = s * t + c * y;
  37.         }
  38.         glEnd();
  39.     }
  40. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement