Добавил:
farel
Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз:
Предмет:
Файл:LAB3 / Unit1
.cpp//---------------------------------------------------------------------------
#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;
currentColor = clRed;
}
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) {
switch(ComboBox1->ItemIndex) {
case 0: currentColor = clRed; break;
case 1: currentColor = clBlue; break;
case 2: currentColor = clGreen; break;
case 3: currentColor = clYellow; break;
}
TPoint P(__x, __y);
std::vector<TPoint> points;
points.push_back(P);
while(!points.empty()) {
P = points.back();
points.pop_back();
Application->ProcessMessages();
Form1->PaintBox1->Canvas->Pixels[P.x][P.y] = currentColor;
if(Form1->PaintBox1->Canvas->Pixels[P.x-1][P.y] != currentColor &&
Form1->PaintBox1->Canvas->Pixels[P.x-1][P.y] != clBlack) {
points.push_back(TPoint(P.x-1, P.y));
}
if(Form1->PaintBox1->Canvas->Pixels[P.x+1][P.y] != currentColor &&
Form1->PaintBox1->Canvas->Pixels[P.x+1][P.y] != clBlack) {
points.push_back(TPoint(P.x+1, P.y));
}
if(Form1->PaintBox1->Canvas->Pixels[P.x][P.y-1] != currentColor &&
Form1->PaintBox1->Canvas->Pixels[P.x][P.y-1] != clBlack) {
points.push_back(TPoint(P.x, P.y-1));
}
if(Form1->PaintBox1->Canvas->Pixels[P.x][P.y+1] != currentColor &&
Form1->PaintBox1->Canvas->Pixels[P.x][P.y+1] != clBlack) {
points.push_back(TPoint(P.x, P.y+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;
if(!Form1->f.IsCreated()) {
Form1->PaintBox1->Canvas->Ellipse(x - 6, y - 6, x + 6, y + 6);
} else {
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;
}
//---------------------------------------------------------------------------