头歌直线裁剪实验

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

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

#define LEFT   1
#define RIGHT  2
#define BOTTOM 4
#define TOP    8

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 CompCode(Point node, MyRect rect)
{
	// 请在此添加你的代码
   /********** Begin ********/

int code = 0;
    if (node.x < rect.xmin)
        code |= LEFT;
    else if (node.x > rect.xmax)
        code |= RIGHT;

    if (node.y < rect.ymin)
        code |= BOTTOM;
    else if (node.y > rect.ymax)
        code |= TOP;
    
    return code;


   /********** End **********/
}

void LineClipCohenSurtherland(MyRect rect, Point& node1, Point& node2)
{
	bool accept = false;
	// 请在此添加你的代码
   /********** Begin ********/
    int code1 = CompCode(node1, rect);
    int code2 = CompCode(node2, rect);

    while (true)
    {
        if ((code1 | code2) == 0)
        {
            accept = true;
            break;
        }
        else if (code1 & code2)
        {
            break;
        }
        else
        {
            int code = (code1 != 0) ? code1 : code2;
            Point node;
            if (code & LEFT)
            {
                node.x = rect.xmin;
                node.y = node1.y + (node2.y - node1.y) * (rect.xmin - node1.x) / (node2.x - node1.x);
            }
            else if (code & RIGHT)
            {
                node.x = rect.xmax;
                node.y = node1.y + (node2.y - node1.y) * (rect.xmax - node1.x) / (node2.x - node1.x);
            }
            else if (code & BOTTOM)
            {
                node.y = rect.ymin;
                node.x = node1.x + (node2.x - node1.x) * (rect.ymin - node1.y) / (node2.y - node1.y);
            }
            else if (code & TOP)
            {
                node.y = rect.ymax;
                node.x = node1.x + (node2.x - node1.x) * (rect.ymax - node1.y) / (node2.y - node1.y);
            }

            if (code == code1)
            {
                node1 = node;
                code1 = CompCode(node1, rect);
            }
            else
            {
                node2 = node;
                code2 = CompCode(node2, rect);
            }
        }
    }



   /********** End **********/
	if (accept)
        LineGL(node1, node2);
}

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) 
		LineClipCohenSurtherland(rect, 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_step1/test.jpg", img);
	return 0;
}

 

 

 

  • 6
    点赞
  • 12
    收藏
    觉得还不错? 一键收藏
  • 3
    评论
以下是利用C++对直线进行裁剪的基本步骤: 1. 定义直线的两个端点。可以使用结构体或类来存储端点的坐标。 2. 定义裁剪窗口的四个边界。同样可以使用结构体或类来存储。 3. 实现裁剪算法。最常见的算法是Cohen-Sutherland算法和Liang-Barsky算法。这两种算法都需要将直线裁剪窗口的边界进行比较,判断直线是否与窗口相交,并计算交点。 4. 根据算法计算得到裁剪后的直线端点坐标,并将其绘制在屏幕上。 下面是一个简单的示例代码,使用Cohen-Sutherland算法直线进行裁剪: ```cpp #include <iostream> #include <graphics.h> // 定义直线端点结构体 struct Point { int x; int y; }; // 定义裁剪窗口边界结构体 struct Boundary { int xmin; int ymin; int xmax; int ymax; }; // 定义直线裁剪函数 void clipLine(Point p1, Point p2, Boundary b) { // 计算直线斜率 float m = (float)(p2.y - p1.y) / (p2.x - p1.x); // 初始化区域码 int code1 = 0, code2 = 0; // 判断端点位置 if (p1.x < b.xmin) code1 |= 1; if (p1.x > b.xmax) code1 |= 2; if (p1.y < b.ymin) code1 |= 4; if (p1.y > b.ymax) code1 |= 8; if (p2.x < b.xmin) code2 |= 1; if (p2.x > b.xmax) code2 |= 2; if (p2.y < b.ymin) code2 |= 4; if (p2.y > b.ymax) code2 |= 8; // 循环直到直线完全在裁剪窗口内部或被裁剪 while (code1 != 0 || code2 != 0) { // 直线完全在裁剪窗口外部 if (code1 & code2) { return; } // 计算交点 float x, y; int code = code1 ? code1 : code2; if (code & 1) { y = p1.y + m * (b.xmin - p1.x); x = b.xmin; } else if (code & 2) { y = p1.y + m * (b.xmax - p1.x); x = b.xmax; } else if (code & 4) { x = p1.x + (b.ymin - p1.y) / m; y = b.ymin; } else { x = p1.x + (b.ymax - p1.y) / m; y = b.ymax; } // 更新端点位置和区域码 if (code == code1) { p1.x = x; p1.y = y; code1 = 0; if (p1.x < b.xmin) code1 |= 1; if (p1.x > b.xmax) code1 |= 2; if (p1.y < b.ymin) code1 |= 4; if (p1.y > b.ymax) code1 |= 8; } else { p2.x = x; p2.y = y; code2 = 0; if (p2.x < b.xmin) code2 |= 1; if (p2.x > b.xmax) code2 |= 2; if (p2.y < b.ymin) code2 |= 4; if (p2.y > b.ymax) code2 |= 8; } } // 绘制裁剪后的直线 line(p1.x, p1.y, p2.x, p2.y); } int main() { // 初始化图形界面 initwindow(640, 480); // 定义直线端点和裁剪窗口边界 Point p1 = {100, 100}; Point p2 = {500, 400}; Boundary b = {200, 100, 400, 300}; // 绘制原始直线裁剪窗口 setcolor(YELLOW); line(p1.x, p1.y, p2.x, p2.y); rectangle(b.xmin, b.ymin, b.xmax, b.ymax); // 裁剪直线 setcolor(RED); clipLine(p1, p2, b); // 等待用户关闭窗口 getch(); closegraph(); return 0; } ``` 在上述示例代码中,我们使用了BGI图形库的line函数来绘制直线和矩形。如果你想使用其他图形库或纯文字界面,需要将绘图函数替换为相应的输出函数。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值