干掉那个BUG 2017-02-22 14:33 采纳率: 33.3%
浏览 1734

OpenGL关于画圆饼的问题

#include
#include
#include

const GLdouble twoPi = 6.283185;
class scrPr
{
public:
GLint x, y;
};

GLsizei winWidth = 400, winHeight = 300;
scrPr circCtr, piePr;
GLint radius = winWidth / 4;
void init(void)
{
glClearColor(1.0, 1.0, 1.0, 1.0);
glMatrixMode(GL_PROJECTION);
gluOrtho2D(0.0, 200.0, 0.0, 150.0);
}

void circleMidpoint(scrPr &cir, int r) //中点画圆函数
{
int x = cir.x, y = cir.y, d, deltax, deltay;
deltax = 3; deltay = 2 - r - r; d = 1 - r;
glVertex2i(x, y);
while (x<y)
{
if (d<0)
{
d += deltax;
deltax += 2;
x++;
}
else
{
d += (deltax + deltay);
deltax += 2; deltay += 2;
x++; y--;
}
glVertex2i(x, y);
glVertex2i(y, x);
glVertex2i(x, cir.y+cir.y-y);
glVertex2i(cir.y + cir.y - y, x);
glVertex2i(cir.x+cir.x-x,y);
glVertex2i(y, cir.x + cir.x - x);
glVertex2i(cir.x + cir.x - x, cir.y + cir.y - y);
glVertex2i(cir.y + cir.y - y, cir.x + cir.x - x);
}
}
void pieChart(void)
{
GLdouble sliceAngle, previousSliceAngle = 0.0;

GLint k, nSlices = 12;
GLfloat dataValues[12] = { 10.0, 7.0, 13.0, 5.0, 13.0, 14.0, 3.0, 16.0, 5.0, 3.0, 17.0, 8.0 };
GLfloat dataSum = 0.0;
glClear(GL_COLOR_BUFFER_BIT);
glColor3f(0.0, 0.0, 0.0);
circCtr.x = winWidth / 2;
circCtr.y = winHeight / 2;
glBegin(GL_POINTS);
circleMidpoint(circCtr, radius);
glEnd();
for ( k = 0; k <nSlices; k++)
{
    dataSum += dataValues[k];
}
for ( k = 0; k <nSlices; k++)
{
    sliceAngle = twoPi*dataValues[k] / dataSum + previousSliceAngle;
    piePr.x = circCtr.x + radius*cos(sliceAngle);
    piePr.y = circCtr.x + radius*sin(sliceAngle);
    glBegin(GL_LINES);
        glVertex2i(circCtr.x, circCtr.y);
        glVertex2i(piePr.x, piePr.y);
    glEnd();
    previousSliceAngle = sliceAngle;
}                             

}

void displayFunc(void)
{
glClear(GL_COLOR_BUFFER_BIT);
glColor3f(0.0, 0.0, 1.0);
pieChart();
glFlush();
}

void winReshapeFcn(int newWidth, int newHeight)
{
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluOrtho2D(0.0, GLdouble(newWidth), 0.0, GLdouble(newHeight));

glClear(GL_COLOR_BUFFER_BIT);

winWidth = newWidth;
winHeight = newHeight;

}

void main(int argc, char**argv)
{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);
glutInitWindowPosition(100, 100);
glutInitWindowSize(winWidth,winHeight);
glutCreateWindow("pieChart");

init();
glutDisplayFunc(displayFunc);
glutReshapeFunc(winReshapeFcn);

glutMainLoop();

}

要的效果是一个圆饼。但是我只能得到一族以圆心为中心的射线,得不到圆饼图。求解答!!!

  • 写回答

1条回答 默认 最新

  • threenewbee 2017-02-22 14:34
    关注
    评论

报告相同问题?

悬赏问题

  • ¥15 有卷积神经网络识别害虫的项目吗
  • ¥15 数据库数据成问号了,前台查询正常,数据库查询是?号
  • ¥15 算法使用了tf-idf,用手肘图确定k值确定不了,第四轮廓系数又太小才有0.006088746097507285,如何解决?(相关搜索:数据处理)
  • ¥15 彩灯控制电路,会的加我QQ1482956179
  • ¥200 相机拍直接转存到电脑上 立拍立穿无线局域网传
  • ¥15 (关键词-电路设计)
  • ¥15 如何解决MIPS计算是否溢出
  • ¥15 vue中我代理了iframe,iframe却走的是路由,没有显示该显示的网站,这个该如何处理
  • ¥15 操作系统相关算法中while();的含义
  • ¥15 CNVcaller安装后无法找到文件