Liang-Barskey裁剪算法(计算机图形学)

   日期:2020-05-09     浏览:176    评论:0    
核心提示:在Opengl应用框架下实现Liang-Barskey算法。完成一个四边形对两条线段的裁剪:四边形的左上角和右下角顶点分别为(100,100),(300,200),线段2的两个端点为(180,50),(180,380)。#include#includeusing namespace std;int LBLineCl...

在Opengl应用框架下实现Liang-Barskey算法。完成一个四边形对两条线段的裁剪:四边形的左上角和右下角顶点分别为(100,100),(300,200),线段2的两个端点为(180,50),(180,380)。



#include<iostream>
#include<GL/glut.h>

using namespace std;
int LBLineClipTest(float p, float q, float& umax, float& umin) {
	float r = 0.0;
	if (p < 0.0) {
		r = q / p;
		if (r > umin) {
			return 0;
		}
		else if (r > umax) {
			umax = r;
		}
	}
	else if (p > 0.0) {
		r = q / p;
		if (r < umax) {
			return 0;
		}
		else if (r < umin) {
			umin = r;
		}
	}
	else if (q < 0.0) {
		return 0;
	}
	return 1;
}

void LBLineClip(float xwl, float xwr, float ywb, float ywt, float x1, float y1, float x2, float y2) {
	float umax, umin, deltax, deltay, xx2, yy2, xx1, yy1;
	deltax = x2 - x1;
	deltay = y2 - y1;
	umax = 0.0;
	umin = 1.0;
	if (LBLineClipTest(-deltax, x1 - xwl, umax, umin)) {
		if (LBLineClipTest(deltax, xwr - x1, umax, umin)) {
			if (LBLineClipTest(-deltay, y1 - ywb, umax, umin)) {
				if (LBLineClipTest(deltay, ywt - y1, umax, umin)) {
					xx1 = int(x1 + umax * deltax + 0.5);
					yy1 = int(y1 + umax * deltay + 0.5);
					xx2 = int(x1 + umin * deltax + 0.5);
					yy2 = int(y1 + umin * deltay + 0.5);
				}
				glBegin(GL_LINES);
				glVertex2f(xx1, yy1);
				glVertex2f(xx2, yy2);
				glEnd();
			}
		}
	}
}

void RenderSence() {
	glClear(GL_COLOR_BUFFER_BIT);
	glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);
	glLineWidth(1);
	glColor3f(0.0f, 0.0f, 0.0f);
	glBegin(GL_LINES);
	glVertex2i(180, 50);
	glVertex2i(180, 380);
	glEnd();
	glFlush();
	glLineWidth(4);
	glRectf(100, 100, 300, 200);
	LBLineClip(100, 300, 100, 200, 180, 50, 180, 380);
	glFlush();
}

void ChangeSize(GLsizei w, GLsizei h) {
	if (h == 0) {
		h = 1;
	}
	glViewport(0, 0, w, h);
	glMatrixMode(GL_PROJECTION);
	glLoadIdentity();
	if (w <= h) {
		glOrtho(-100.0f, 250.0f, -100.0f, 250.0f * h / w, 1.0f, -1.0f);
	}
	else {
		glOrtho(-100.0f, 250.0f * w / h, -100.0f, 250.0f, 1.0f, -1.0f);
	}
	glMatrixMode(GL_MODELVIEW);
	glLoadIdentity();

}

void SetupRC() {
	glClearColor(1.0f, 1.0f, 1.0f, 1.0f);
}

void main(int argc, char** argv) {
	glutInit(&argc, argv);
	glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);
	glutInitWindowSize(1000, 500);
	glutCreateWindow("GLLine");
	glutDisplayFunc(RenderSence);
	glutReshapeFunc(ChangeSize);
	SetupRC();
	glutMainLoop();
}







 
打赏
 本文转载自:网络 
所有权利归属于原作者,如文章来源标示错误或侵犯了您的权利请联系微信13520258486
更多>最近资讯中心
更多>最新资讯中心
0相关评论

推荐图文
推荐资讯中心
点击排行
最新信息
新手指南
采购商服务
供应商服务
交易安全
关注我们
手机网站:
新浪微博:
微信关注:

13520258486

周一至周五 9:00-18:00
(其他时间联系在线客服)

24小时在线客服