【OpenGL】计算机图形学实验三: 区域填充算法实验(区域填充算法)

实验三: 区域填充算法实验

(区域填充算法)

1、实验目的和要求

熟悉并掌握多边形区域用顺序扫描和种子填充算法原理,并利用数据结构和程序设计知识加以实现。

2、实验设备

PC机、CodeBlocks\VS系列\OpenGL安装包

3、实验内容及原理

根据自己的兴趣选择1-2个作为本次实验的研究内容

  1. 实现多边形扫描转换算法;
  2. 实现种子填充算法(简单种子填充算法和基于扫描线的种子填充算法);
  3. 实现边标志填充算法;
  4. GDI函数实现多边形填充;
  5. 利用OpenGL编程绘制多边形并填充之;进行填充与比较。

实验原理(基本知识)

边标志填充算法:先用一种特殊的颜色在帧缓冲存储器中将多边形的边界(水平边部分的边界除外)勾画出来,然后将着色的像素点依x坐标递增的顺序两两配对,再将每对像素构成的扫描线区间内所有像素置为填充色。

4、实验源程序代码、运行结果

4.1边标志算法

4.1.1源程序代码

#ifdef __APPLE__
#include <GLUT/glut.h>
#else
#include <GL/glut.h>
#endif
#include <cmath>
#include <vector>
#include <iostream>
#include <set>

using namespace std;

set< pair<int,int> > edgeMarks;

void resize(GLsizei w, GLsizei h) {
    if (h == 0) {
        h = 1;
    }

    glViewport(0, 0, w, h);
    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();

    if (w <= h) {
        glOrtho(0.0f, 200.0f, 0.0f, 200.f * h / w, 1.0f, -1.0f);
    } else {
        glOrtho(0.0f, 200.0f * w / h, 0.0f, 200.0f, 1.0f, -1.0f);
    }

    glMatrixMode(GL_MODELVIEW);
    glLoadIdentity();
}

void drawPixel(int x, int y) {
    glBegin(GL_POINTS);
        glVertex2i(x, y);
    glEnd();
}

// check 判断(x1, y1)是否是三点之中的极值点
bool check(int x0, int y0, int x1, int y1, int x2, int y2) {
    // x1 是否在中间?
    if (x1 >= x0 && x1 >= x2) {
        return false;
    }
    if (x1 <= x0 && x1 <= x2) {
        return false;
    }

    if (y0 > y1 && y2 > y1) {
        return true;
    }

    if (y0 < y1 && y2 < y1) {
        return true;
    }

    return false;
}

// BresenhamLine
void BresenhamLine(vector<int> p0, vector<int> p1) {
    int x0 = p0[0], y0 = p0[1],
        x1 = p1[0], y1 = p1[1];

    // 两种特殊情况: x0 == x1 || y0 == y1
    if (x0 == x1) {
        if (y0 > y1) {
            swap(y0, y1);
        }
        for (int i = y0; i <= y1; i++) {
            drawPixel(x0, i);
        }
        return ;
    } else if (y0 == y1) {
        drawPixel(x0, y0);
        drawPixel(x1, y1);
        edgeMarks.insert(make_pair(x0, y0));
        edgeMarks.insert(make_pair(x1, y1));
        return ;
    }

    int kRev = (y1 - y0) / (x1 - x0);

    bool isKGT1 = false; // 1 : K is greater than 1
    bool isKNeg = (y1 - y0) * (x1 - x0) >= 0 ? false : true; // 1 : K is negative
    if (abs(kRev) >= 1) { // |k| > 1
        swap(x0, y0);
        swap(x1, y1);
        isKGT1 = true;
    }

    if (isKGT1) {
        if (isKNeg) {
            if (y0 < y1) {
                swap(y0, y1);
                swap(x0, x1);
            }
        } else {
            if (y0 > y1) {
                swap(y0, y1);
                swap(x0, x1);
            }
        }

    } else {
        if (x0 > x1) {
            swap(y0, y1);
            swap(x0, x1);
        }
    }

    int x = x0, y = y0;
    int dx = x1 - x0, dy = y1 - y0;
    if (isKNeg) {
        dy = -dy;
    }

    int e = -dx;

    while (x <= x1) {
        if (isKGT1) {
            drawPixel(y, x);
            edgeMarks.insert(make_pair(y, x));
        } else {
            drawPixel(x, y);
            edgeMarks.insert(make_pair(x, y));
        }
        x++;
        e += 2 * dy;

        if (e > 0) {
            if (isKNeg) {
                y--;
            } else {
                y++;
            }
            e -= 2 * dx;
        }
    }


}

void edgeMarkFilling(vector< vector<int> > points) {
    // get y_min & y_max
    int y_min = points[0][1], y_max = points[0][1];
    int x_min = points[0][0], x_max = points[0][0];

    // draw lines
    for (unsigned int i = 1; i < points.size(); i++) {
        BresenhamLine(points[i - 1], points[i]);
        y_min = min(points[i][1], y_min);
        y_max = max(points[i][1], y_max);
        x_min = min(points[i][0], x_min);
        x_max = max(points[i][0], x_max);

        // 局部最低点 —— 0 个重叠的交点,局部最高点 —— 0个
        if (i < points.size() - 1) {
            if (check(points[i - 1][0], points[i - 1][1], points[i][0], points[i][1], points[i + 1][0], points[i + 1][1])) {
                edgeMarks.erase(make_pair(points[i][0], points[i][1]));
            }
        }

    }
    BresenhamLine(points[0], points[points.size() - 1]);
    int lastpoint = points.size() - 1;
    if (check(points[lastpoint - 1][0], points[lastpoint - 1][1], points[lastpoint][0], points[lastpoint][1], points[0][0], points[0][1])) {
        edgeMarks.erase(make_pair(points[lastpoint][0], points[lastpoint][1]));
    }
    if (check(points[lastpoint][0], points[lastpoint][1], points[0][0], points[0][1], points[1][0], points[1][1])) {
        edgeMarks.erase(make_pair(points[0][0], points[0][1]));
    }

    // filling
    glColor3f(0.0, 0.0, 1.0);
    glBegin(GL_POINTS);
    for (int i = y_max; i >= y_min; i--) {
        bool isInside = false;
        for(int j = x_min; j < x_max; j++) {
            if (edgeMarks.count(make_pair(j, i)) == 1) {
                isInside = !isInside;
            }
            if (isInside) {
                glVertex2i(j, i);
                // cout << j << ' ' << i << endl;
            }
        }
    }
    glEnd();
}

