2012-04-08 68 views
8

我的電腦圖形功課畫線,是落實只用畫點的能力OpenGL的算法。與Bresenham直線算法

所以,很顯然,我需要得到drawLine()上班之前,我可以畫任何東西。 drawLine()只能使用整數來完成。沒有浮點。

這是教我的東西。基本上,線可以分爲4個不同的類別,積極陡峭,積極淺,負陡和負淺。這是我應該畫出來:

expected result

而且這是圖片我的程序繪製:

actual result

的顏色爲我們做。我們給了頂點,我們需要使用Bresenham的線算法來繪製基於起點和終點的線。

這是我到目前爲止有:

int dx = end.x - start.x; 
int dy = end.y - start.y; 

//initialize varibales 
int d; 
int dL; 
int dU; 

if (dy > 0){ 
     if (dy > dx){ 
       //+steep 
       d = dy - 2*dx; 
       dL = -2*dx; 
       dU = 2*dy - 2*dx; 

       for (int x = start.x, y = start.y; y <= end.y; y++){ 
         Vertex v(x,y); 
         drawPoint(v); 

         if (d >= 1){ 
           d += dL; 
         }else{ 
           x++; 
           d += dU; 
         } 
       }    
     } else { 
       //+shallow 
       d = 2*dy - dx; 
       dL = 2*dy; 
       dU = 2*dy - 2*dx; 

       for (int x = start.x, y = start.y; x <= end.x; x++) { 
         Vertex v(x,y); 
         drawPoint(v); 

         // if choosing L, next y will stay the same, we only need 
         // to update d by dL 
         if (d <= 0) { 
           d += dL; 
         // otherwise choose U, y moves up 1 
         } else { 
           y++; 
           d += dU; 
         } 
       } 
     } 
} else { 
     if (-dy > dx){ 
       cout << "-steep\n"; 
       //-steep 
       d = dy - 2*dx; 
       //south 
       dL = 2*dx; 
       //southeast 
       dU = 2*dy - 2*dx; 

       for (int x = start.x, y = start.y; y >= end.y; --y){ 
         Vertex v(x,y); 
         drawPoint(v); 

         //if choosing L, next x will stay the same, we only need 
         //to update d 
         if (d >= 1){ 
           d -= dL; 
         } else { 
           x++; 
           d -= dU; 
         } 
       } 

     } else { 
       cout << "-shallow\n"; 
       //-shallow 
       d = 2*dy - dx; 
       dL = 2*dy; 
       dU = 2*dy - 2*dx; 

       for (int x = start.x, y = start.y; x <= end.x; x++){ 
         Vertex v(x,y); 
         drawPoint(v); 

         if (d >= 0){ 
           d += dL; 
         } else { 
           --y; 
           d -= dU; 
         } 
       } 
     } 
} 

我知道我的錯誤將是愚蠢的事,但老實說,我想不出什麼我做錯了。爲什麼有些線條如上所示不正確?

+0

什麼是不工作? – 2012-04-08 03:03:21

+0

如果你看一下上面我的代碼段的兩張圖片中,「第二象限」和「四象限」不提款權。我將這些引號放在引號中,因爲point(0,0)位於圖片的左下角。基本上,第一個else語句包含不正常工作的代碼 - 淺 - 和。 – ToastyMallows 2012-04-08 03:10:44

+0

在2個簡單的例子中運行你的代碼,一個可行,一個不行。說(0,0),(5,5)和(0,5),(5,0),跟蹤兩者,看他們是否有分歧。這將是一個更好的練習,而不是讓某個人爲你做這件事。 – 2012-04-08 03:24:33

回答

0

如果有人想知道問題是什麼,我仍然不知道那是什麼。我最終做的是將我的代碼重新分解,以使「淺」和「四步」分別使用相同的算法,分別爲+淺和+陡。調整x,y座標(否定x或y座標)後,當我去繪製它們時,我否定了原來的否定,從而將它繪製在正確的位置上。

+0

我不熟悉C語言,但也許有一些fraktional值已被轉換成絕對值? – 2014-04-25 10:46:33

5

你可以在C++繪製使用Bresenham算法在http://www.etechplanet.com/codesnippets/computer-graphics-draw-a-line-using-bresenham-algorithm.aspx線的完整代碼:

/*BRESENHAAM ALGORITHM FOR LINE DRAWING*/ 
#include<iostream.h> 
#include<graphics.h> 
#include<stdio.h> 
#include<conio.h> 
#include<stdlib.h> 
#include<math.h> 
#include<dos.h> 
void bhm_line(int,int,int,int,int); 
void main() 
{ 
int ghdriver=DETECT,ghmode,errorcode,x1,x2,y1,y2; 
initgraph(&ghdriver,&ghmode,"..\\bgi"); 
errorcode = graphresult(); 
if(errorcode !=grOk) 
{ 
    cout<<"Graphics error:%s\n"<<grapherrormsg(errorcode); 
    cout<<"Press any key to halt:"; 
    getch(); 
    exit(1); 
} 
clrscr(); 
cout<<"Enter the coordinates (x1,y1): "; 
cin>>x1>>y1; 
cout<<"Enter the coordinates (x2,y2): "; 
cin>>x2>>y2; 
bhm_line(x1,y1,x2,y2,1); 
getch(); 
} 
void bhm_line(int x1,int y1,int x2,int y2,int c) 
{ 
int x,y,dx,dy,dx1,dy1,px,py,xe,ye,i; 
dx=x2-x1; 
dy=y2-y1; 
dx1=fabs(dx); 
dy1=fabs(dy); 
px=2*dy1-dx1; 
py=2*dx1-dy1; 
if(dy1<=dx1) 
{ 
    if(dx>=0) 
    { 
    x=x1; 
    y=y1; 
    xe=x2; 
    } 
    else 
    { 
    x=x2; 
    y=y2; 
    xe=x1; 
    } 
    putpixel(x,y,c); 
    for(i=0;x<xe;i++) 
    { 
    x=x+1; 
    if(px<0) 
    { 
    px=px+2*dy1; 
    } 
    else 
    { 
    if((dx<0 && dy<0) || (dx>0 && dy>0)) 
    { 
    y=y+1; 
    } 
    else 
    { 
    y=y-1; 
    } 
    px=px+2*(dy1-dx1); 
    } 
    delay(0); 
    putpixel(x,y,c); 
    } 
} 
else 
{ 
    if(dy>=0) 
    { 
    x=x1; 
    y=y1; 
    ye=y2; 
    } 
    else 
    { 
    x=x2; 
    y=y2; 
    ye=y1; 
    } 
    putpixel(x,y,c); 
    for(i=0;y<ye;i++) 
    { 
    y=y+1; 
    if(py<=0) 
    { 
    py=py+2*dx1; 
    } 
    else 
    { 
    if((dx<0 && dy<0) || (dx>0 && dy>0)) 
    { 
    x=x+1; 
    } 
    else 
    { 
    x=x-1; 
    } 
    py=py+2*(dx1-dy1); 
    } 
    delay(0); 
    putpixel(x,y,c); 
    } 
} 
} 
+3

這是一個僅鏈接的答案;請編輯您的答案以包含實際的代碼,而不僅僅是鏈接。 – LittleBobbyTables 2013-05-06 18:55:49

+0

bhm_line()中的「delay(0)」是什麼? – 2017-05-17 17:10:12