【计算机图形学】实验报告5线段裁剪算法

1、编写程序实现Cohen-Sutherland线段裁剪算法。2、编程实现Liang-Barsky算法。1、实现Cohen-Sutherland线段裁剪算法。#include<gl/glut.h>#include<stdio.h>#include<stdlib.h>#define LEFT_EDGE 1#define RIGHT_EDGE 2#define BOTTOM_EDGE 4#define TOP_EDGE 8void LineGL(int.
摘要由CSDN通过智能技术生成

1、编写程序实现Cohen-Sutherland线段裁剪算法。
2、编程实现Liang-Barsky算法。

1、实现Cohen-Sutherland线段裁剪算法。

#include<gl/glut.h>
#include<stdio.h>
#include<stdlib.h>
#define LEFT_EDGE 1
#define RIGHT_EDGE 2
#define BOTTOM_EDGE 4
#define TOP_EDGE 8
void LineGL(int x0, int y0, int x1, int y1) {
   
	glBegin(GL_LINES);
	glColor3f(1.0f, 0.0f, 0.0f); glVertex2f(x0, y0);
	glColor3f(0.0f, 1.0f, 0.0f); glVertex2f(x1, y1);
	glEnd();
}
void Liney(int x3, int y3, int x4, int y4) {
   
	glBegin(GL_LINES);
	glColor3f(0.0f, 0.0f, 0.0f); glVertex2f(x3, y3);
	glColor3f(0.0f, 0.0f, 0.0f); glVertex2f(x4, y4);
	glEnd();
}
void Linex(int x5, int y5, int x6, int y6) {
   
	glBegin(GL_LINES);
	glColor3f(0.0f, 0.0f, 0.0f); glVertex2f(x5, y5);
	glColor3f(0.0f, 0.0f, 0.0f); glVertex2f(x6, y6);
	glEnd();
}
struct Rectangle {
   
	float xmin, xmax, ymin, ymax;
};
Rectangle rect;
int x0, y0, x1, y1, x3, y3, x4, y4, x5, y5, x6, y6;
int CompCode(int x, int y, Rectangle rect) {
   
	int code = 0x00;
	if (y < rect.ymin)
		code = code | 4;
	if (y > rect.ymax)
		code = code | 8;
	if (x > rect.xmax)
		code = code | 2;
	if (x < rect.xmin)
		code = code | 1;
	return code;
}
int cohensutherlandlineclip(Rectangle rect, int &x0, int &y0, int &x1, int &y1)
{
   
	int accept, done;
	float x, y;
	accept = 0;
	done = 0;
	int code0, code1, codeout;
	code0 = CompCode(x0, y0, rect);
	code1 = CompCode(x1, y1, rect);
	do {
   
		if (!(code0 | code1)) {
   //整条线段在窗口内
			accept = 1;//取之
			done = 1;
		}
		else if (code0 & code1)//两个端点同在窗口一侧,弃之
			done = 1;
		else {
   //线段与窗口存在交点
			if (code0 != 0)
				codeout = code0;
			else
				codeout = code1;
			//求交点
			if 
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值