opengl实现cs、liang-barsky直线裁剪算法

该博客详细介绍了如何使用OpenGL实现计算机图形学中的CS(Crossing Segment)和Liang-Barsky直线裁剪算法,包括代码示例,用于在指定矩形区域内进行直线裁剪。读者将学习到两种裁剪算法的原理及其在OpenGL环境中的应用。
摘要由CSDN通过智能技术生成
#include<glut.h>
#include<iostream>
#define L 00001
#define R 00002
#define B 00004
#define T 00010
#define M 00000

#define LT 00011
#define LB 00005
#define RT 00012
#define RB 00006
//#define L 0x0001
//#define R 0x0002
//#define B 0x0004
//#define T 0x0008
//#define M 0x0000
//
//#define LT 0x0009
//#define LB 0x0005
//#define RT 0x000a
//#define RB 0x0006
using namespace std;
struct rectan {
	int xx0, yy0;
	int xx1, yy1;
}rec;
int temX = 0, tempY = 0;
int c0 = 0, c1 = 0, c3 = 0;
bool f = false;

int CSLine(float xwl, float xwr, float ywb, float ywt, float x, float y) {   //得到编码区
	int temp = 0;
	if (x < xwl) {
		temp |= L;
	}
	else if (x > xwr) {
		temp |= R;
	}
	if (y < ywb) {
		temp |= B;
	}
	else if (y > ywt) {
		temp |= T;
	}
	return temp;
}
void CSLineP(int temp, float xwl, float xwr, float ywb, float ywt, float &x1, float &y1, float x2, float y2, float& x, float& y) {
	float k = (y2 - y1) / (x2 - x1);
	if ((temp & L) == 1) {
		y1 = y1 + k * (xwl - x1);
		x1 = xwl;
	}
	if ((temp & R) == 2) {
		y1 = y1 + k * (xwr - x1);
		x1 = xwr;
	}
	if ((temp & B) == 4 && y1 < ywb) {
		x1 = x1 + (ywb - y1) / k;
		y1 = ywb;
	}
	if ((temp & T) == 8 && y1 > ywt) {
		x1 = x1 + (ywt - y1) / k;
		y1 = ywt;
	
// 线裁剪CSView.cpp : implementation of the CCSView class // #include "stdafx.h" #include "线裁剪CS.h" #include "线裁剪CSDoc.h" #include "线裁剪CSView.h" #define left 200 //默认图形窗口 #define right 400 #define bottom 100 #define top 300 #define l 1 //区域编码 #define r 2 #define b 4 #define t 8 #ifdef _DEBUG #define new DEBUG_NEW #undef THIS_FILE static char THIS_FILE[] = __FILE__; #endif ///////////////////////////////////////////////////////////////////////////// // CCSView IMPLEMENT_DYNCREATE(CCSView, CView) BEGIN_MESSAGE_MAP(CCSView, CView) //{{AFX_MSG_MAP(CCSView) ON_WM_LBUTTONDOWN() ON_WM_RBUTTONDOWN() //}}AFX_MSG_MAP // Standard printing commands ON_COMMAND(ID_FILE_PRINT, CView::OnFilePrint) ON_COMMAND(ID_FILE_PRINT_DIRECT, CView::OnFilePrint) ON_COMMAND(ID_FILE_PRINT_PREVIEW, CView::OnFilePrintPreview) END_MESSAGE_MAP() ///////////////////////////////////////////////////////////////////////////// // CCSView construction/destruction CCSView::CCSView() { // TODO: add construction code here } CCSView::~CCSView() { } BOOL CCSView::PreCreateWindow(CREATESTRUCT& cs) { // TODO: Modify the Window class or styles here by modifying // the CREATESTRUCT cs return CView::PreCreateWindow(cs); } ///////////////////////////////////////////////////////////////////////////// // CCSView drawing void CCSView::OnDraw(CDC* pDC) { CCSDoc* pDoc = GetDocument(); ASSERT_VALID(pDoc); pDC->TextOut(150,1," 单击左键定线起点,右击定线终点"); pDC->TextOut(150,20,"线裁剪结果会自动显示"); // TODO: add draw code for native data here pDC->Rectangle(left,top,right,bottom); } ///////////////////////////////////////////////////////////////////////////// // CCSView printing BOOL CCSView::OnPreparePrinting(CPrintInfo* pInfo) { // default preparation return DoPreparePrinting(pInfo); } void CCSView::OnBeginPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/) { // TODO: add extra initialization before printing } void CCSView::OnEndPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/) { // TODO: add
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值