銆愬崄澶╄嚜鍒惰蒋娓叉煋鍣ㄣ€慏AY 02锛氱敾涓€鏉$洿绾匡紙DDA 绠楁硶 & Bresenham鈥檚 绠楁硶锛?/a>

Posted 鍗や唬鐑?,

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了銆愬崄澶╄嚜鍒惰蒋娓叉煋鍣ㄣ€慏AY 02锛氱敾涓€鏉$洿绾匡紙DDA 绠楁硶 & Bresenham鈥檚 绠楁硶锛?/a>相关的知识,希望对你有一定的参考价值。


鍐欐枃涓嶆槗锛屾伋姹傚悇浣嶈浼楄€佺埛 鐐硅禐 馃憤锛屾敹钘?馃搧锛岃瘎璁?馃挰 涓夎繛鏀寔涓€涓嬶紒锛侊紒璋㈣阿浣狅紝杩欏鎴戠湡鐨勫緢閲嶈锛?/p>


绗竴澶╂垜浠惌寤轰簡 C++ 鐨勮繍琛岀幆澧冨苟鐢讳簡涓€涓偣锛屾牴鎹?鐐?鈫?绾?鈫?闈?/code> 鐨勯『搴忥紝浠婂ぉ鎴戜滑璁茶濡備綍鐢讳竴鏉$洿绾裤€?/p>

鏈枃涓昏璁茶В鐩寸嚎缁樺埗绠楁硶鐨勬帹瀵煎拰鎬濊矾锛堣帿鎷呭績锛屽彧娑夊強鍒颁竴鐐圭偣鐨勪腑瀛︽暟瀛︾煡璇嗭級锛屾渶鍚庝細缁欏嚭浠g爜瀹炵幇锛屽ぇ瀹舵斁蹇冪殑鐪嬩笅鍘诲氨濂姐€?/p>

1.DDA 鐩寸嚎绠楁硶

1.1 绠€鍗曞疄鐜?/h3>

鎴戜滑鍏堟潵鍥為【涓€涓嬩腑瀛︾殑鍑犱綍鐭ヨ瘑锛屽浣曞湪浜岀淮骞抽潰鍐呰〃绀轰竴鏉$洿绾匡紵鏈€甯歌鐨勫氨鏄?strong>鏂滄埅寮?/strong>浜嗭細

$$y = kx + b$$

鍏朵腑鏂滅巼鏄?$k$锛岀洿绾垮湪 $y$ 杞翠笂鐨勬埅璺濇槸 $b$銆?/p>


鏂滄埅寮?/strong>鍦ㄦ暟瀛︿笂鏄病鍟ラ棶棰樼殑锛屼絾鏄湪瀹為檯鐨勫伐绋嬮」鐩腑锛屽洜涓虹‖浠惰祫婧愭槸鏈夐檺鐨勶紝鎴戜滑涓嶅彲鑳戒篃娌″繀瑕?/strong>琛ㄧず涓€鏉℃棤闄愰暱搴︾殑鐩寸嚎锛岀幇瀹炲線寰€鏄凡鐭ヤ竴鏉$嚎娈电殑璧风偣 $(x_1, y_1)$ 鍜?strong>缁堢偣 $(x_2, y_2$)锛岀劧鍚庢妸瀹冪敾鍑烘潵銆?/p>

杩欐椂鍊欑敤涓ょ偣寮?/strong>琛ㄧず涓€鏍圭洿绾挎槸鏈€鏂逛究鐨勶紝鍏朵腑 $x_1 \\leq x \\leq x_2$锛?y_1 \\leq y \\leq y_2$锛?/p>

$$\\frac{x-x_{1}}{x_{2}-x_{1}}=\\frac{y-y_{1}}{y_{2}-y_{1}}$$

鎶婁笂闈㈢殑寮忓瓙绋嶄綔鍙樺舰锛屽彲浠ユ妸 $x$ 鍜?$y$ 鐢ㄥ弬鏁?$\\lambda$ 琛ㄧず锛?/p>

