⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 2.6 图形裁剪.htm

📁 计算机图形学教程计算机图形学教程
💻 HTM
📖 第 1 页 / 共 3 页
字号:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<!-- saved from url=(0058)http://www.ekany.com/wdg98/cg/contents/chapter2/les261.htm -->
<HTML><HEAD><TITLE>2</TITLE>
<META http-equiv=Content-Type content="text/html; charset=gb2312">
<META content="MSHTML 6.00.2800.1106" name=GENERATOR></HEAD>
<BODY>
<H2 align=justify><A name="2.6 图形裁剪"><B><FONT face=楷体_GB2312><FONT size=5>2.6 
图形裁剪 </FONT></FONT></B></A></H2>
<P align=justify><FONT face=楷体_GB2312><FONT size=4>&nbsp;&nbsp;&nbsp; 
本节中,我们讨论一个二维矩形区域的裁剪,这个矩形区域称为窗口。当窗口被确定之后,只有窗口内的物体才能显示出来。窗口之外的物体都是不可见的。因此,可以不参加标准化转换及随后的显示操作,从而节约处理时间。裁剪(clipping)是裁去窗口之外物体的一种操作。</FONT></FONT><B></P>
<H3 align=justify><A name="2.6.1 直线的剪裁"><FONT face=楷体_GB2312 size=4>2.6.1 
直线的剪裁</FONT></A></B></H3>
<P align=justify><FONT face=楷体_GB2312 
size=4>直线和窗口的关系可以分为如下三类(图2.6.1):</FONT></P>
<P align=justify><FONT face=楷体_GB2312 
size=4>(a)整条直线在窗口之内。此时,不需剪裁,显示整条直线。</FONT></P>
<P align=justify><FONT face=楷体_GB2312 
size=4>(b)整条直线在窗口之外,此时,不需剪裁,不显示整条直线。</FONT></P>
<P align=justify><FONT face=楷体_GB2312 
size=4>(c)部分直线在窗口之内,部分在窗口之外。此时,需要求出直线与窗框之交点,并将窗口外的直线部分剪裁掉,显示窗口内的部分。</FONT></P>
<P align=justify><FONT face=楷体_GB2312 
size=4>直线剪裁算法有两个主要步骤:首先将不需剪裁的直线挑出,并删去其中在窗外的直线。然后,对其余直线,逐条与窗框求交点,并将窗外部分删去。下面介绍的直线剪裁算法是由Cohen及Sutherland提出的。</FONT></P>
<P align=justify><FONT face=楷体_GB2312 
size=4>Cohen-Sutherland直线剪裁算法以区域编码为基础,将窗口及其周围的八个方向以4 
bit的二进制数进行编码。4个bit分别代表窗外上、下、右、左空间的编码值。如左上区域编码为1001,右上区域编码为1010。窗内编码为0000,如图2.6.2所示。</FONT></P>
<P align=justify><FONT face=楷体_GB2312 size=4>&nbsp;&nbsp;&nbsp;&nbsp; <IMG 
height=157 alt="2_6_1.gif (2719 bytes)" src="2.6 图形裁剪.files/2_6_1.gif" 
width=295>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <IMG height=191 
alt="2_6_2.gif (2745 bytes)" src="2.6 图形裁剪.files/2_6_2.gif" 
width=251></FONT></P>
<P align=justify><FONT face=楷体_GB2312 
size=4>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 图2.6.1 直线与窗口的关系图 
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 
2.6.2 直线剪裁算法中的区域编码</FONT></P>
<P align=justify><FONT face=楷体_GB2312 
size=4>图2.6.2所示的编码方法将窗口及其邻域分为5个区域:</FONT></P>
<P align=justify><FONT face=楷体_GB2312 size=4>内域:区域(0000);</FONT></P>
<P align=justify><FONT face=楷体_GB2312 size=4>上域:区域(1001,1000,1010);</FONT></P>
<P align=justify><FONT face=楷体_GB2312 size=4>下域:区域(0101,0100,0110);</FONT></P>
<P align=justify><FONT face=楷体_GB2312 size=4>左域、区域(1001,0001,0101);</FONT></P>
<P align=justify><FONT face=楷体_GB2312 size=4>右域:区域(1010,0010,0110)</FONT></P>
<P align=justify><FONT face=楷体_GB2312 size=4>这就带来二个优点:</FONT></P>
<P align=justify><FONT face=楷体_GB2312 
size=4>1、容易将不需剪裁的直线挑出。规则是:如果一条直线的两端在同一区域,则该直线不需剪裁,否则,该直线为可能剪裁直线。</FONT></P>
<P align=justify><FONT face=楷体_GB2312 
size=4>2、对可能剪裁的直线缩小了与之求交的边框范围。规则是:如果直线的一个端点在上(下,左,右)域,则此直线与上边框求交,然后删去上边框以上的部分。该规则对直线的另一端点也适用。这样,一条直线至多只需与两条边框求交。</FONT></P>
<P align=justify><FONT face=楷体_GB2312 
size=4>因此,Cohen-Sutherland的区域编码剪裁算法是一个简明高速的直线剪裁算法。算法的主要思想为:</FONT></P>
<P align=justify><FONT face=楷体_GB2312 size=4>依次对每条直线p1p2作如下处理:</FONT></P>
<P align=justify><FONT face=楷体_GB2312 size=4>1)对直线两端点p1, 
p2按各自所在的区域编码。p1和p2的编码分别记为:</FONT></P>
<P align=justify><FONT face=楷体_GB2312 size=4>C1(p1)={a1, b1, c1, d1}</FONT></P>
<P align=justify><FONT face=楷体_GB2312 size=4>C2(p2)={a2, b2, c2, d2}</FONT></P>
<P align=justify><FONT face=楷体_GB2312 size=4>(其中ai, bi, ci, di取值域为{1, 0}, i={1, 
2})</FONT></P>
<P align=justify><FONT face=楷体_GB2312 size=4>2)if 
(ai=bi=ci=di=0则显示整条直线,取出下一条直线,返1);否则</FONT></P>
<P><FONT face=楷体_GB2312 size=4>&nbsp;&nbsp;&nbsp;&nbsp; if [(a1&Ccedil; a2)&Egrave; (b1&Ccedil; b2)&Egrave; 
(c1&Ccedil; c2)&Egrave; (d1&Ccedil; d2)=1]则取出下一条直线,返1);否则</FONT></P>
<P align=justify><FONT face=楷体_GB2312 size=4>3)if(a1&Egrave; 
a2=1)则求直线与窗上边(y=Yw-max)之交点,并删去交点以上部分;</FONT></P>
<P align=justify><FONT face=楷体_GB2312 size=4>if(b1&Egrave; 
b2=1)则求直线与窗下边(y=Yw-min)之交点,并删去交点以下部分;</FONT></P>
<P align=justify><FONT face=楷体_GB2312 size=4>if(c1&Egrave; 
c2=1)则求直线与窗右边(x=Xw-max)之交点,并删去交点以右部分;</FONT></P>
<P align=justify><FONT face=楷体_GB2312 size=4>if(d1&Egrave; 
d2=1)则求直线与窗左边(x=Xw-min)之交点,并删去交点以左部分;</FONT></P>
<P align=justify><FONT face=楷体_GB2312 size=4>4)返1);</FONT></P>
<P align=justify><FONT face=楷体_GB2312 
size=4>上述算法思想由程序2.6.1实现。其中参数与变量的含义为:</FONT></P>
<P align=justify><FONT face=楷体_GB2312 size=4>x1, y1, x2, y2:输入直线两端坐标;</FONT></P>
<P align=justify><FONT face=楷体_GB2312 size=4>code1, code2:两端点的编码,各四位。</FONT></P>
<P align=justify><FONT face=楷体_GB2312 size=4>done:是否剪裁完毕的标志,True:剪裁完。</FONT></P>
<P align=justify><FONT face=楷体_GB2312 size=4>display:是否需显示的标志,True:显示直线x1, y1, 
x2, y2。</FONT></P>
<P align=justify><FONT face=楷体_GB2312 size=4>m:直线之斜率。</FONT></P>
<P align=justify><FONT face=楷体_GB2312 
size=4>主程序为clip_a_line。它调用四个子程序,功能分别为:</FONT></P>
<P align=justify><FONT face=楷体_GB2312 size=4>1)encode(x, y, c):功能为:判断点(x, 
y)所在的区域,赋予c以相应的编码(程序2.6.2)。</FONT></P>
<P align=justify><FONT face=楷体_GB2312 size=4>2)accept(c1, c2):功能为:根据两端点的编码c1, 
c2,判断直线是否在窗口之内(程序2.6.3)。</FONT></P>
<P align=justify><FONT face=楷体_GB2312 size=4>3)reject(c1, c2):功能为:根据两端点的编码c1, 
c2,判断直线是否在窗口之外(程序2.6.4)。</FONT></P>
<P align=justify><FONT face=楷体_GB2312 size=4>4)swap_if_needed(x1, y1, x2, y2, 
c1, c2);功能为:判断(x1, y1)是否在窗口之外,如否,则将x1, y1, c1值与x2, y2, 
c2值交换(程序2.6.5)。</FONT></P>
<BLOCKQUOTE>
  <P style="MARGIN-TOP: 0px; MARGIN-BOTTOM: 0px" align=justify><FONT 
  face=Arial>clip_a_line(x1, y1, x2, y2, xw_min, xw_max, yw_min, 
  yw_max)</FONT></P>
  <P style="MARGIN-TOP: 0px; MARGIN-BOTTOM: 0px" align=justify><FONT 
  face=Arial>int x1, x2, y1, y2</FONT>; <FONT face=Arial>xw_min, xw_max, yw_min, 
  yw_max;</FONT></P>
  <P style="MARGIN-TOP: 0px; MARGIN-BOTTOM: 0px" align=justify><FONT 
  face=Arial>{</FONT></P>
  <BLOCKQUOTE>
    <P style="MARGIN-TOP: 0px; MARGIN-BOTTOM: 0px" align=justify><FONT 
    face=Arial>int i, code1[4], code2[4], done, display;</FONT></P>
    <P style="MARGIN-TOP: 0px; MARGIN-BOTTOM: 0px" align=justify><FONT 
    face=Arial>float m;</FONT></P>
    <P style="MARGIN-TOP: 0px; MARGIN-BOTTOM: 0px" align=justify><FONT 
    face=Arial>int x11, x22, y11, y22, mark;</FONT></P>
    <P style="MARGIN-TOP: 0px; MARGIN-BOTTOM: 0px" align=justify><FONT 
    face=Arial>done = 0;</FONT></P>
    <P style="MARGIN-TOP: 0px; MARGIN-BOTTOM: 0px" align=justify><FONT 
    face=Arial>display = 0;</FONT></P>
    <P style="MARGIN-TOP: 0px; MARGIN-BOTTOM: 0px" align=justify><FONT 
    face=Arial>while(done = = 0)</FONT></P>
    <P style="MARGIN-TOP: 0px; MARGIN-BOTTOM: 0px" align=justify><FONT 
    face=Arial>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; {</FONT></P>
    <P style="MARGIN-TOP: 0px; MARGIN-BOTTOM: 0px" align=justify><FONT 
    face=Arial>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; x11=x1; x22=x2; 
    y11=y1; y22=y2;</FONT></P>
    <P style="MARGIN-TOP: 0px; MARGIN-BOTTOM: 0px" align=justify><FONT 
    face=Arial>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; encode(x1, y1, 
    code1, xw_min, xw_max, yw_min, yw_max);</FONT></P>
    <P style="MARGIN-TOP: 0px; MARGIN-BOTTOM: 0px" align=justify><FONT 
    face=Arial>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; encode(x2, y2, 
    code2, xw_min, xw_max, yw_min, yw_max);</FONT></P>
    <P style="MARGIN-TOP: 0px; MARGIN-BOTTOM: 0px" align=justify><FONT 
    face=Arial>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; if(accept(code1, 
    code2))</FONT></P>
    <P style="MARGIN-TOP: 0px; MARGIN-BOTTOM: 0px" align=justify><FONT 
    face=Arial>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 
    {</FONT></P>
    <P style="MARGIN-TOP: 0px; MARGIN-BOTTOM: 0px" align=justify><FONT 
    face=Arial>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 
    done=1;</FONT></P>
    <P style="MARGIN-TOP: 0px; MARGIN-BOTTOM: 0px" align=justify><FONT 
    face=Arial>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 
    display=1;</FONT></P>
    <P style="MARGIN-TOP: 0px; MARGIN-BOTTOM: 0px" align=justify><FONT 
    face=Arial>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 
    break;</FONT></P>
    <P style="MARGIN-TOP: 0px; MARGIN-BOTTOM: 0px" align=justify><FONT 
    face=Arial>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 
    }</FONT></P>
    <P style="MARGIN-TOP: 0px; MARGIN-BOTTOM: 0px" align=justify><FONT 
    face=Arial>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; else</FONT></P>
    <P style="MARGIN-TOP: 0px; MARGIN-BOTTOM: 0px" align=justify><FONT 
    face=Arial>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 
    if(reject(codel, code2))</FONT></P>
    <P style="MARGIN-TOP: 0px; MARGIN-BOTTOM: 0px" align=justify><FONT 
    face=Arial>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 
    {</FONT></P>
    <P style="MARGIN-TOP: 0px; MARGIN-BOTTOM: 0px" align=justify><FONT 
    face=Arial>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 
    done=1;</FONT></P>
    <P style="MARGIN-TOP: 0px; MARGIN-BOTTOM: 0px" align=justify><FONT 
    face=Arial>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 
    break;</FONT></P>
    <P style="MARGIN-TOP: 0px; MARGIN-BOTTOM: 0px" align=justify><FONT 
    face=Arial>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 
    }</FONT></P>
    <P style="MARGIN-TOP: 0px; MARGIN-BOTTOM: 0px" align=justify><FONT 
    face=Arial>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -