视频加载失败

课程

1159 字
约 4 分钟

4.2 Liang-Barsky 参数化裁剪算法

计算机图形学labs/lab04-line-clipping·更新于 2026-09-15

4.2 Liang-Barsky 参数化裁剪算法

1. 任务描述

根据下面要求,在右侧修改代码空间,绘制出预期输出的图片。平台会对你编写的代码进行测试。

本关任务

  1. 理解直线裁剪的原理(Cohen-Sutherland 算法、中点分割算法、梁友栋算法)。
  2. 利用 VC + OpenGL 实现直线的编码裁剪算法,在屏幕上用一个封闭矩形裁剪任意一条直线。
  3. 调试、编译、修改程序。

运行前

运行前

输出

输出

具体要求

  1. ClipTest(float p, float q, float* u1, float* u2) 函数进行补全。
  2. LineClipLiangBarsky(MyRect rect, Point& node1, Point& node2) 函数进行补全,最终实现裁剪后的图片。

相关知识

为了完成本关任务,你需要掌握:Liang-Barsky 参数化裁剪算法。

1. Liang-Barsky 算法原理

图A Liang-Barsky 参数化裁剪算法

  1. 输入直线段的两端点坐标:P0=(x0, y0)P1=(x1, y1),以及窗口的四条边界:L, R, T, B
  2. dx = 0,则 x0 = x1。此时进一步判断是否满足 x0 < xminx0 > xmax,若满足,则该直线段完全在窗口外,不可见,算法转第 7 步。否则,计算 u1u2 的交点范围,算法转第 5 步。
  3. dy = 0,则 y0 = y1。此时进一步判断是否满足 y0 < yminy0 > ymax,若满足,则该直线段完全在窗口外,不可见,算法转第 7 步。否则,计算 u1u2 的交点范围,算法转第 5 步。
  4. 若上述两条均不满足,此时计算 u1u2
  5. 求得 u1u2 后,进行判断:若 u1 > u2,则直线段在窗口外,不可见,算法转第 7 步。若 u1 <= u2,利用直线的参数方程求得直线段在窗口内的有效交点两端点坐标。
  6. 利用直线的扫描转换算法绘制在窗口内的直线段。
  7. 算法结束。

开始你的任务吧,祝你成功!

2. 我的回答

文件 step2/test2.cpp:

// 评测代码所用头文件-开始
#include <opencv2/core/core.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <opencv2/imgproc/imgproc.hpp>
// 评测代码所用头文件-结束

// 提示:写完代码请保存之后再进行评测
#include <GL/freeglut.h>
#include <stdio.h>

struct MyRect
{
	int xmin, xmax, ymin, ymax;
    MyRect() : xmin(), xmax(), ymin(), ymax(){};
    MyRect(int a, int b, int c, int d) : xmin(a), xmax(b), ymin(c), ymax(d){};
};

struct Point
{
	int x, y;
	Point() : x(), y() {};
	Point(int a, int b) :x(a), y(b) {};
};

MyRect  rect;
Point vPoint[6];

void LineGL(Point node1, Point node2)
{
	glBegin(GL_LINES);
	glColor3f(0.0f, 1.0f, 0.0f);
    glVertex2f(node1.x, node1.y);
	glVertex2f(node2.x, node2.y);
	glEnd();
}

int ClipTest(float p, float q, float* u1, float* u2)
{
    // 请在此添加你的代码
    /**** Begin **/
    float r;
    if (p < 0.0) {
        r = q / p;
        if (r > *u2)
            return 0; // 完全在边界外
        else if (r > *u1)
            *u1 = r;  // 更新靠近起点的交点参数
    }
    else if (p > 0.0) {
        r = q / p;
        if (r < *u1)
            return 0; // 完全在边界外
        else if (r < *u2)
            *u2 = r;  // 更新靠近终点的交点参数
    }
    else { // p == 0,直线与边界平行
        if (q < 0.0)
            return 0; // 平行且在窗口外
    }
    return 1;
    /**** End **/
}