$$\\left.\\begin{array}{l}x=\\lambda\\left(x_{2}-x_{1}\\right)+x_{1} \\\\ y=\\lambda\\left(y_{2}-y_{1}\\right)+y_{1}\\end{array}\\right\\} 0 \\leq \\lambda \\leq 1$$

杩欐椂鍊欐垜浠彧瑕佸彇涓嶅悓鐨?$\\lambda$锛屽氨鍙互寰楀嚭瀵瑰簲鐨?x 鍜?y銆?/p>


鎸夌収浠ヤ笂鐨勬€濊矾锛屾垜浠彲浠ョ敤浠g爜瀹炵幇涓€涓嬨€?code>C++ 鐨勫疄鐜颁篃寰堢畝鍗曪紝濡備笅鎵€绀猴紙dl 琛ㄧず $d \\lambda$锛夛細

void line(
  int x1, int y1, 
  int x2, int y2, 
  TGAImage &image, TGAColor color) { 
    const float dl = 0.01;
    int dx = x2 - x1;
    int dy = y2 - y1;
    for (float t=0.0; t<1.0; t+=dl) { 
        int x = x1 + dx * t;
        int y = y1 + dy * t;
        image.set(x, y, color);
    } 
}

杩欎釜鏄洿绾跨畻娉曠殑鍒濇瀹炵幇锛屽彧鑳借銆岃兘鐢ㄣ€嶏紝鍦颁綅鍜屾帓搴忕畻娉曢噷鐨勩€屽啋娉℃帓搴忋€嶄竴鏍凤紝鐩殑杈惧埌浜嗭紝浣嗘槸鎬ц兘涓嶅お濂斤細

  • 姣忕敾涓€涓偣锛岄兘瑕佽繍琛屼袱娆′箻娉?/li>
  • 澶ч噺浣跨敤娴偣杩愮畻锛堜紬鎵€鍛ㄧ煡锛?v_{float}$ < $v_{fixed}$ < $v_{int}$锛?/li>
  • 濡傛灉 dl 鍙栫殑姣旇緝灏忥紝浼氬鑷翠竴涓儚绱犵偣浼氳缁樺埗澶氭锛岄噸澶嶈绠?/li>
  • 濡傛灉 dl 鍙栫殑姣旇緝澶э紝浼氬鑷寸洿绾挎柇鎺?/li>

1.2 浼樺寲

涓嬮潰鎴戜滑灏变竴姝ヤ竴姝ヤ紭鍖栦笂闈㈢殑绠楁硶銆?/p>

棣栧厛鎴戜滑娉ㄦ剰鍒帮紝瀵逛簬灞忓箷缁樺埗鐩寸嚎杩欎釜鍦烘櫙锛?strong>鐞嗚涓婃槸杩炵画鐨勶紝浣嗗疄闄呮槸绂绘暎鐨?/strong>銆?/p>

姣斿璇?$x$ 浠?$x_1$ 鍙樺寲鍒?$x_2$ 鏃讹紝姣忔缁樺埗鏃讹紝$x$ 閮芥槸鎸夋闀?1 澧為暱鐨勶紝涔熷氨鏄?$x_{new} = x_{old} + 1$銆?/p>

杩欐椂鍊?$y_{new} = y_{old} + \\frac{y_2 - y_1}{x_{2}-x_{1}} = y_{old} + k$銆?/p>

鎴戜滑鎶婁笂闈㈢殑鍏紡鍐欐垚浠g爜锛屽氨鏄笅闈㈣繖涓牱瀛愶細

void line(
  int x1, int y1, 
  int x2, int y2, 
  TGAImage &image, TGAColor color) { 
  float x = x1;
  float y = y1;
  float step = std::abs(x2 - x1);
  float dlx = (x2 - x1) / step;
  float dly = (y2 - y1) / step;
  
  for (int i=1; i<step; i++) { 
    image.set(x, y, color);
    x = x + dlx;
    y = y + dlx;
  } 
}

