AKB Forums

Go Back   AKB Forums > Technical sections > Algorithms
Home Register Blogs FAQ Members List Calendar Downloads Arcade Mark Forums Read

Algorithms The source of algorithms for your project

Troubles when posting message? Click here! :: Проблемы с отправлением сообщения? Нажмите сюда!

Reply
 
LinkBack Thread Tools Display Modes
Old Jan 21, 2002, 23:29   #1
»
 
z0mbie's Avatar
 
Join Date: Jan 2002
Posts: 776
Rep Power: 7
Reputation: 10
Send a message via ICQ to z0mbie
Post zalivka [Fill Tool v graph. editor-ax]

я тут написал, работает, но наверняка есть алгоритм лучше, может кто знает ?

void Fill(int array[MAX_X][MAX_Y],int x,int y,int color)<BR>{<BR>if(x<0||y<0||x>MAX_X||y>MAX_Y) return;<BR>if(array[x][y]==color) return;

array[x][y]=c;

Fill(array,x+1,y,c);<BR>Fill(array,x-1,y,c);<BR>Fill(array,x,y+1,c);<BR>Fill(array,x,y-1,c);<BR>}
z0mbie is offline   Reply With Quote Quote selected
Old Jan 22, 2002, 00:24   #2
Administrator
 
acid's Avatar
 
Join Date: Sep 2001
Location: Yerevan, Armenia
Posts: 7,069
Blog Entries: 15
Rep Power: 10
Reputation: 246
Post

Заливка замкнутой области<BR>Пример показывает один из самых простых и эффективных способов заливки. Использовать его в таком варианте не рекомендую, рисовать на TImage->Canvas довольно медленно, но в качестве примера очень удобно.

Процедура последовательно просматривает соседние точки, и если они не залиты, то заливает и добавляет 4 соседние точки в очередь, на рассмотрение. И так пока очередь не кончится.

На рисунке черными стрелками показаны точки, которые будут добавлены в очередь для просмотра, а красными, которые не будут добавлены, так как уже залиты.

<BR> <BR> <BR> <BR>Теперь собственно - исходник. (Borland C++ Builder). На форме достаточно иметь TImage - Image1.

