Добавил:
Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:

LAB3. / Unit1

.cpp
Скачиваний:
4
Добавлен:
01.02.2019
Размер:
4.37 Кб
Скачать
//---------------------------------------------------------------------------

#include <vcl.h>
#pragma hdrstop

#include "Unit1.h"
#include <vector>
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"
TForm1 *Form1;
//---------------------------------------------------------------------------
__fastcall TForm1::TForm1(TComponent* Owner)
	: TForm(Owner)
{
	StartFill = false;
}

void __fastcall TForm1::PaintBox1Paint(TObject *Sender)
{
	PaintBox1->Canvas->Brush->Color = clWhite;
	PaintBox1->Canvas->Rectangle(0, 0, PaintBox1->Width, PaintBox1->Height);
	PaintBox1->Canvas->Pen->Width = 2;
	f.Draw();
}

void TForm1::createFigure() {

}

void __fastcall TForm1::PaintBox1MouseMove(TObject *Sender, TShiftState Shift, int X,
		  int Y)
{
	if(!f.IsCreated() && f.startCreate) {
		if(!f.firstPoint) {
			PaintBox1->Repaint();
			Point2D* tmp = new Point2D(X, Y);
			tmp->Draw();

		} else {
			PaintBox1->Repaint();
			Form1->PaintBox1->Canvas->MoveTo(f.lastPoint->x, f.lastPoint->y);
			Form1->PaintBox1->Canvas->LineTo(X, Y);
		}
	}

}

void __fastcall TForm1::PaintBox1MouseUp(TObject *Sender, TMouseButton Button, TShiftState Shift,
		  int X, int Y)
{
	if(!f.IsCreated() && f.startCreate) {
		if(f.firstPoint) {
			if(f.firstPoint->x - 6 < X &&
				f.firstPoint->x + 6 > X &&
				f.firstPoint->y - 6 < Y &&
				f.firstPoint->y + 6 > Y) {
				f.created = true;
				Point2D* tmp = new Point2D(f.firstPoint->x, f.firstPoint->y);
				f.AddPoint(tmp);
				PaintBox1->Repaint();
				return;
			}
		}
		Point2D* tmp = new Point2D(X, Y);
		f.AddPoint(tmp);
		PaintBox1->Repaint();
	}
	if(StartFill) {
		Fil(X, Y);
    }
}

void TForm1::Fil(int __x, int __y) {
	Point2D P(__x, __y);
	std::vector<Point2D> points;
	points.push_back(P);
	this->DoubleBuffered = true;
	int minX, maxX;
	while(!points.empty()) {
		P = points.back();
		points.pop_back();
		Application->ProcessMessages();
		Form1->PaintBox1->Canvas->Pixels[P.x][P.y] = clBlack;
		minX = P.x-1;
		while(Form1->PaintBox1->Canvas->Pixels[minX][P.y] == clWhite) {
			minX--;
		}
		maxX = P.x+1;
		while(Form1->PaintBox1->Canvas->Pixels[maxX][P.y] == clWhite) {
			maxX++;
		}
		for(int i = P.x; i > minX; i--) {
			Form1->PaintBox1->Canvas->Pixels[i][P.y] = clBlack;
		}
		for(int i = P.x; i < maxX; i++) {
			Form1->PaintBox1->Canvas->Pixels[i][P.y] = clBlack;
		}
		bool flag = 1;
		for(int i = minX; i <= maxX; i++) {
			if(Form1->PaintBox1->Canvas->Pixels[i][P.y-1] != clBlack) {
				if(flag) {
					points.push_back(Point2D(i, P.y-1));
					flag = 0;
				}
			} else {
				flag = 1;
			}
		}
		flag = 1;
		for(int i = minX; i <= maxX; i++) {
			if(Form1->PaintBox1->Canvas->Pixels[i][P.y+1] != clBlack) {
				if(flag) {
					points.push_back(Point2D(i, P.y+1));
					flag = 0;
				}
			} else {
				flag = 1;
			}
		}
	}
	StartFill = false;
}
//---------------------------------------------------------------------------
// Figure
bool Figure::IsCreated() {
	return created;
}
void Figure::AddPoint(Point2D* __p) {
	Point2D* newPoint = __p;
	if(!firstPoint) {
		firstPoint = lastPoint = newPoint;
		return;
	}
	lastPoint->next = newPoint;
	lastPoint = newPoint;
}
void Figure::Clear() {
	firstPoint = lastPoint = NULL;
}
void Figure::Draw() {
	for(Point2D* currentPoint = firstPoint; currentPoint != lastPoint; currentPoint = currentPoint->next) {
		currentPoint->Draw();
		Form1->PaintBox1->Canvas->MoveTo(currentPoint->x, currentPoint->y);
		if(currentPoint == lastPoint) {
			Form1->PaintBox1->Canvas->LineTo(firstPoint->x, firstPoint->y);
		} else {
			Form1->PaintBox1->Canvas->LineTo(currentPoint->next->x, currentPoint->next->y);
		}
	}
}
// Point
void Point2D::Draw() {
	Form1->PaintBox1->Canvas->Pen->Color = clBlack;
	Form1->PaintBox1->Canvas->Brush->Color = clWhite;
	Form1->PaintBox1->Canvas->Ellipse(x - 1, y - 1, x + 1, y + 1);
}


void __fastcall TForm1::Button1Click(TObject *Sender)
{
	f.startCreate = true;
}
//---------------------------------------------------------------------------
void __fastcall TForm1::FillClick(TObject *Sender)
{
	StartFill = true;
}
//---------------------------------------------------------------------------

Соседние файлы в папке LAB3.