【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
    点赞
  • 30
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值