OpenGL 利用中点算法画抛物线:y = x*x / 16

算法已不再赘述。具体学习请访问:
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();

}

这里写图片描述

评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值