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

ООП / C++ / Lab3C / ex2 / Lab3

.cpp
Скачиваний:
65
Добавлен:
18.02.2017
Размер:
4.84 Кб
Скачать
//---------------------------------------------------------------------------
#include <vcl.h>
#pragma hdrstop
#include "Lab3.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"
TForm1 *Form1;
//---------------------------------------------------------------------------
__fastcall TForm1::TForm1(TComponent* Owner) : TForm(Owner) {
this->g = 0.5;
Ball = new CBall(150, 50);
Line = new CLine(Image1->Width / 2, Image1->Height / 2, 100, 100);
this->Line-> sd = false;
Line->SetAng(TrackBar1->Position);
this->Cls();
right = false; left = false;
};
//---------------------------------------------------------------------------
void __fastcall TForm1::Cls() {
Image1->Canvas->Pen->Width = 3;
Image1->Canvas->Pen->Color = RGB(255,255,255);
Image1->Canvas->Brush->Color = RGB(255,255,255);
Image1->Canvas->Rectangle(0, 0, Image1->Width, Image1->Height); };
//---------------------------------------------------------------------------
void __fastcall TForm1::Timer1Timer(TObject *Sender) {
this->Cls();

if ((!right) && (Ball->x + 22 >= Image1->Width)) {
	right = true;
	left = false;
	Ball->vx = -Ball->vx * 0.7;
	Ball->vy = 0;
};
if ((Ball->y + 22 >= Image1->Height)) {
	Ball->vy = -Ball->vy * 0.7;
	down = true;
};
if ((!left) && (Ball->x - 22 <= 0)) {
	left = true;
	right = false;
	Ball->vx = -Ball->vx * 0.7;
}
if ((left) && (Ball->vy < 0.01)) {
	g = 0;
}

if (TrackBar1->Position > Line->Ang) Line->SetAng(Line->Ang + 1);
if (TrackBar1->Position < Line->Ang) Line->SetAng(Line->Ang - 1);
Ball->ay = this->g;
Ball->ax = 0;
if (Collision(Ball, Line)) {
	double x, y;
	Line->LineN(x, y);
	this->Vetrix(Ball->ax, Ball->ay, x, y);
	this->Vetrix(Ball->vx, Ball->vy, x, y); };
if (Line->sd) {if (Line->l > 0) Line->l -= 2; Line->a = 0; Line->v = 0; } else Line->Move();
if (CollisionS(Ball, Line)) {
	double x, y;
	x = (Line->x0 - Line->x);
	y = (Line->y0 - Line->y);
	this->Vetrix(Ball->ax, Ball->ay, x, y);
	double e = sqrt((Line->x0 - Line->x) * (Line->x0 - Line->x) + (Line->y0 - Line->y) * (Line->y0 - Line->y));
	Ball->vx = Line->v * (Line->x - Line->x0) / e;
	Ball->vy = Line->v * (Line->y - Line->y0) / e; };
Ball->Draw();
Line->Draw();
Line->DrawS();
Ball->Move(); };
//---------------------------------------------------------------------------
bool __fastcall TForm1::Collision(CBall *B, CLine *L) {
double r = B->r;
double d = sqrt((L->x - L->x0) * (L->x - L->x0) + (L->y - L->y0) * (L->y - L->y0));
double d1 = sqrt((B->x - L->x) * (B->x - L->x) + (B->y - L->y) * (B->y - L->y));
double d2 = sqrt((B->x - L->x0) * (B->x - L->x0) + (B->y - L->y0) * (B->y - L->y0));
double s1 = r * sqrt(abs(d1 * d1 - r * r));
double s2 = r * sqrt(abs(d2 * d2 - r * r));
double p = (d + d1 + d2) * 0.5;
double s = sqrt(p * (p - d) * (p - d1) * (p - d2)) * 2;
if ((d2 < d) && (d1 < d) && (s1 + s2 >= s)) {
	double e = sqrt((L->x0 - L->x) * (L->x0 - L->x) + (L->y0 - L->y) * (L->y0 - L->y));
	if (e>0) {
B->x=B->x-((L->y0-L->y)*(r-s/d))/e;
B->y=B->y+((L->x0-L->x)*(r-s/d))/e;
}
return true;
};
return false;
};

bool __fastcall TForm1::CollisionS(CBall *B,CLine *L)
{
double xn,yn; L->LineN(xn,yn); xn=xn*L->h; yn=yn*L->h;
double e=sqrt((L->x0-L->x)*(L->x0-L->x)+(L->y0-L->y)*(L->y0-L->y));
double r=B->r,xl1=L->x0+(L->x-L->x0)*L->l/e,yl1=L->y0+(L->y-L->y0)*L->l/e,xl2=L->x0+(L->x-L->x0)*L->l/e+xn,yl2=L->y0+(L->y-L->y0)*L->l/e+yn;
double d=sqrt((xl2-xl1)*(xl2-xl1)+(yl2-yl1)*(yl2-yl1));
double d1=sqrt((B->x-xl1)*(B->x-xl1)+(B->y-yl1)*(B->y-yl1));
double d2=sqrt((B->x-xl2)*(B->x-xl2)+(B->y-yl2)*(B->y-yl2));
double s1=r*sqrt(abs(d1*d1-r*r));
double s2=r*sqrt(abs(d2*d2-r*r));
double p=(d+d1+d2)*0.5;
double s=sqrt(p*(p-d)*(p-d1)*(p-d2))*2;
if ((d2<d) && (d1<d) && (s1+s2>=s))
{
double e=sqrt((xl1-xl2)*(xl1-xl2)+(yl1-yl2)*(yl1-yl2));
if (e>0) {
B->x=B->x+((yl1-yl2)*(r-s/d))/e;
B->y=B->y-((xl1-xl2)*(r-s/d))/e;
}
return true;
};
return false;
};

void __fastcall TForm1::Vetrix(double &ax,double &ay,double nx,double ny)
{
if ((nx*nx+ny*ny)>0) {
ax=ax-(nx*(ax*nx+ay*ny))/(nx*nx+ny*ny);
ay=ay-(ny*(ax*nx+ay*ny))/(nx*nx+ny*ny); }
};
void __fastcall TForm1::OnKeyD2(TObject *Sender, WORD &Key, TShiftState Shift) {
if (Key==65) Line->Ang=Line->Ang+1;
if (Key==83) Line->Ang=Line->Ang-1;
if (Line->Ang>90) {Line->Ang=90;};
if (Line->Ang<-90) {Line->Ang=-90;};
TrackBar1->Position=Line->Ang;
Line->SetAng(TrackBar1->Position);
if (Key==32) {Line->sd=true;};
}
//---------------------------------------------------------------------------
void __fastcall TForm1::OnKeyU2(TObject *Sender, WORD &Key, TShiftState Shift) {
	if (Key==32) Line->sd=false; }
//---------------------------------------------------------------------------




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