杩欎釜绠楁硶鍏跺疄杩樻湁涓€鐐瑰効闂锛屽氨鏄粯鍒舵枩鐜囧ぇ浜?1 鐨勭洿绾挎椂锛岀粯鍒跺嚭鐨勭洿绾夸細鏂帀銆傛瘮濡傝浠?(0, 0) 鐐圭粯鍒跺埌 (2, 4) 鐐癸紝鎸夌収涓婇潰鐨勭畻娉曞彧浼氱粯鍒朵袱涓偣锛屼絾鏄垜浠湡鏈涚殑鏄彸鍥鹃偅鏍凤紝璧风爜鍚勪釜鍍忕礌瑕佽繛鎺ヨ捣鏉ワ細

瑙e喅鏂规硶涔熷緢绠€鍗曪紝缁樺埗杩欑姣旇緝銆岄櫋宄€嶇殑鐩寸嚎鏃讹紙鏂滅巼缁濆鍊煎ぇ浜?1锛夛紝浠?y 鐨勫彉鍖栦负鍩哄噯锛岃€屼笉鏄互 x锛岃繖鏍峰氨鍙互閬垮厤涓婇潰鐩寸嚎涓嶈繛缁儏鍐点€?/p>

鏈€鍚庣殑鐩寸嚎绠楁硶灏辨槸杩欐牱锛?/p>

void line(
  int x1, int y1, 
  int x2, int y2, 
  TGAImage &image, TGAColor color) { 
  float x = x1;
  float y = y1;
  int dx = x2 - x1;
  int dy = y2 - y1;
  float step;
  float dlx, dly;

  // 鏍规嵁 dx 鍜?dy 鐨勯暱搴﹀喅瀹氬熀鍑?  if (std::abs(dx) >= std::abs(dy)) {
    step = std::abs(dx);
  } else {
    step = std::abs(dy);
  }

  dlx = dx / step;
  dly = dy / step;

  for (int i=1; i<step; i++) {
    image.set(x, y, color);
    x = x + dlx;
    y = y + dly;
  }
}

鐒跺悗鎴戜滑鐢ㄨ繖涓畻娉曟祴璇曚竴涓嬩笉鍚岃捣鐐逛笉鍚屾枩鐜囩殑鐩寸嚎锛岀湅鏁堟灉杩愯鑹ソ锛?/p>


杩欎釜绠楁硶灏辨槸缁忓吀鐨?DDA (Digital differential analyzer) 绠楁硶)锛屼粬姣旀垜浠竴寮€濮嬬殑浠g爜瑕侀珮鏁堢殑澶氾細

  • 娑堥櫎浜嗗惊鐜唴鐨勪箻娉曡繍绠?/li>
  • 閬垮厤浜嗛噸澶嶇殑缁樺埗杩愮畻
  • 淇濊瘉绾挎杩炵画涓嶄細鏂帀

浣嗘槸瀹冭繕鏈変釜寰堣€楁€ц兘鐨勯棶棰橈細璁$畻杩囩▼涓秹鍙婂ぇ閲忕殑娴偣杩愮畻銆?/p>

浣滀负娓叉煋鍣ㄦ渶搴曞眰鐨勭畻娉曪紝鎴戜滑鑲畾甯屾湜鏄秺蹇秺濂姐€備笅闈㈡垜浠氨鏉ュ涔犱竴涓嬶紝娑堥櫎娴偣杩愮畻鐨?Bresenham鈥檚 鐩寸嚎绠楁硶銆?/p>

2.Bresenham鈥檚 鐩寸嚎绠楁硶

2.1 鍒濇瀹炵幇

鏈妭鍐呭涓嶄細浠庝竴寮€濮嬪氨璁插畬鍠勭増鐨?Bresenham鈥檚 绠楁硶锛屾垜浠厛浠庝竴涓皬鑺傚紑濮嬫帹瀵硷紝鏈€鍚庢帹瀵煎嚭瀹屽杽鐨勭畻娉曘€?/p>

鏈€涓€寮€濮嬶紝鎴戜滑鍏堣€冭檻鎵€鏈夌洿绾块噷鐨勪竴涓瓙闆嗭紝鍗虫枩鐜囪寖鍥村湪 $[0, 1]$ 涔嬮棿鐨勭洿绾匡細$0 \\leq k \\leq 1$銆?/p>

