其實就是bfs或者dfs都可以
#include
#include
#include
#include
#include
using
namespace
std;
float wid = 400; //設定視窗的大小,約定視窗必須為正方形
float height = wid; //設定視窗的大小
int numbers = 20; //設定劃分的網格的個數
float t = wid/numbers; //模擬畫素下的單位1
int con [41][41];
/* 引數設定說明:
輸入直線的兩點a(x1,y1);b(x2,y2)
您應當確保引數範圍在-400~400.且為20的倍數。
這是因為我們的單位步長為20.
*支援不同斜率
*支援兩點位置顛倒
*/struct v ;
int x1 = 120,y1=80,x2 =300,y2 = 200;
int xx1 = 20,yy1=180,xx2 =300,yy2 = 200;
int ***1 = 20,yyy1=180,***2 =120,yyy2 = 80;
void draw_point(float x, float y,int k_kind,int d_kind);
float translater(int x);
void swap(int &a, int &b)
void bresenham(int x1, int y1,int x2, int y2)
int dx = abs(x2-x1), dy = abs(y2-y1);
if (y1 > y2)
if (dy > dx)
float d = (dy +dy -dx)*t; //令d為決策量(這裡利用d = dx*w*2避免浮點運算)
float x = x1+0.0,y = y1+0.0;
draw_point(translater(x),translater(y),k_kind,d_kind); //繪製下乙個點
while( x < x2)
else
x= x + t;
cout
<< "saa "
<< (int)x/20
<<"-- "
<<(int)y/20
}}float translater(int x)
void bfs_filling()
cout
<< temp.x/20 - 1
<<" "
<< temp.y/20
<< con[temp.x/20-1][temp.y/20]=0&&con[temp.x/20-1][temp.y/20]==0)
if (con[temp.x/20][temp.y/20+1]==0)
if (temp.y/20-1 >=0 &&con[temp.x/20][temp.y/20-1]==0)
}}}void draw_point(float x , float y, int k_kind,int d_kind)else
if (k_kind ==1 &&d_kind==1)else
if (k_kind==1&&d_kind ==0)
int xx = x*400/20;
int yy = y*400/20;
con[xx][yy]=1;
glvertex3f(x,y,0.0);
glend();
glflush();
}void draw_line(int x1, int y1 ,int x2 , int y2)
void grid()
glcolor3f(1.0,0,0);
glbegin(gl_lines); //繪製座標系,便於觀察
glvertex2f(-1,0);
glvertex2f(1,0);
glvertex2f(0,-1);
glvertex2f(0,1);
glend();
glflush();
draw_line(x1, y1, x2, y2);
draw_line(xx1, yy1, xx2, yy2);
draw_line(***1, yyy1, ***2, yyy2);
//重新整理緩衝,保證繪圖命令能被執行
bresenham(x1, y1,x2,y2);
bresenham(xx1, yy1,xx2,yy2);
bresenham(***1, yyy1,***2,yyy2);
bfs_filling();
}int main(int argc, char *argv)
}//初始化glut library
glutinit(&argc, argv);
//對視窗的大小進行初始化
glutinitwindowsize(700,700);
glutinitwindowposition(300,200);
// 設定視窗出現的位置
//glutinitwindowposition(int x, int y);
glutinitdisplaymode(glut_rgba);
glutcreatewindow("class16_hw1");
glutdisplayfunc(&grid);
glutmainloop();
return
0;}
OpenGL實現邊緣填充演算法
邊緣填充的思想 列舉每一條邊,對這條邊右邊的畫素進行求補操作,就是有填充則去掉,無填充就進行填充 include include include include include inc程式設計客棧lude using namespace std int x int y int vis 500 500...
C STL填充演算法
include include include include include include include include include using namespace std template void printvector vectorv vectorvarr arr,arr 9 pri...
邊緣填充演算法
邊緣填充演算法是先求出多邊形的每條邊與掃瞄線的交點,然後將交點右側的所有畫素顏色全部取為補色 或反色 按任意順序處理完多邊形的所有邊後,就完成了多邊形的填充任務。邊緣填充演算法利用了影象處理中的求 補 或求 反 的概念,對於黑白影象,求補就是把rgb 1,1,1 白色 的畫素置為rgb 0,0,0 ...