Лабораторная работа 4.
Задание: Реализовать в виде процедуры «Простой алгоритм заполнения с затравкой». Приложение разработано в среде Borland C++ Builder
#include <vcl.h> #pragma hdrstop #include "Unit1.h" #pragma package(smart_init) #pragma resource "*.dfm" TForm1 *Form1; int x0=0,y0=0,start=0,xtmp,ytmp,xmet=-4,ymet=-2,metka=0;
int tx,ty,xm,xr,xl,j,c,meta; //Переменные самого алгоритма TColor kraska=clRed,bcolor=clBlue,nomy,my; struct pointt { unsigned int x; unsigned int y; };
static pointt pont[500][500]; //Матрица реализаций int raz;
cel() { Form1->PaintBox1->Canvas->Pen->Color = bcolor; Form1->PaintBox1->Canvas->Brush->Color=RGB(255,255,255); Form1->PaintBox1->Canvas->Rectangle(10,10,210,110); Form1->PaintBox1->Canvas->FloodFill(0,0,RGB(255,255,255),fsSurface); }
__fastcall TForm1::TForm1(TComponent* Owner) : TForm(Owner) { kraska=RGB(255,0,0); bcolor=RGB(0,0,255); cel(); Edit1->Text="<-- Нарисуйте гранично-заданную область -->"; }
Zakras() { xm=tx; while(Form1->PaintBox1->Canvas->Pixels[tx][ty]!=bcolor) { Form1->PaintBox1->Canvas->Pixels[tx][ty]=kraska; tx=tx+1; if (tx<=0) break; if (ty<=0) break; if (tx>420) break; if (ty>420) break; }
if(Form1->PaintBox1->Canvas->Pixels[tx][ty]==bcolor) xr=tx-1;
tx=xm; while(Form1->PaintBox1->Canvas->Pixels[tx][ty]!=bcolor) { Form1->PaintBox1->Canvas->Pixels[tx][ty]=kraska; tx=tx-1; if (tx<=0) break; if (ty<=0) break; if (tx>420) break; if (ty>420) break; }
tx=tx+1; if(Form1->PaintBox1->Canvas->Pixels[tx-1][ty]==bcolor) xl=tx;
}
Stack() {
tx=xl; ty=ty+j; while(tx<=xr) { c=0; while((Form1->PaintBox1->Canvas->Pixels[tx][ty]!=bcolor)&& (Form1->PaintBox1->Canvas->Pixels[tx][ty]!=kraska)&&(tx<xr)) {tx++;c=1;} if(c==1){ raz=raz+1; while((Form1->PaintBox1->Canvas->Pixels[tx][ty]==bcolor)|| (Form1->PaintBox1->Canvas->Pixels[tx][ty]==kraska)) tx--; pont[raz]->x=tx; pont[raz]->y=ty; } tx=tx+1; while(((Form1->PaintBox1->Canvas->Pixels[tx][ty]==bcolor)|| (Form1->PaintBox1->Canvas->Pixels[tx][ty]==kraska))&&(tx<xr)&&(tx>xl)) {tx=tx+1;} }
}
Zaliv() { raz=1; pont[raz]->x=x0; pont[raz]->y=y0; while(raz>0) { tx=pont[raz]->x; ty=pont[raz]->y; raz=raz-1; Form1->PaintBox1->Canvas->Pixels[tx][ty]=kraska; Zakras(); j=1; Stack(); j=-2; Stack(); } Form1->Edit1->Text="Все закончилось"; }
void __fastcall TForm1::drawing(TObject *Sender, TMouseButton Button, TShiftState Shift, int X, int Y) { if(start==5) { x0=X; y0=Y; Canvas->Pixels[X][Y]=kraska; Zaliv(); }
if((Button==mbLeft)&&(start!=5)) { Canvas->Pen->Color = bcolor; // выбрать цвет контура
if(metka==1) Canvas->LineTo(X,Y); metka=1;
xtmp=X; ytmp=Y; Canvas->MoveTo(X,Y); if(start==0) {x0=X,y0=Y;start=1;} }
if (Button==mbRight) { Canvas->Pen->Color = bcolor; Canvas->LineTo(x0,y0); metka=0; start=0; } }
void __fastcall TForm1::movexy(TObject *Sender, TShiftState Shift, int X, int Y) { Label2->Caption=X; Label4->Caption=Y; }
void __fastcall TForm1::reset_key(TObject *Sender) { start=0;
PaintBox1->Visible=false; PaintBox1->Visible=true;
start=0;
Edit1->Text="<-- Нарисуйте гранично-заданную область -->";
}
void __fastcall TForm1::vpered(TObject *Sender) { Edit1->Text=" Выберите точку закраски"; start=5; }
Содержимое Unit.h
#ifndef Unit1H #define Unit1H #include <Classes.hpp> #include <Controls.hpp> #include <StdCtrls.hpp> #include <Forms.hpp> #include <ExtCtrls.hpp> class TForm1: public TForm { __published: // IDE-managed Components TEdit *Edit1; TLabel *Label2; TLabel *Label4; TButton *Button1; TPaintBox *PaintBox1; TLabel *Label1; TLabel *Label3; void __fastcall drawing(TObject *Sender, TMouseButton Button, TShiftState Shift, int X, int Y); void __fastcall movexy(TObject *Sender, TShiftState Shift, int X, int Y); void __fastcall reset_key(TObject *Sender); void __fastcall vpered(TObject *Sender);
private: // User declarations public: // User declarations __fastcall TForm1(TComponent* Owner); }; extern PACKAGE TForm1 *Form1; #endif
|