涓婁竴灏忚妭閲屾垜浠杩囷紝瀵逛簬灞忓箷缁樺埗鐩寸嚎杩欎釜鍦烘櫙锛?strong>鐞嗚涓婃槸杩炵画鐨勶紝浣嗗疄闄呮槸绂绘暎鐨?/strong>銆傛垜浠厛鍋囪宸茬粡缁樺埗浜嗕竴涓偣 $(x,\\ y)$锛岄偅涔堝湪鍍忕礌灞忓箷涓婏紝涓嬩竴涓柊鐐圭殑浣嶇疆锛屽彧鍙兘鏈変袱绉嶆儏鍐碉細

  • $(x + 1,\\ y)$
  • $(x + 1,\\ y + 1)$

閭d箞闂灏辫浆鍖栦负锛屼笅涓€涓柊鐐圭殑浣嶇疆璇ュ浣曢€夋嫨锛?/p>

鎴戞兂澶у搴旇閮芥兂鍒版柟妗堜簡锛屽ぇ浣撴€濊矾濡備笅

  • 鍏堟妸 $x_{new} = x + 1$ 杩欎釜鍊煎甫鍏ョ洿绾挎柟绋嬮噷锛岀畻鍑烘潵 $y_{new}$ 鐨勫€?/li>
  • 鐒跺悗姣旇緝 $y_{new}$ 鍜?$y + 0.5$ 鐨勫ぇ灏?/p>

    • $y_{new} \\leq y + 0.5$锛岄€夌偣 $(x + 1,\\ y)$
    • $y_{new} > y + 0.5$锛岄€夌偣 $(x + 1,\\ y + 1)$

鎴戜滑鍐嶆妸鎬濊矾瀹屽杽涓€涓嬶紝鎶婃瘡娆″彇鑸嶆椂鐨勮宸€冭檻杩涘幓锛?/p>

濡備笂鍥炬墍绀猴紝瀹為檯涓婄粯鍒剁殑鐐圭殑浣嶇疆鏄?$(x, y)$锛岀悊璁轰笂鐐逛綅缃槸 $(x,\\ y + \\epsilon)$銆?/p>

褰撶偣浠?$x$ 绉诲姩鍒?$x + 1$ 鏃讹紝鐞嗚涓婃柊鐐圭殑浣嶇疆搴旇鏄?$(x + 1,\\ y + \\epsilon + k)$锛屽叾涓?k 鏄洿绾跨殑鏂滅巼銆?/p>

瀹為檯缁樺埗鏃讹紝瑕佹瘮杈?$y + \\epsilon + k$ 鍜?$y + 0.5$ 鐨勫ぇ灏忥細

  • $y + \\epsilon + k \\leq y + 0.5$锛岄€夌偣 $(x + 1,\\ y)$
  • $y + \\epsilon + k > y + 0.5$锛岄€夌偣 $(x + 1,\\ y + 1)$

瀵逛簬涓嬩竴涓柊鐐?$x + 2$锛屾垜浠彲浠ユ寜鐓т笅寮忔洿鏂拌宸?$\\epsilon$锛?/p>

  • 鑻ュ墠涓€涓偣閫夋嫨鐨勬槸 $(x + 1,\\ y)$锛屽垯 $\\epsilon_{new} = (y + \\epsilon + k) - y = \\epsilon + k$
  • 鑻ュ墠涓€涓偣閫夋嫨鐨勬槸 $(x + 1,\\ y + 1)$锛屽垯 $\\epsilon_{new} = (y + \\epsilon + k) - (y + 1) = \\epsilon + k - 1$

鎶婁笂闈㈢殑鎬濊€冭繃绋嬬敤浼唬鐮佽〃绀轰竴涓嬶細

$
begin{aligned}
& epsilon leftarrow 0, quad y leftarrow y_{1} \\
& pmb{for} x leftarrow x_{1} pmb{to} x_{2} pmb{do} \\
& quad pmb{draw} pmb{point} pmb{at} (x, y) \\
& quad pmb{if} ( (epsilon + k) < 0.5) \\
& quad quad epsilon leftarrow epsilon + k \\
& quad pmb{else} \\
& quad quad y leftarrow y + 1 \\
& quad quad epsilon leftarrow epsilon + k - 1 \\
& quad pmb{end} pmb{if} \\
& pmb{end} pmb{for}
end{aligned}
$

