画线算法 中点画线 Bresenham DDA

画线算法 中点画线 Bresenham DDA

  • Reshape 在模板那里介绍
#include<GL/glut.h>
#include<math.h>
#pragma comment(linker,"/subsystem:\"windows\" /entry:\"mainCRTStartup\"")
//DDA画法(依据斜率)
void DDA(int x1, int y1, int x2, int y2)
{
    int k, i;
    float x, y, dx, dy;
    if (abs(x2 - x1) < abs(y2 - y1)) //斜率的绝对值大于1,每次沿y方向加减一个单位 //以大的为准,应该是精度高呗
        k = abs(y2 - y1);
    else k = abs(x2 - x1);
    dx = float(x2 - x1) / k;
    dy = float(y2 - y1) / k; //〒_〒,这点写错了,float是必须的,要不整除会变成0,纠结我好长时间
    x = float(x1);
    y = float(y1);
    glColor3f(1.0f, 1.0f, 0.0f);
    glPointSize(5);
    for (i = 0; i < k; i++)
    {
        glBegin(GL_POINTS);
        glVertex2i((int)(x + 0.5), (int)(y + 0.5));
        glEnd();
        x += dx;
        y += dy;
    }
}
//中点画线算法(模拟/逐点比较)
void MidPointLine(int x1, int y1, int x2, int y2)
{
    int a, b, dt1, dt2, d, x, y;
    a = y1 - y2;
    b = x2 - x1;
    d = a + a + b;  //为了避免小数,这里取2倍
    dt1 = a + b + a + b;
    dt2 = a + a;
    x = x1;
    y = y1;
    glColor3f(1.0f, 0.0f, 0.0f);
    glBegin(GL_POINTS);
    glVertex2i(x, y);
    glEnd();
    while (x < x2)
    {
        if (d < 0)
        {
            x++;
            y++;
            d += dt1;
        }
        else
        {
            x++;
            d += dt2;
        }
        glBegin(GL_POINTS);
        glVertex2i(x, y);
        glEnd();
    }
}
//Bresenham画线算法(插值法)
void Bresenham(int x1, int y1, int x2, int y2)
{
    int x, y, dx, dy, d, d1, d2, inc, temp;
    dx = x2 - x1;
    dy = y2 - y1;
    if (dx * dy >= 0)
        inc = 1;
    else
        inc = -1;
    glColor3f(0.0f, 1.0f, 0.0f);
    if (abs(dx) > abs(dy))
    {
        if (dx < 0)  //将2a,3a区域的直线变换到1a,4a区域
        {
            temp = x1;  x1 = x2;    x2 = temp;
            temp = y1;  y1 = y2;    y2 = temp;
            dx = -dx;   dy = -dy;
        }
        d = 2 * dy - dx;
        d1 = 2 * dy;
        d2 = 2 * (dy - dx);
        x = x1;
        y = y1;
        glBegin(GL_POINTS);
        glVertex2i(x, y);
        glEnd();
        while (x < x2)
        {
            x++;
            if (d < 0)
                d += d1;
            else
            {
                y += inc;
                d += d2;
            }
            glBegin(GL_POINTS);
            glVertex2i(x, y);
            glEnd();
        }
    }
    else
    {
        if (dy < 0)  //将3b,4b区域的直线变换到1b,2b区域
        {
            temp = x1;  x1 = x2;    x2 = temp;
            temp = y1;  y1 = y2;    y2 = temp;
            dx = -dx;   dy = -dy;
        }
        d = 2 * dx - dy;
        d1 = 2 * dx;
        d2 = 2 * (dx - dy);
        x = x1;
        y = y1;
        glBegin(GL_POINTS);
        glVertex2i(x, y);
        glEnd();
        while (y < y2)
        {
            y++;
            if (d < 0)
                d += d1;
            else
            {
                x += inc;
                d += d2;
            }
            glBegin(GL_POINTS);
            glVertex2i(x, y);
            glEnd();
        }
    }
}
void myDisplay(void)
{
    glShadeModel(GL_FLAT);
    glClearColor(0, 1, 1, 0);
    glClear(GL_COLOR_BUFFER_BIT);
    DDA(300, 100, 0, 0); //调用DDA函数画线,黄色的
    MidPointLine(0, 0, 400, 200); //红色的
    Bresenham(0, 0, 100, 300); //绿色的
    glFlush();
}
void Reshape(int w, int h)
{
    glViewport(0, 0, w, h); //设置窗口值
    glMatrixMode(GL_PROJECTION); //这三个函数调整绘图坐标,使左下角位置为(0,0),右上角位置为(w,h)
    glLoadIdentity();
    gluOrtho2D(0.0, w, 0.0, h); //设置坐标系值, 这个要放在Reshape中,否则窗口右上角的坐标一直都是初始的w和h
}
int main(int argc, char * argv[])
{
    glutInit(&argc, argv);
    glutInitDisplayMode(GLUT_RGB | GLUT_SINGLE);
    glutInitWindowPosition(300, 100);
    glutInitWindowSize(450, 450);
    glutCreateWindow("DRAWLINE in different ways");
    glutDisplayFunc(myDisplay);
    glutReshapeFunc(Reshape); //窗口初次创建是被调用
    glutMainLoop();
    return 0;
}

你可能感兴趣的:(画线算法 中点画线 Bresenham DDA)