干掉那个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 【提问】基于Invest的水源涵养
  • ¥20 微信网友居然可以通过vx号找到我绑的手机号
  • ¥15 寻一个支付宝扫码远程授权登录的软件助手app
  • ¥15 解riccati方程组
  • ¥15 display:none;样式在嵌套结构中的已设置了display样式的元素上不起作用?
  • ¥15 使用rabbitMQ 消息队列作为url源进行多线程爬取时,总有几个url没有处理的问题。
  • ¥15 Ubuntu在安装序列比对软件STAR时出现报错如何解决
  • ¥50 树莓派安卓APK系统签名
  • ¥65 汇编语言除法溢出问题