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

📄 2.1.2 直线的bresenham算法.htm

📁 计算机图形学教程计算机图形学教程
💻 HTM
📖 第 1 页 / 共 2 页
字号:
      <P style="MARGIN-TOP: 0px; MARGIN-BOTTOM: 0px" align=justify><FONT 
      face=Arial>dy=y2-y1;</FONT></P>
      <P style="MARGIN-TOP: 0px; MARGIN-BOTTOM: 0px" align=justify><FONT 
      face=Arial>if (dx*dy&gt;=0) /*</FONT>准备<FONT face=Arial>x</FONT>或<FONT 
      face=Arial>y</FONT>的单位递变值。<FONT face=Arial>*/</FONT></P>
      <P style="MARGIN-TOP: 0px; MARGIN-BOTTOM: 0px" align=justify><FONT 
      face=Arial>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 
      inc=1;</FONT></P>
      <P style="MARGIN-TOP: 0px; MARGIN-BOTTOM: 0px" align=justify><FONT 
      face=Arial>else</FONT></P>
      <BLOCKQUOTE>
        <P style="MARGIN-TOP: 0px; MARGIN-BOTTOM: 0px" align=justify><FONT 
        face=Arial>inc=-1;</FONT></P></BLOCKQUOTE>
      <P style="MARGIN-TOP: 0px; MARGIN-BOTTOM: 0px" align=justify><FONT 
      face=Arial>if (abs(dx)&gt;abs(dy)){</FONT></P>
      <P style="MARGIN-TOP: 0px; MARGIN-BOTTOM: 0px" align=justify><FONT 
      face=Arial>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; if(dx&lt;0){</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; 
      tmp=x1; /*</FONT>将<FONT face=Arial>2a, 3a</FONT>象限方向<FONT 
      face=Arial>*/</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; 
      x1=x2; /*</FONT>的直线变换到<FONT face=Arial>1a, 4a*/</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; 
      x2=tmp;</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; 
      tmp=y1; /*</FONT>象限方向去<FONT face=Arial>*/</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; 
      y1=y2;</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; 
      dx=-dy;</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; 
      dy=-dy;</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; 
      }</FONT></P>
      <P style="MARGIN-TOP: 0px; MARGIN-BOTTOM: 0px" align=justify><FONT 
      face=Arial>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; p=2*dy-dx;</FONT></P>
      <P style="MARGIN-TOP: 0px; MARGIN-BOTTOM: 0px" align=justify><FONT 
      face=Arial>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; const1=2*dy; 
      /*</FONT>注意此时误差的<FONT face=Arial>*/</FONT></P>
      <P style="MARGIN-TOP: 0px; MARGIN-BOTTOM: 0px" align=justify><FONT 
      face=Arial>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; const2=2*(dy-dy); 
      /*</FONT>变化参数取值<FONT face=Arial>. */</FONT></P>
      <P style="MARGIN-TOP: 0px; MARGIN-BOTTOM: 0px" align=justify><FONT 
      face=Arial>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; x=x1;</FONT></P>
      <P style="MARGIN-TOP: 0px; MARGIN-BOTTOM: 0px" align=justify><FONT 
      face=Arial>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; y=y1;</FONT></P>
      <P style="MARGIN-TOP: 0px; MARGIN-BOTTOM: 0px" align=justify><FONT 
      face=Arial>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; set_pixel(x, y, c);</FONT></P>
      <P style="MARGIN-TOP: 0px; MARGIN-BOTTOM: 0px" align=justify><FONT 
      face=Arial>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; while (x&lt;x2){</FONT></P>
      <P style="MARGIN-TOP: 0px; MARGIN-BOTTOM: 0px" align=justify><FONT 
      face=Arial>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 
      x++;</FONT></P>
      <P style="MARGIN-TOP: 0px; MARGIN-BOTTOM: 0px" align=justify><FONT 
      face=Arial>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; if 
      (p&lt;0)</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; 
      p+=const1;</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; 
      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;&nbsp;&nbsp;&nbsp;&nbsp; 
      y+=inc;</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; 
      p+=const2;</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; 
      }</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; 
      set_piexl(x, y, c);</FONT></P>
      <P style="MARGIN-TOP: 0px; MARGIN-BOTTOM: 0px" align=justify><FONT 
      face=Arial>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }</FONT></P>
      <P style="MARGIN-TOP: 0px; MARGIN-BOTTOM: 0px" align=justify><FONT 
      face=Arial>}</FONT></P>
      <P style="MARGIN-TOP: 0px; MARGIN-BOTTOM: 0px" align=justify><FONT 
      face=Arial>else {</FONT></P>
      <P style="MARGIN-TOP: 0px; MARGIN-BOTTOM: 0px" align=justify><FONT 
      face=Arial>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; if (dy&lt;0){</FONT></P>
      <P style="MARGIN-TOP: 0px; MARGIN-BOTTOM: 0px" align=left><FONT 
      face=Arial>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 
      tmp=x1; /* </FONT>将<FONT face=Arial>3b, 4b</FONT>象限方向的<FONT 
      face=Arial>*/</FONT></P>
      <P style="MARGIN-TOP: 0px; MARGIN-BOTTOM: 0px" align=left><FONT 
      face=Arial>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 
      x1=x2; /*</FONT>直线变换到<FONT face=Arial>2b, 1b */</FONT></P>
      <P style="MARGIN-TOP: 0px; MARGIN-BOTTOM: 0px" align=left><FONT 
      face=Arial>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 
      x2=tmp; /*</FONT>象限方向去<FONT face=Arial>. */</FONT></P>
      <P style="MARGIN-TOP: 0px; MARGIN-BOTTOM: 0px" align=left><FONT 
      face=Arial>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 
      tmp=y1;</FONT></P>
      <BLOCKQUOTE>
        <BLOCKQUOTE>
          <P style="MARGIN-TOP: 0px; MARGIN-BOTTOM: 0px" align=left><FONT 
          face=Arial>y1=y2;</FONT></P>
          <P style="MARGIN-TOP: 0px; MARGIN-BOTTOM: 0px" align=left><FONT 
          face=Arial>dx=-dy;</FONT></P>
          <P style="MARGIN-TOP: 0px; MARGIN-BOTTOM: 0px" align=left><FONT 
          face=Arial>dy=-dy;</FONT></P>
          <P style="MARGIN-TOP: 0px; MARGIN-BOTTOM: 0px" align=justify><FONT 
          face=Arial>}</FONT></P></BLOCKQUOTE></BLOCKQUOTE>
      <BLOCKQUOTE>
        <P style="MARGIN-TOP: 0px; MARGIN-BOTTOM: 0px" align=justify><FONT 
        face=Arial>p=2*dx-dy; /*</FONT>注意此时误差的<FONT face=Arial>*/</FONT></P>
        <P style="MARGIN-TOP: 0px; MARGIN-BOTTOM: 0px" align=justify><FONT 
        face=Arial>const1=2*dx; /*</FONT>变化参数取值<FONT face=Arial>. */</FONT></P>
        <P style="MARGIN-TOP: 0px; MARGIN-BOTTOM: 0px" align=justify><FONT 
        face=Arial>const2=2*(dx-dy);</FONT></P>
        <P style="MARGIN-TOP: 0px; MARGIN-BOTTOM: 0px" align=justify><FONT 
        face=Arial>x=x1;</FONT></P>
        <P style="MARGIN-TOP: 0px; MARGIN-BOTTOM: 0px" align=justify><FONT 
        face=Arial>y=y1;</FONT></P>
        <P style="MARGIN-TOP: 0px; MARGIN-BOTTOM: 0px" align=justify><FONT 
        face=Arial>set_pixel (x, y, c);</FONT><FONT 
      face=宋体></P></FONT></BLOCKQUOTE>
      <P style="MARGIN-TOP: 0px; MARGIN-BOTTOM: 0px" align=justify><FONT 
      face=Arial>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; while (y&lt;y2){</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; 
      y++;</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; 
      if(p&lt;0)</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; 
      p+=const1;</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; 
      else{</FONT></P>
      <BLOCKQUOTE>
        <BLOCKQUOTE>
          <P style="MARGIN-TOP: 0px; MARGIN-BOTTOM: 0px" align=justify><FONT 
          face=Arial>x+=inc;</FONT></P>
          <P style="MARGIN-TOP: 0px; MARGIN-BOTTOM: 0px" align=justify><FONT 
          face=Arial>p+=const2;</FONT></P>
          <P style="MARGIN-TOP: 0px; MARGIN-BOTTOM: 0px" align=justify><FONT 
          face=Arial>set_pixel (x, y, c);</FONT></P>
          <P style="MARGIN-TOP: 0px; MARGIN-BOTTOM: 0px" align=justify><FONT 
          face=Arial>}</FONT></P></BLOCKQUOTE></BLOCKQUOTE>
      <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; 
      }</FONT></P>
      <P style="MARGIN-TOP: 0px; MARGIN-BOTTOM: 0px" align=justify><FONT 
      face=Arial>}</FONT></P>
      <P style="MARGIN-TOP: 0px; MARGIN-BOTTOM: 0px" align=justify> </P>
      <P style="MARGIN-TOP: 0px; MARGIN-BOTTOM: 0px" align=justify>程序<FONT 
      face=Arial>2.1.2 </FONT>适用于直线所有八个方向的<FONT 
      face=Arial>Bresenham</FONT>直线生成算法</P></BLOCKQUOTE></BLOCKQUOTE></BLOCKQUOTE>
<P><A href="http://www.ekany.com/wdg98/cg/contents/chapter2/les211.htm"><FONT 
face=楷体_GB2312>&lt;上一节〉</FONT></A><FONT face=楷体_GB2312>&nbsp;&nbsp;&nbsp; <A 
href="http://www.ekany.com/wdg98/cg/contents/chapter2/les221.htm">〈下一节〉</A> 
&nbsp;&nbsp;&nbsp; <A 
href="http://www.ekany.com/wdg98/cg/tutorial/chapter2/lesson2-1.htm">〈返回〉</A></FONT></P>
<P> </P></BODY></HTML>

⌨️ 快捷键说明

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