<BLOCKQUOTE><font size="1" face="MS Sans Serif, Verdana, Helvetica, sans-serif">code:</font><HR><pre><BR>void ImageFill(short x, short y, TColor color) {<BR> // Получим размеры изображения<BR> int imw=Form1->Image1->Width;<BR> int imh=Form1->Image1->Height;<BR> // Выделим памяти для складирования координат, которые еще предстоит залить<BR> int STACK_SIZE=(imw+2)*(imh+2);<BR> short *floodfill_stackx=(short*)malloc(STACK_SIZE*sizeof (short));<BR> short *floodfill_stacky=(short*)malloc(STACK_SIZE*sizeof (short));<BR> if (floodfill_stacky==NULL || floodfill_stackx==NULL) return;<BR> int stack_head=0;<BR> int stack_tail=0;<BR> TColor clr=Form1->Image1->Canvas->Pixels[x][y];<BR> floodfill_stackx[stack_head]=x;<BR> floodfill_stacky[stack_head]=y;<BR> Form1->Image1->Canvas->Pixels[x][y]=color;<BR> stack_head++;<BR> // Пока не кончится память или точки на изображении<BR> while (stack_head<STACK_SIZE && stack_head>stack_tail) {<BR> x=floodfill_stackx[stack_tail];<BR> y=floodfill_stacky[stack_tail];<BR> stack_tail++;<BR> if (x>=0 && y>=0 && x<imw && y<imh) {<BR> // Проверим точку справа, если она не залита, то зальем<BR> if (Form1->Image1->Canvas->Pixels[x+1][y]==clr) {<BR> floodfill_stackx[stack_head]=x+1;<BR> floodfill_stacky[stack_head]=y;<BR> Form1->Image1->Canvas->Pixels[x+1][y]=color;<BR> stack_head++;<BR> }<BR> // Проверим точку слева, если она не залита, то зальем<BR> if (Form1->Image1->Canvas->Pixels[x-1][y]==clr) {<BR> floodfill_stackx[stack_head]=x-1;<BR> floodfill_stacky[stack_head]=y;<BR> Form1->Image1->Canvas->Pixels[x-1][y]=color;<BR> stack_head++;<BR> }<BR> // Проверим точку снизу, если она не залита, то зальем<BR> if (Form1->Image1->Canvas->Pixels[x][y+1]==clr) {<BR> floodfill_stackx[stack_head]=x;<BR> floodfill_stacky[stack_head]=y+1;<BR> Form1->Image1->Canvas->Pixels[x][y+1]=color;<BR> stack_head++;<BR> }<BR> // Проверим точку сверху, если она не залита, то зальем<BR> if (Form1->Image1->Canvas->Pixels[x][y-1]==clr) {<BR> floodfill_stackx[stack_head]=x;<BR> floodfill_stacky[stack_head]=y-1;<BR> Form1->Image1->Canvas->Pixels[x][y-1]=color;<BR> stack_head++;<BR> }<BR> }<BR> }<BR> Form1->Image1->Canvas->TextOutA(1,1,IntToStr(stack_head));<BR> // Освободим память<BR> free(floodfill_stacky);<BR> free(floodfill_stackx);<BR> }

void __fastcall TForm1::Button1Click(TObject *Sender) {<BR> Image1->Canvas->Ellipse(20,20,300,200);<BR> ImageFill(1,1,0xFF0000);<BR> }<BR></pre><HR></BLOCKQUOTE>
__________________
Chat with acid


acid is offline   Reply With Quote Quote selected
Old Jan 24, 2002, 04:08   #3
Дошкольник
 
Dark Abyss of Yerevan's Avatar
 
Join Date: Jan 2002
Location: hell
Posts: 124
Rep Power: 7
Reputation: 10
Send a message via ICQ to Dark Abyss of Yerevan
Post

2Zomb:<BR>Hey man, ya i ne znal shto ti uje pereshel na<BR>'pure' programming <IMG SRC="wink.gif" border="0"> tak derzhat'.<BR>Nashet algoritma. IMHO algoritm ne effectiven.<BR>Rekursiyu ispol`zovat' v dannom sluchaye ne imeyet smisla. Sovershenno neponyatno zachem vse vremya nujno peredavat' `array`.

Vtoroy algoritm v sushnosti to je samoe, no rekursiya ne ispolzuetsya.<BR>Instead, ispolzuetsya ochered'. Oba algoritma<BR>iz sebya predstavlyayut chastniy sluchay algoritma proxozhdeniya 4arnogo dereva. <BR>Algoritm zomba - pryamoy poryadok proxozhdeniya (recursive).

Ispol`zovanie ocheredi(stacka) est' ne shto inoye kak implementaciya rekursii na bolee nizkom urovne. Ved' esli vspomnit', pri vizove funkcii vsya infa zapixivaetsya ne kuda to a imenno v stack.

<BR>2Acid:<BR> Slushay, ti eto sam napisal? esli net skaji<BR>pojaluysta otkuda u tebya eti doci.
__________________
[x]-=-[ ]-=-[x]
Dark Abyss of Yerevan is offline   Reply With Quote Quote selected
Old Jan 24, 2002, 15:39   #4
Administrator
 
acid's Avatar
 
Join Date: Sep 2001
Location: Yerevan, Armenia
Posts: 7,069
Blog Entries: 15
Rep Power: 10
Reputation: 246
Post

2DAY

Ne-a, ja ne pishu na Borland-e.<BR>A istochnik vot zdes':<BR><A HREF="http://www.codenet.ru" TARGET=_blank>Istochnik</A>
__________________
Chat with acid


acid is offline   Reply With Quote Quote selected
Old Jan 25, 2002, 01:14   #5
Дошкольник
 
Dark Abyss of Yerevan's Avatar
 
Join Date: Jan 2002
Location: hell
Posts: 124
Rep Power: 7
Reputation: 10
Send a message via ICQ to Dark Abyss of Yerevan
Post

thanks
__________________
[x]-=-[ ]-=-[x]
Dark Abyss of Yerevan is offline   Reply With Quote Quote selected
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



All times are GMT. The time now is 01:25.


Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
This board was founded on September 29, 2001
Powered by Viper Internet

Affordable Web Hosting | ParevNet

Buy text link