2.2 娑堥櫎娴偣杩愮畻

瑙傚療涓婇潰鐨勪吉浠g爜锛屾垜浠彲浠ュ彂鐜拌繖閲岄潰鍑虹幇浜?0.5锛屼篃灏辨槸璇村瓨鍦ㄦ诞鐐硅繍绠椼€備笅闈㈡垜浠氨閫氳繃涓€浜涚瓑浠风殑鏁板鍙樻崲娑堥櫎娴偣鏁般€?/p>

棣栧厛瀵逛簬涓嶇瓑寮?$\\epsilon + k < 0.5$锛屾垜浠粰瀹冧笉绛夊彿宸﹀彸涓よ竟鍚屾椂涔樹互 2 鍊嶇殑 $\\Delta x$锛岃繖鏍峰氨鍙互鍚屾椂娑堥櫎鏂滅巼闄ゆ硶鍜屽父閲?0.5 甯︽潵鐨勬诞鐐硅繍绠?

$$\\epsilon + \\Delta y / \\Delta x < 0.5$$

$$2 \\epsilon \\Delta x + 2 \\Delta y <\\Delta x$$

鐒跺悗鐢?$\\epsilon^{\\prime}$ 琛ㄧず $\\epsilon\\Delta x$锛屼笂寮忓彲浠ヨ浆鎹负 $$2(\\epsilon^{\\prime} + \\Delta y)< \\Delta x$$

鍚屾牱鐨勶紝鎴戜滑鍦ㄦ洿鏂?$\\epsilon$ 鏃讹紝鎶婂畠涔熸浛鎹负 $\\epsilon^{\\prime}$ 锛屼篃灏辨槸瀵逛簬涓嬮潰涓ゅ紡锛?/p>

  • $\\epsilon = \\epsilon + m$
  • $\\epsilon = \\epsilon + m - 1$

绛夊彿涓よ竟鍚屾椂涔樹互 $\\Delta x$锛屾湁锛?/p>

  • $\\epsilon \\Delta x = \\epsilon \\Delta x+\\Delta y$
  • $\\epsilon \\Delta x = \\epsilon \\Delta x+\\Delta y-\\Delta x$

鐒跺悗鐢?$\\epsilon^{\\prime}$ 琛ㄧず $\\epsilon\\Delta x$锛屽彲浠ュ緱鍒帮細

  • $\\epsilon^{\\prime} = \\epsilon^{\\prime}+\\Delta y$
  • $\\epsilon^{\\prime} = \\epsilon^{\\prime}+\\Delta y-\\Delta x$

杩欐椂鍊欐垜浠氨鍙互寰楀埌涓€涓幓鎺夋诞鐐规暟杩愮畻鐨勪吉浠g爜锛?/p>

$
begin{aligned}
& epsilon^{prime} leftarrow 0, quad y leftarrow y_{1} \\
& pmb{for} x leftarrow x_{1} pmb{to} x_{2} pmb{do} \\
& quad pmb{draw} pmb{point} pmb{at} (x, y) \\
& quad pmb{if} ( 2 (epsilon^{prime} + Delta y) < Delta x) \\
& quad quad epsilon^{prime} leftarrow epsilon^{prime} + Delta y \\
& quad pmb{else} \\
& quad quad y leftarrow y + 1 \\
& quad quad epsilon^{prime} leftarrow epsilon^{prime} + Delta y - Delta x \\
& quad pmb{end} pmb{if} \\
& pmb{end} pmb{for}
end{aligned}
$


C++ 瀹炵幇濡備笅锛?/p>

void line(Screen &s,
  int x1, int y1,
  int x2, int y2,
  TGAImage &image, TGAColor color) {
  int y = y1;
  int eps = 0;
  int dx = x2 - x1;
  int dy = y2 - y1;

  for (int x = x1; x <= x2; x++) {
    image.set(x, y, color);
    eps += dy;
    // 杩欓噷鐢ㄤ綅杩愮畻 <<1 浠f浛 *2
    if((eps << 1) >= dx)  {
      y++;
      eps -= dx;
    }
  }
}

