算法已不再赘述。具体学习请访问:
http://blog.csdn.net/codeblocksm/article/details/50991357
这里需要指出的是要将抛物线分成两部分。在斜率为1之前是一个部分,在斜率是1之后是一个部分。前面用x来自增,后面用y来自增。
献出手稿:
又到了开心的贴代码的时间了 (///_///)
//所给代码在VS2013下运行和调试成功
#include <stdafx.h>//在win32控制台下运行的,其他地方使用该代码的时候可删除..
#include <GL/glut.h>
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <algorithm>
using namespace std;
void init(){
glClearColor(0.0, 0.0, 0.0, 0.0);
glMatrixMode(GL_PROJECTION);
gluOrtho2D(-100.0, 100.0, -10.0, 140.0);
}
void setPixel(GLint x, GLint y)
{
glBegin(GL_POINTS);
glVertex2i(x, y);
glEnd();
}
void parabola()
{
double x = 0, y = 0;
double d = -7. / 16;
double pre = 0;
glClear(GL_COLOR_BUFFER_BIT);
glColor3f(1.0, 0.0, 0.0);
while (x <= 8)
{
if (d < 0)
d += (3 + 2 * x) / 16;
else
d += (-13 + 2 * x) / 16, pre = y, y++;
x++;
setPixel(x, y);
setPixel(-x, y);
}
setPixel(x, y);
x = 8, y = 4, pre = 8;
d = (17 * 17) / (16. * 4) - 5.;
while (y <= 110)
{
if (d >= 0)
d -= 1;
else
d += (x / 8. - 7. / 8.), pre = x, x++;
y++;
setPixel(x, y);
setPixel(-x, y);
}
glFlush();
}
void main(int argc, char ** argv)
{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);
glutInitWindowPosition(50, 100);
glutInitWindowSize(400, 300);
glutCreateWindow("wsm: y = x*x / 16 ");
init();
glutDisplayFunc(parabola);
glutMainLoop();
}