多边形的扫描转换

实验环境

Visual Studio 2019

三、实验步骤/过程

#include   

#include

#include 

using namespace std;

int n;

struct vertex {

    float ver_x;

    float ver_y;

};

typedef struct XET {

    float x;

    float dx, ymax;

    XET* next;

}AET, NET;

struct point {

    float x;

    float y;

};

vertex* ver;

inline void input() {

    cout << "输入要显示的多边形共有几个顶点" << endl;

    cin >> n;

    ver = (vertex*)malloc(sizeof(vertex) * n);//输入顶点以(x,y)格式

    for (int i = 0; i < n; i++) {

        cout << "按顺序输入每个顶点的想x,y值,每输入完一个顶点,点击回车进入下一个顶点的输入" << endl;

        cin >> ver[i].ver_x >> ver[i].ver_y;

    }

}

void Polygon() {

    int MaxY = 0;

    int i;

    for (i = 0; i < n; i++) {

        if (ver[i].ver_y > MaxY)

            MaxY = ver[i].ver_y;

    }

    AET* pAET = new AET;

    pAET->next = NULL;

    NET* pNET[1024];

    for (i = 0; i <= MaxY; i++) {

        pNET[i] = new NET;

        pNET[i]->next = NULL;

    }

    glClear(GL_COLOR_BUFFER_BIT);

    glColor3f(0.0, 0.0, 0.0);

    glBegin(GL_POINTS);

    for (i = 0; i < MaxY; i++) {

        for (int j = 0; j < n; j++) {

            if (ver[j].ver_y == i) {

                if (ver[(j + 1 + n) % n].ver_y > ver[j].ver_y) {

                    NET* p = new NET;

                    p->x = ver[j].ver_x;

                    p->ymax = ver[(j + 1 + n) % n].ver_y;

                    p->dx = (ver[(j + 1 + n) % n].ver_x - ver[j].ver_x) / (ver[(j + 1 + n) % n].ver_y - ver[j].ver_y);

                    p->next = pNET[i]->next;

                    pNET[i]->next = p;

                }

                if (ver[(j - 1 + n) % n].ver_y > ver[j].ver_y) {

                    NET* p = new NET;

                    p->x = ver[j].ver_x;

                    p->ymax = ver[(j - 1 + n) % n].ver_y;

                    p->dx = (ver[(j - 1 + n) % n].ver_x - ver[j].ver_x) / (ver[(j - 1 + n) % n].ver_y - ver[j].ver_y);

                    p->next = pNET[i]->next;

                    pNET[i]->next = p;

                }

            }

        }

    }

    glClear(GL_COLOR_BUFFER_BIT);

    if (n % 2 == 0) {

        glColor3f(1.0, 0.0, 0.0);     // 设置当前色为红色

    }

    else

    {

        glColor3f(0.0, 0.0, 1.0);     // 设置当前色为蓝色

    }

    glBegin(GL_POINTS);

    for (i = 0; i <= MaxY; i++) {

        AET* p = new AET;

        p = pAET->next;

        AET* n = new AET;

        //将新边表中的活性边按照从左到右的顺序排序

        if (pNET[i]->next && pNET[i]->next->next) {

            if (pNET[i]->next->dx > 0) {

                NET* t = new NET;

                t = pNET[i]->next;

                n = pNET[i]->next->next;

                t->next = NULL;

                n->next = NULL;

                pNET[i]->next = n;

                n->next = t;

            }

        }

        //更新活性边表中的活性边x坐标的值

        while (p) {

            p->x = p->x + p->dx;

            p = p->next;

        }

        p = pAET->next;

        n = pAET;

        //删掉扫描线高度等同于ymax的废弃点

        while (p) {

            if (p->ymax == i) {

                n->next = p->next;

                p = n->next;

            }

            else {

                p = p->next;

                n = n->next;

            }

        }

        //插入新点,按照顺序插入

        p = pAET->next;

        n = pAET;

        NET* a = new NET;

        a = pNET[i]->next;

        if (a) {

            NET* b = new NET;

            b = a;

            while (b->next) {

                b = b->next;

            }

            if (!pAET->next) {

                pAET->next = a;

            }

            else {

                while (p) {

                    if (a->x < p->x) {

                        b->next = p;

                        n->next = a;

                        break;

                    }

                    if (!p->next) {

                        p->next = a;

                        break;

                    }

                    n = n->next;

                    p = p->next;

                }

            }

        }

        //填充2

        p = pAET->next;

        while (p && p->next) {

            for (float j = p->x; j <= p->next->x; j++) {

                glVertex2i(static_cast<int>(j), i);

            }

            p = p->next->next;

        }

    }

    glEnd();

    glFlush();

}

void init(void) {

    glClearColor(1.0, 1.0, 1.0, 0.0);   //设置底色 红绿蓝和alpha

    glMatrixMode(GL_PROJECTION);      //这个是投影的意思

    gluOrtho2D(0.0, 600, 0.0, 450); //gluOrtho2D是指屏幕区域对应的模型坐标范围

}

int main(int argc, char* argv) {

    glutInit(&argc, &argv);

    glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);

    glutInitWindowPosition(50, 100);

    glutInitWindowSize(400, 300);

    glutCreateWindow("图形学实验一");

    input();

    init();

    glutDisplayFunc(Polygon);

    glutMainLoop();

}

实验结果

多边形的扫描转换_第1张图片

 

你可能感兴趣的:(c++,开发语言,c语言)