杩欐牱鎴戜滑灏卞疄鐜颁簡鏂滅巼鍦?$[0, 1]$ 鍖洪棿鐨勯珮鏁堢畻娉曘€備篃灏辨槸璇达紝鐜板湪鎴戜滑鍙互缁樺埗 1/8 涓薄闄愮殑鐩寸嚎浜嗐€傚墿涓嬭寖鍥寸殑鐩寸嚎锛屽彲浠ラ€氳繃浜ゆ崲 xy 绛夋柟寮忓疄鐜扮粯鍒躲€傚叿浣撶殑瀹炵幇閮芥槸浜涜剰娲荤疮娲伙紝灏变笉鎽嗗嚭鏉ヤ簡锛屾劅鍏磋叮鐨勫彲浠ュ幓 GitHub 涓婄湅浠g爜鐨?a href="https://link.segmentfault.com/?url=https%3A%2F%2Fgithub.com%2Fskychx%2FtoyRenderer%2Fblob%2Fd31afbe91a140b95cef735776d13bca2dd7a5567%2Ftinyrenderer%2Fmain.cpp%23L20" rel="nofollow">瀹屾暣瀹炵幇銆?/p>


3.缁樺埗妯″瀷

杩欎竴閮ㄥ垎鍙互缁撳悎鍘熻嫳鏂囨暀绋?/a>瀛︿範锛屾垜鍙仛涓€浜涚粏鑺備笂鐨勮ˉ鍏呫€?/p>

鍓嶉潰涓や釜灏忚妭閮芥槸绠楁硶鍩虹瀛︿範锛屾湰灏忚妭寮€濮嬪姞杞戒竴涓潪娲蹭汉鐨?.obj 妯″瀷锛岀劧鍚庢妸妯″瀷涓婃瘡涓笁瑙掑舰闈㈢殑鐐硅繛鎺ヨ捣鏉ャ€?/p>

OBJ 鏂囦欢鏄竴绉嶈骞挎硾浣跨敤鐨?3D 妯″瀷鏂囦欢鏍煎紡锛坥bj 涓哄悗缂€鍚嶏級锛岀敤鏉ユ弿杩颁竴涓笁缁存ā鍨嬨€傛ā鍨嬪叧閿瓧杈冧负绻佺悙锛岄檺浜庣瘒骞呮湰鏂囨殏涓嶅睍寮€锛屽ぇ瀹跺彲浠ヨ嚜琛屾悳绱㈠涔犮€?/blockquote>

杩欎竴鑺傜殑娴佺▼涔熷緢娓呮锛氫粠纾佺洏涓婂姞杞?.obj 鏂囦欢 鈫?鎸夎鍒嗘瀽 .obj 鏂囦欢 鈫?鏋勫缓 model 鈫?寰幆 model 涓殑姣忎釜涓夎褰?鈫?杩炴帴涓夎褰㈢殑涓夋潯杈?鈫?娓叉煋鍑哄浘


涓婅瘔娴佺▼鐨勫墠涓夋宸茬粡琚師浣滆€呭皝瑁呭ソ浜嗭紝鎴戜滑鐩存帴鎶?a href="https://link.segmentfault.com/?url=https%3A%2F%2Fgithub.com%2Fskychx%2FtoyRenderer%2Ftree%2Fday02-line-drawing-algorithm%2Ftinyrenderer" rel="nofollow">婧愮爜閲岀殑 model.h 鍜? model.cpp 鎷栧埌涓诲伐绋嬮噷灏卞彲浠ヤ簡锛屾劅鍏磋叮鐨勪汉鍙互鐪嬩竴涓嬫簮鐮佸疄鐜帮紝闈炲父绠€鍗曪紝鍦ㄤ竴涓?while 寰幆閲屼竴鐩?readline 灏卞彲浠ヤ簡锛屽洜涓哄拰鍥惧舰瀛﹀叧绯讳笉澶э紝鎴戣繖閲屽氨鐣ヨ繃浜嗐€?/p>

