博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
OpenGL——圆公式相关变化的绘制
阅读量:6787 次
发布时间:2019-06-26

本文共 2980 字,大约阅读时间需要 9 分钟。

#include
#include
//旧版本 固定管线#include
#include
using namespace std;const GLdouble twoPi = 6.283185;struct screenPt { GLint x, y;};typedef enum{ limacon =1, cardioid, threeLeaf, fourLeaf, spiral} curveName;GLsizei winWidth = 600, winHeight = 500;void init(){ //窗口背景为白色 glClearColor(1, 1, 1, 1); glMatrixMode(GL_PROJECTION); gluOrtho2D(0.0, 200.0, 0.0, 150.0);}void lineSegment(screenPt pt1, screenPt pt2){ glBegin(GL_LINES); glVertex2i(pt1.x, pt1.y); glVertex2i(pt2.x, pt2.y); glEnd();}void drawCurve(GLint curveNum){ const GLdouble twoPi = 6.283185; const GLint a = 175, b = 60; GLfloat r, theta, dtheta = 1.0 / float(a); GLint x0 = 200, y0 = 250; screenPt curvePt[2]; glColor3f(0.0, 0.0, 0.0); curvePt[0].x = x0; curvePt[0].y = y0; switch (curveNum) { case limacon: curvePt[0].x += a + b; break; case cardioid: curvePt[0].x += a + a; break; case threeLeaf: curvePt[0].x += a; break; case fourLeaf: curvePt[0].x += a; break; case spiral: break; default: break; } theta = dtheta; while (theta < twoPi) { switch (curveNum) { case limacon: r = a * cos(theta) + b; break; case cardioid: r = a * (1 + cos(theta)); break; case threeLeaf: r = a * cos(theta * 3); break; case fourLeaf: r = a * cos(theta * 2); break; case spiral: r = (a / 4.0) * theta; break; default: break; } curvePt[1].x = x0 + r * cos(theta); curvePt[1].y = y0 + r * sin(theta); lineSegment(curvePt[0], curvePt[1]); curvePt[0].x = curvePt[1].x; curvePt[0].y = curvePt[1].y; theta += dtheta; }}void displayFcn(){ GLint curveNum; glClear(GL_COLOR_BUFFER_BIT); glColor3f(0.0, 0.0, 1.0); cout << "选择下列任意图形进行绘制\n"; cout << "1-limacon, 2-cardioid, 3-threeLeaf, 4-fourLeaf, 5-spiral\n"; if (curveNum == 1 || curveNum == 2 || curveNum == 3 || curveNum == 4 || curveNum == 5) { drawCurve(curveNum); } else { exit(0); } glFlush(); }void winReshapeFcn(GLint newWidth, GLint newHeight){ glMatrixMode(GL_PROJECTION); glLoadIdentity(); gluOrtho2D(0.0, GLdouble(newWidth), 0.0, GLdouble(newHeight)); glClear(GL_COLOR_BUFFER_BIT); winWidth = newWidth; winHeight = newHeight;}int main(int argc, char* argv[]){
glutInit(&argc, argv); glutInitDisplayMode(GLUT_RGB | GLUT_SINGLE); glutInitWindowPosition(100, 100); glutInitWindowSize(winWidth, winHeight); glutCreateWindow("Curves"); init(); glutDisplayFunc(displayFcn); glutReshapeFunc(winReshapeFcn); glutMainLoop(); system("pause"); return 0;}

 

转载于:https://www.cnblogs.com/farewell-farewell/p/9403757.html

你可能感兴趣的文章
Springboot中关于跨域问题的一种解决方法
查看>>
PHP和Apache的安装
查看>>
要让div中的float不会自动显示到下一行来?
查看>>
五种排序方法(选择、冒泡、快排、插入、希尔)
查看>>
位运算及其应用实例(1)
查看>>
解决cocos2d 热更是连不上https服务器
查看>>
vim相关
查看>>
捐助账号
查看>>
线程交替运行
查看>>
ubuntu10.04 –像QQ一样截屏,注解
查看>>
三年观察揭示TNF抑制剂持续改善强柱患者躯体功能的预测因子
查看>>
数据库练习
查看>>
mongodb的开机自启动
查看>>
1303: [CQOI2009]中位数图
查看>>
1011: [HNOI2008]遥远的行星
查看>>
QTP的那些事--有关一个webtable数据的获取案例
查看>>
20190520
查看>>
《Python 二三事》——python学习必看(转载)
查看>>
Minimum Spanning Tree.prim/kruskal(并查集)
查看>>
北邮14&18年软院机试【参考】答案
查看>>