日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

计算机图形学实验一直线-DDA算法

發布時間:2023/12/9 编程问答 39 豆豆
生活随笔 收集整理的這篇文章主要介紹了 计算机图形学实验一直线-DDA算法 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

這里寫自定義目錄標題

    • 一、基本知識和實驗步驟
    • 二、代碼展示

一、基本知識和實驗步驟

  • DDA(數值微分)算法
    DDA 算法原理:如圖 1-1 所示,已知過端點怕p0(x0,y0),p1(x1,y1)的直線段 p0,p1;直線斜率為 k=(y1-y0)/(x1-x0) ,從x 的左端點x0開始,向 x 右端 點步進畫線,步長=1(個像素),計算相應的 y 坐標y=kx+B;取像素點 [x , round(y) ] 作為當前點的 坐標。計算yi+1=kxi+1+B=kx1+B+kdx, 當x=1 ,yi+1*=yi+k,即當 x 每遞增 1,y 遞增 k(即 直線斜率)。 注意:上述分析的算法僅適用于 k<=1 的情形。在 這種情況下,x 每增加 1, y 最多增加 1。當x>=1 時, 必須把 x,y 地位互換,y 每增加 1,x 相應增加 1/k。
  • 二、代碼展示

  • GPoint.h 文件中的代碼
  • #pragma once ref class GPoint { public: int x; int y; GPoint(void); GPoint(int, int); };
  • GPoint.cpp文件中的代碼
  • #include "StdAfx.h" #include "GPoint.h" GPoint::GPoint(void) { x=0; y=0; } GPoint::GPoint(int x0, int y0) { x = x0; y = y0; }
  • Line.h 文件中的代碼
  • #pragma once using namespace System::Drawing; #include "GPoint.h" ref class Line { public: GPoint Begin; GPoint End; Line(void); Line(GPoint^, GPoint^); void DDA(Color,Bitmap ^); void Bresenham1(Color,Bitmap ^); void Bresenham2(Color,Bitmap ^); void Bresenham3(Color,Bitmap ^); void MidPointLine(Color,Bitmap ^); };
  • Line.cpp文件中的代碼
  • #include "GPoint.h" #include "math.h" Line::Line() { GPoint Begin; GPoint End; Begin.x =0; Begin.y =0; End.x=0; End.y=0; } Line::Line(GPoint ^P0, GPoint ^P1) { Begin.x =P0->x; Begin.y =P0->y; End.x=P1->x; End.y=P1->y; }
  • DDA算法代碼
  • void Line::DDA(Color color,Bitmap ^bmp) { int dx = abs(End.x - Begin.x); int dy = abs(End.y - Begin.y); int s1, s2; if (End.x >= Begin.x) s1 = 1; else s1 = -1; if (End.y >= Begin.y) s2 = 1; else s2 = -1; float x =(float) Begin.x; float y =(float) Begin.y; float k = (float)dy / dx; if (k <= 1) { for (int i = 0; i <= dx; i++) { x += s1; bmp->SetPixel((int) x, (int)( y+0.5), color); y += s2 * k; } } else { for (int i = 0; i <= dy; i++) { y += s2; bmp->SetPixel((int)(x+0.5), (int) y, color); x += s1 *1/ k; } } }

    侵刪

    `

    總結

    以上是生活随笔為你收集整理的计算机图形学实验一直线-DDA算法的全部內容,希望文章能夠幫你解決所遇到的問題。

    如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。