鏈€鍚庣殑鐢讳笁瑙掑舰鐨勪唬鐮佸涓嬶紝鍏抽敭姝ラ鎴戝凡缁忕敤娉ㄩ噴鏍囨敞浜嗭細

// 瀹炰緥鍖栨ā鍨?model = new Model("obj/african_head.obj");

// 寰幆妯″瀷閲岀殑鎵€鏈変笁瑙掑舰
for (int i = 0; i < model->nfaces(); i++) {
  std::vector<int> face = model->face(i);

  // 寰幆涓夎褰笁涓《鐐癸紝姣忎袱涓《鐐硅繛涓€鏉$嚎
  for (int j = 0; j < 3; j++) {
    Vec3f v0 = model->vert(face[j]);
    Vec3f v1 = model->vert(face[(j + 1) % 3]);
    
    // 鍥犱负妯″瀷绌洪棿鍙栧€艰寖鍥存槸 [-1, 1]^3锛屾垜浠鎶婃ā鍨嬪潗鏍囧钩绉诲埌灞忓箷鍧愭爣涓?    // 涓嬮潰 (point + 1) * width(height) / 2 鐨勬搷浣滃鍚嶄负瑙嗗彛鍙樻崲锛圴iewport Transformation锛?    int x0 = (v0.x + 1.) * width / 2.;
    int y0 = (v0.y + 1.) * height / 2.;
    int x1 = (v1.x + 1.) * width / 2.;
    int y1 = (v1.y + 1.) * height / 2.;
    
    // 鐢荤嚎
    line(x0, y0, x1, y1, image, white);
  }
}

鏈€鍚庢覆鏌撳嚭鐨勫浘鍍忓涓嬶細



浠婂ぉ瀛︿範浜嗗浣曠敾涓€鏉$嚎锛屾槑澶╂垜浠涔犲浣曠敾涓€涓?strong>涓夎褰?/strong>銆?/p>


鍗氬鍘熸枃锛岄槄璇讳綋楠屾洿浣筹紙鍏紡娌℃湁閿欎贡锛?/p>

澶у瀵瑰浘褰㈠鎰熷叴瓒g殑璇濓紝鍙互鍏虫敞 馃洶锔?鍙枫€?strong>鍗よ泲瀹為獙瀹?/strong>銆嶏紝鍚庡彴鍥炲銆屽浘褰㈠銆嶈幏鍙栫粡鍏告暀鏉愩€婅檸涔?銆嬪拰銆奟eal Time Rendering 4銆?/p>

鍐欐枃涓嶆槗锛屾伋姹傚悇浣嶈浼楄€佺埛 鐐硅禐 馃憤锛屾敹钘?馃搧锛岃瘎璁?馃挰 涓夎繛鏀寔涓€涓嬶紒锛侊紒璋㈣阿浣狅紝杩欏鎴戠湡鐨勫緢閲嶈锛?/p>


鍙傝€冭繛鎺ワ細

Line Drawing on Raster Displays

The Bresenham Line-Drawing Algorithm

DDA Line Drawing Algorithm - Computer Graphics

Bresenham\'s Line Drawing Algorithm



以上是关于銆愬崄澶╄嚜鍒惰蒋娓叉煋鍣ㄣ€慏AY 02锛氱敾涓€鏉$洿绾匡紙DDA 绠楁硶 & Bresenham鈥檚 绠楁硶锛?/a>的主要内容,如果未能解决你的问题,请参考以下文章

win10瀛椾綋娓叉煋

vue娓叉煋鑷畾涔塲son鏁版嵁

鎵嬪啓Vue2.0婧愮爜锛堝洓锛?娓叉煋鏇存柊鍘熺悊

绉诲姩绔竷灞€

銆愬睆骞曞姞宸ュ厬鎹?鍩硅銆戞洸灞?iPad 鎵嬭〃 浼樺娍鍔犲伐

cesium 娴佺嚎 椋炵嚎 杩佸緳鍥?鏀婚槻鍥?/a>