void display() {
    glClear(GL_COLOR_BUFFER_BIT);
    glColor3d(1, 0, 0);

    vector< vector<int> > points(3);
    vector<int> p0(2), p1(2), p2(2), p3(2), p4(2), p5(2), p6(2);
    p0[0] = 40, p0[1] = 40;
    p1[0] = 10, p1[1] = 10;
    p2[0] = 70, p2[1] = 10;

    points[0] = p0;
    points[1] = p1;
    points[2] = p2;

    edgeMarkFilling(points);

    glutSwapBuffers();
}

int main(int argc, char *argv[]) {
    glutInit(&argc, argv);
    glutInitWindowSize(400, 400);
    glutInitWindowPosition(10, 10);
    glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE);

    glutCreateWindow("EdgeMarkFilling");

    glutReshapeFunc(resize);
    glutDisplayFunc(display);

    // setRC
    glClearColor(1, 1, 1, 1);

    glutMainLoop();

    return 0;
}

4.1.2运行结果

 注:以上的边标志算法没有对抽丝等情况进行处理,比较原始

  • 1
    点赞
  • 28
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
以下是一个基于 OpenGL 的种子填充算法实现代码: ```cpp #include <iostream> #include <GL/glut.h> const int WIDTH = 600; const int HEIGHT = 400; void init() { glClearColor(0.0, 0.0, 0.0, 0.0); glMatrixMode(GL_PROJECTION); glLoadIdentity(); gluOrtho2D(0.0, WIDTH, 0.0, HEIGHT); glMatrixMode(GL_MODELVIEW); } void display() { glClear(GL_COLOR_BUFFER_BIT); glColor3f(1.0, 1.0, 1.0); // 选择一个种子点 int seedX = 300; int seedY = 200; // 开始填充 glPointSize(1.0); glBegin(GL_POINTS); glColor3f(1.0, 0.0, 0.0); glVertex2i(seedX, seedY); glEnd(); glFlush(); // 记录已经填充的点 bool filled[WIDTH][HEIGHT] = { false }; // 添加种子点到队列 int queue[WIDTH * HEIGHT][2]; int queueStart = 0; int queueEnd = 0; queue[queueEnd][0] = seedX; queue[queueEnd][1] = seedY; queueEnd++; // 开始种子填充算法 while (queueStart != queueEnd) { // 取出队列头部的点 int x = queue[queueStart][0]; int y = queue[queueStart][1]; queueStart++; // 向上填充 int upX = x; int upY = y + 1; if (upY < HEIGHT && !filled[upX][upY]) { filled[upX][upY] = true; glBegin(GL_POINTS); glVertex2i(upX, upY); glEnd(); queue[queueEnd][0] = upX; queue[queueEnd][1] = upY; queueEnd++; } // 向下填充 int downX = x; int downY = y - 1; if (downY >= 0 && !filled[downX][downY]) { filled[downX][downY] = true; glBegin(GL_POINTS); glVertex2i(downX, downY); glEnd(); queue[queueEnd][0] = downX; queue[queueEnd][1] = downY; queueEnd++; } // 向左填充 int leftX = x - 1; int leftY = y; if (leftX >= 0 && !filled[leftX][leftY]) { filled[leftX][leftY] = true; glBegin(GL_POINTS); glVertex2i(leftX, leftY); glEnd(); queue[queueEnd][0] = leftX; queue[queueEnd][1] = leftY; queueEnd++; } // 向右填充 int rightX = x + 1; int rightY = y; if (rightX < WIDTH && !filled[rightX][rightY]) { filled[rightX][rightY] = true; glBegin(GL_POINTS); glVertex2i(rightX, rightY); glEnd(); queue[queueEnd][0] = rightX; queue[queueEnd][1] = rightY; queueEnd++; } glFlush(); } } int main(int argc, char** argv) { glutInit(&argc, argv); glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB); glutInitWindowSize(WIDTH, HEIGHT); glutInitWindowPosition(100, 100); glutCreateWindow("Seed Fill Algorithm"); init(); glutDisplayFunc(display); glutMainLoop(); return 0; } ``` 在这个实现中,我们首先选择了一个种子点,然后在 OpenGL 中将其绘制出来。接着,我们使用一个二维布尔数组 `filled` 来记录哪些点已经被填充了。我们使用一个队列来保存待填充的点,初始时只有种子点在队列中。在每次循环中,我们从队列头部取出一个点,向上下左右四个方向检查是否需要填充,如果需要填充,就在 OpenGL 中绘制该点,并将其加入队列。我们不断重复这个过程,直到队列为空,即所有需要填充的点都已经填充完毕。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值