bool LineClipLiangBarsky(MyRect rect, Point& node1, Point& node2)
{
    // 请在此添加你的代码
    /**** Begin **/
    float u1 = 0.0, u2 = 1.0;
    float dx = node2.x - node1.x;
    float dy = node2.y - node1.y;

    // 依次对左、右、下、上四个边界进行参数化裁剪测试
    if (ClipTest(-dx, node1.x - rect.xmin, &u1, &u2) &&
        ClipTest(dx, rect.xmax - node1.x, &u1, &u2) &&
        ClipTest(-dy, node1.y - rect.ymin, &u1, &u2) &&
        ClipTest(dy, rect.ymax - node1.y, &u1, &u2))
    {
        // 必须暂存初始起点,因为修改 node1 后,计算 node2 会被干扰
        Point temp = node1;

        if (u2 < 1.0) {
            node2.x = (int)(temp.x + u2 * dx);
            node2.y = (int)(temp.y + u2 * dy);
        }
        if (u1 > 0.0) {
            node1.x = (int)(temp.x + u1 * dx);
            node1.y = (int)(temp.y + u1 * dy);
        }
        return true;
    }

    return false;
    /**** End **/
}

void MyDisplay()
{
	glClear(GL_COLOR_BUFFER_BIT);
	glColor3f(1.0f, 1.0f, 1.0f);
	glRectf(rect.xmin, rect.ymin, rect.xmax, rect.ymax);

	for(int i = 0; i < 5; i+=2){
        bool bAccept = LineClipLiangBarsky(rect, vPoint[i], vPoint[i+1]);
		if(bAccept)
            LineGL(vPoint[i], vPoint[i+1]);
	}

	glFlush();
}

void Init()
{
	glClearColor(0.0, 0.0, 0.0, 0.0);
	glShadeModel(GL_FLAT);

	rect = MyRect(100, 300, 100, 300);

	vPoint[0] = Point(200, 50); vPoint[1] = Point(350, 250);
	vPoint[2] = Point(125, 205); vPoint[3] = Point(250, 255);
	vPoint[4] = Point(40, 150); vPoint[5] = Point(150, 40);
}

void MyReshape(int w, int h)
{
	glViewport(0, 0, (GLsizei)w, (GLsizei)h);
	glMatrixMode(GL_PROJECTION);
	glLoadIdentity();
	gluOrtho2D(0.0, (GLdouble)w, 0.0, (GLdouble)h);
}

int main(int argc, char* argv[])
{
	glutInit(&argc, argv);
	glutInitDisplayMode(GLUT_RGB | GLUT_SINGLE);
	glutInitWindowPosition(100, 100);
	glutInitWindowSize(400, 400);
	glutCreateWindow("Hello World!");

	Init();
	glutDisplayFunc(MyDisplay);
    glutReshapeFunc(MyReshape);
    glutMainLoopEvent();

    /*************以下为评测代码,与本次实验内容无关,请勿修改**************/
	GLubyte* pPixelData = (GLubyte*)malloc(400 * 400 * 3);//分配内存
    GLint viewport[4] = {0};
    glReadBuffer(GL_FRONT);
    glPixelStorei(GL_UNPACK_ALIGNMENT, 4);
    glGetIntegerv(GL_VIEWPORT, viewport);
    glReadPixels(viewport[0], viewport[1], viewport[2], viewport[3], GL_RGB, GL_UNSIGNED_BYTE, pPixelData);

	cv::Mat img;
    std::vector<cv::Mat> imgPlanes;
    img.create(400, 400, CV_8UC3);
    cv::split(img, imgPlanes);

    for(int i = 0; i < 400; i ++) {
        unsigned char* plane0Ptr = imgPlanes[0].ptr<unsigned char>(i);
        unsigned char* plane1Ptr = imgPlanes[1].ptr<unsigned char>(i);
        unsigned char* plane2Ptr = imgPlanes[2].ptr<unsigned char>(i);
        for(int j = 0; j < 400; j ++) {
            int k = 3 * (i * 400 + j);
            plane2Ptr[j] = pPixelData[k];
            plane1Ptr[j] = pPixelData[k+1];
            plane0Ptr[j] = pPixelData[k+2];
        }
    }
    cv::merge(imgPlanes, img);
    cv::flip(img, img ,0);
    cv::namedWindow("openglGrab");
    cv::imshow("openglGrab", img);
    cv::imwrite("../img_step2/test.jpg", img);
	return 0;
}
Profile Image of the Author
Sonder
好想要技术
这是公告标题
这只是一个公告
分类
标签
站点信息
构建平台
GitHub Actions
博客版本
Firefly v6.16.7
文章许可
CC BY-NC-SA 4.0
文章目录