OpenGL实现Cohen-Surtherland算法、梁友栋-Barskey裁剪算法

这篇博客介绍了如何使用OpenGL和VC++实现二维图形的裁剪,特别是Cohen-Sutherland和Barskey算法。实验内容包括理解裁剪原理,实现直线裁剪,并通过Visual C++6.0进行程序调试。提供了代码示例,展示如何裁剪直线并显示在屏幕上。
摘要由CSDN通过智能技术生成

一、实验目的

了解二维图形裁剪的原理(点的裁剪、直线的裁剪、多边形的裁剪),利用VC+OpenGL实现直线的裁剪算法。

二、实验内容

(1)理解直线裁剪的原理(Cohen-Surtherland算法、梁友栋-Barskey算法)

(2)利用VC+OpenGL实现直线的Cohen-Surtherland算法,在屏幕上用一个封闭矩形裁剪任意一条直线。

(3)调试、编译、修改程序。

(4)实现梁友栋-Barskey裁剪算法。

三、实验环境

Visual C++6.0

四、运行结果截图(例图)

 

 

五、代码

//Cohen-Surtherland算法

#include<stdio.h>

#include<stdlib.h>

#include<GL/glut.h>

#define LEFT_EDGE 1

#define RIGHT_EDGE 2

#define BOTTOM_EDGE 4

#define TOP_EDGE 8

int x0,y0,x1,y1;

void LineGL(int x0,int y0,int x1,int y1)

{

 glBegin(GL_LINES);

 glColor3f(1.0,0.0,0.0); glVertex2f(x0,y0);

 glColor3f(0.0,1.0,0.0); glVertex2f(x1,y1);

 glEnd();

}

struct Rectangle

{

 float xmin,xmax,ymin,ymax;

};

Rectangle rect;

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;

 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(codeout&LEFT_EDGE){

    y=y0+(y1-y0)*(rect.xmin-x0)/(x1-x0);

    x=(float)rect.xmin;

   }

   else if(codeout&RIGHT_EDGE){

    y=y0+(y1-y0)*(rect.xmax-x0)/(x1-x0);

    x=(float)rect.xmax;

   }

   else if(codeout&BOTTOM_EDGE){

    x=x0+(x1-x0)*(rect.ymin-y0)/(y1-y0);

    y=(float)rect.ymin;}

   else if(codeout&TOP_EDGE){

     x=x0+(x1-x0)*(rect.ymax-y0)/(y1-y0);

  • 3
    点赞
  • 18
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论
Cohen-Sutherland算法是一种在计算机图形学中广泛使用的直线裁剪算法,它可以快速地确定直线裁剪窗口是否相交,并且可以准确地计算出相交点的位置。下面是使用Python实现Cohen-Sutherland算法的示例代码: ```python # 定义裁剪区域的边界值 LEFT = 1 RIGHT = 2 BOTTOM = 4 TOP = 8 # 计算点的区域码 def computeCode(x, y, xmin, ymin, xmax, ymax): code = 0 if x < xmin: code |= LEFT elif x > xmax: code |= RIGHT if y < ymin: code |= BOTTOM elif y > ymax: code |= TOP return code # Cohen-Sutherland算法 def cohenSutherlandClip(x1, y1, x2, y2, xmin, ymin, xmax, ymax): code1 = computeCode(x1, y1, xmin, ymin, xmax, ymax) code2 = computeCode(x2, y2, xmin, ymin, xmax, ymax) while True: if code1 == 0 and code2 == 0: return x1, y1, x2, y2 elif code1 & code2 != 0: return None else: code = code1 if code1 != 0 else code2 if code & LEFT != 0: x = xmin y = y1 + (y2 - y1) * (xmin - x1) / (x2 - x1) elif code & RIGHT != 0: x = xmax y = y1 + (y2 - y1) * (xmax - x1) / (x2 - x1) elif code & BOTTOM != 0: y = ymin x = x1 + (x2 - x1) * (ymin - y1) / (y2 - y1) elif code & TOP != 0: y = ymax x = x1 + (x2 - x1) * (ymax - y1) / (y2 - y1) if code == code1: x1, y1 = x, y code1 = computeCode(x1, y1, xmin, ymin, xmax, ymax) else: x2, y2 = x, y code2 = computeCode(x2, y2, xmin, ymin, xmax, ymax) ``` 在上面的代码中,`computeCode`函数用于计算一个点的区域码,`cohenSutherlandClip`函数则是实现Cohen-Sutherland算法的主要部分。如果两个点都在裁剪窗口的内部,那么直接返回这两个点;如果两个点都在裁剪窗口的外部,那么直接返回`None`表示这条直线不可见;否则就计算出这条直线裁剪窗口的交点,并将交点作为新的顶点继续进行裁剪
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

USING2

你的鼓励是我最大的动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值