实现绘制正多边形函数

/***

函数名称:DrawRegularPolygon2

函数参数1:正多边形顶点数量

函数参数2:偏移的弧度

***/

 

void DrawRegularPolygon2(const UINT ptNumber, INT AngleOffset)

{


    //小于3无法形成多边形
    ASSERT(ptNumber >= 3);

    //擦除窗口
    glClear(GL_COLOR_BUFFER_BIT);

    //设置点颜色
    glColor3f(1,1,1);

    //设置世界窗口
    SetWorldWindow(-10, 10, -10, 10);

    //设置视口
    SetViewPort(0, m_fWidth, 0, m_fHeight);

    //定义半径
    GLfloat Radius = 8.0;

    //定义π
    GLfloat pi = 3.1415926;

    //偏移弧度转换
    GLfloat offset = AngleOffset/pi*180;

    GLPoint *pt = new GLPoint[ptNumber+1];    //注意:第0个元素不用

    //计算并绘顶点
    glBegin(GL_POINTS);

    glVertex2f(0, 0);

    //这里利用数学公式:P(i) = (S*R*cos(i*a + os), S*R*sin(i*a + os))算出顶点的坐标
    //其中P(i)代表第i个顶点
    //a = π/顶点数量*2
    //os 为偏移弧度,可利用此参数实现旋转
    //R 为多边形外圆的半径
    //另外,可以增加参数S以实现缩放,只需将R*S即可(本函数中并未实现缩放)

    for(int i=1; i<=ptNumber; ++i)
    {
        pt[i].x = Radius*cos(i*(pi/ptNumber*2)+offset);
        pt[i].y = Radius*sin(i*(pi/ptNumber*2)+offset);

        glVertex2f(pt[i].x, pt[i].y);
    }

    glEnd();

    //连线
    glBegin(GL_LINE_LOOP);
    for(int i=1; i<=ptNumber; ++i)
    {
        glVertex2f(pt[i].x, pt[i].y);
    }

    glEnd();

    delete [] pt;
    pt = NULL;

    glFlush();

}

 

你可能感兴趣的:(OS,null,delete,buffer)