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

ООП / C++ / Lab3C / ex3 / Unit1

.cpp
Скачиваний:
65
Добавлен:
18.02.2017
Размер:
3.21 Кб
Скачать
//---------------------------------------------------------------------------
#include <vcl.h>
#pragma hdrstop
#include "Unit1.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"
TForm1 *Form1;
//---------------------------------------------------------------------------
__fastcall TForm1::TForm1(TComponent* Owner) : TForm(Owner) {
Capture = -1;
MouseDown = false;
p[0] = new CBall(Image1->Width * 0.25, Image1->Height * 0.75);
p[1] = new CBall(Image1->Width * 0.5, Image1->Height * 0.25);
p[2] = new CBall(Image1->Width * 0.75, Image1->Height * 0.5);
this->Cls(); }
//---------------------------------------------------------------------------
void __fastcall TForm1::Cls() {
Image1->Canvas->Brush->Color = clWhite;
Image1->Canvas->FillRect(Rect(0, 0, Image1->Width, Image1->Height)); }
//---------------------------------------------------------------------------
void __fastcall TForm1::Timer1Timer(TObject *Sender) {
this->Cls();
this->Draw();
for (int i = 0; i < 3; i++)
	p[i]->Draw(); }
//---------------------------------------------------------------------------
void __fastcall TForm1::Draw() {
Form1->Image1->Canvas->Pen->Width = 3;
Form1->Image1->Canvas->Pen->Color = clRed;
Form1->Image1->Canvas->Brush->Color = clRed;
int x0, y0;
for (double i = 0; i < 1; i += 0.01) {
	double x = (1-i)*(1-i)*p[0]->X + p[1]->X * (1 - i) * i * 2 + i*i*p[2]->X;
	double y = (1-i)*(1-i)*p[0]->Y + p[1]->Y * (1 - i) * i * 2 + i*i*p[2]->Y;
	if (i == 0) {
		x0 = x;
		y0 = y; };
	Form1->Image1->Canvas->MoveTo(x0, y0);
	Form1->Image1->Canvas->LineTo(x, y);
	x0 = x;
	y0 = y; };
Form1->Image1->Canvas->Pen->Width = 1;
Form1->Image1->Canvas->Pen->Color = clBlack;
Form1->Image1->Canvas->MoveTo(p[0]->X, p[0]->Y);
Form1->Image1->Canvas->LineTo(p[1]->X, p[1]->Y);
Form1->Image1->Canvas->LineTo(p[2]->X, p[2]->Y); }
void __fastcall TForm1::Image1MouseDown(TObject *Sender, TMouseButton Button, TShiftState Shift, int X, int Y) {
MouseDown = true;
Capture = -1;
for (int i = 0; i < 3; i++)
	if (sqrt((X - p[i]->X) * (X - p[i]->X) + (Y - p[i]->Y) * (Y-p[i]->Y)) <= p[i]->R) {
		Capture = i;
		dx = p[Capture]->X - X;
		dy = p[Capture]->Y - Y; }; }
//---------------------------------------------------------------------------
void __fastcall TForm1::Image1MouseMove(TObject *Sender, TShiftState Shift, int X, int Y) {
if ((MouseDown) && (Capture != -1)) {
	p[Capture]->SetX(dx + X);
	p[Capture]->SetY(dy + Y); }; }
//---------------------------------------------------------------------------
void __fastcall TForm1::Image1MouseUp(TObject *Sender, TMouseButton Button, TShiftState Shift, int X, int Y) {
MouseDown = false; }
//---------------------------------------------------------------------------
void __fastcall TForm1::Image1DblClick(TObject *Sender) {
Capture = -1;
MouseDown = false;
p[0] = new CBall(Image1->Width * 0.25, Image1->Height * 0.75);
p[1] = new CBall(Image1->Width * 0.5, Image1->Height * 0.25);
p[2] = new CBall(Image1->Width * 0.75, Image1->Height * 0.5);
this->Cls(); }
//---------------------------------------------------------------------------
Соседние файлы в папке ex3