DDA(數字微分分析儀)繪製線條,dda分析儀

來源:互聯網
上載者:User

DDA(數字微分分析儀)繪製線條,dda分析儀

DDA(digital differential analyzer,數字微分分析法)是一種線段掃描轉換演算法。它的主要演算法原理是:

在一個座標軸上以單位間隔對線段取樣,從而在另一座標軸上確定最靠近線條路徑的對應座標值。

下面是DDA演算法的C++代碼(裡面的Window、Application等對象,是我自己用OpenGL封裝的類庫,僅僅是為了簡便與學習,可以直接使用OpenGL代替)

#include <iostream>#include <conio.h>using namespace std;#include "Application.h"int handle(const float a) {return int(a+0.5);}void drawPoint(Window& window, int x, int y){Point *point = new Point(x, y,Color(255,255,255));window.add((Object*)point);}/*window是用於繪圖的視窗(x0,y0)是起始座標,(x1,y1)是終點座標*/void lineDDA(Window& window, int x0, int y0, int x1, int y1) {int dx = x1 - x0, dy = y1 - y0;//dx為兩點橫座標之差,dy為兩點縱座標之差float xIncrement, yIncrement;//float x = x0, y = y0;//將座標的類型轉換為浮點類型int length = 0;//記錄選定座標軸對應邊的長度(dx或dy)//判斷斜率大小if (fabs(float(dx)) > fabs(float(dy))){ //dx>dy,即斜率小於1時xIncrement = 1;//以X軸為單位間隔yIncrement = float(dy) / float(dx); //每在X軸遞增1個單位,Y軸的遞增量大小length = dx;//記錄此時的選定單位間隔的座標軸的線條X軸對應邊長度}else{yIncrement = 1;//同上xIncrement = float(dx) / float(dx);length = dy;}//繪製出起始點,handle函數浮點數轉換為整數(同時會累積誤差)drawPoint(window, handle(x), handle(y));for (int i = 0; i < length; i++){//依次給座標以給定量遞增x += xIncrement;y += yIncrement;//繪製點drawPoint(window, handle(x), handle(y));}}int main(int argc, char *argv[]){Window window("Hello",100,100,400,300);window.create();//使用演算法lineDDA(window, 10, 10, 250, 250);Application app;app.init(argc, argv);app.add(window);app.show();_getch();return 0;}//*/


聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.