- •Задание.
- •Int Classify(int sx,int sy,int kx,int ky,int px,int py)
- •Void __fastcall tForm1::Timer1Timer(tObject *Sender)
- •Void __fastcall tForm1::FormClose(tObject *Sender, tCloseAction &Action)
- •Void __fastcall tForm1::Edit1Change(tObject *Sender)
- •Void __fastcall tForm1::Edit2Change(tObject *Sender)
Int Classify(int sx,int sy,int kx,int ky,int px,int py)
{
int tx,ty,Tpx,Tpy;
tx = kx - sx;
ty = ky - sy;
Tpx = px - sx;
Tpy = py - sy;
float s = tx*Tpy-ty*Tpx;
if(s > 0.0)return LEFT;
if(s < 0.0)return RIGHT;
return 0;
}
Void __fastcall tForm1::Timer1Timer(tObject *Sender)
{
int a,b;
TRect r;
r.top = 0;
r.left = 0;
r.right = Image1->Width;
r.bottom = Image1->Height;
Image1->Canvas->Brush->Color = clBlack;
Image1->Canvas->Pen->Color = clGreen;
Image1->Canvas->FillRect(r);
Image1->Canvas->Ellipse(1, 1, Image1->Width-1, Image1->Height-1);
Image1->Canvas->Ellipse(Image1->Width/4, Image1->Height/4, 3*Image1->Width/4, 3*Image1->Height/4);
Image1->Canvas->MoveTo(Image1->Width/2,0);
Image1->Canvas->LineTo(Image1->Width/2,Image1->Height);
Image1->Canvas->MoveTo(0,Image1->Height/2);
Image1->Canvas->LineTo(Image1->Width,Image1->Height/2);
Image1->Canvas->MoveTo(Image1->Width/2,Image1->Height/2);
X = COS[Angle];
Y = SIN[Angle];
Image1->Canvas->Pen->Color = clLime;
Image1->Canvas->LineTo(X,Y);
for(int i = 0;i < 2;i++)
{
Sam[i].move();
if(Sam[i].Xpos > Image1->Width || Sam[i].Xpos < 0)
Sam[i].sx = -Sam[i].sx;
if(Sam[i].Ypos > Image1->Height || Sam[i].Ypos < 0)
Sam[i].sy = -Sam[i].sy;
Image1->Canvas->Pixels[Sam[i].Xpos][Sam[i].Ypos] = clRed;
if(Angle >= 0 && Angle <= 180)
{
a = Classify(Image1->Width/2,Image1->Height/2,X,Y,Sam[i].Xpos,Sam[i].Ypos);
b = Classify(Image1->Width/2,Image1->Height/2,oldX,oldY,Sam[i].Xpos,Sam[i].Ypos);
if(a == RIGHT && b == LEFT)
{
Sam[i].TX = Sam[i].Xpos;
Sam[i].TY = Sam[i].Ypos;
Sam[i].count = 7;
}
}
else
{
a = Classify(Image1->Width/2,Image1->Height/2,X,Y,Sam[i].Xpos,Sam[i].Ypos);
b = Classify(Image1->Width/2,Image1->Height/2,oldX,oldY,Sam[i].Xpos,Sam[i].Ypos);
if(a == RIGHT && b == LEFT)
{
Sam[i].TX = Sam[i].Xpos;
Sam[i].TY = Sam[i].Ypos;
Sam[i].count = 7;
}
}
if(Sam[i].count!=0)
{
Sam[i].count--;
Image1->Canvas->Ellipse(Sam[i].TX,Sam[i].TY,Sam[i].TX+Sam[i].count,Sam[i].TY+Sam[i].count);
}
}
oldX = X;
oldY = Y;
Angle+=5;
if(Angle >= 360)Angle = 0;
sprintf(buf,"%f",Sam[1].Xpos);
Edit6->Text = buf;
sprintf(buf,"%f",Sam[1].Ypos);
Edit8->Text = buf;
sprintf(buf,"%f",Sam[0].Xpos);
Edit7->Text = buf;
sprintf(buf,"%f",Sam[0].Ypos);
Edit9->Text = buf;
}
//---------------------------------------------------------------------------
Void __fastcall tForm1::FormClose(tObject *Sender, tCloseAction &Action)
{
Timer1->Enabled = false;
}
//---------------------------------------------------------------------------
Void __fastcall tForm1::Edit1Change(tObject *Sender)
{
Speed = atoi(Edit1->Text.c_str());
if(Speed < 0 || Speed > 10)
Edit1->Text = "";
else
Timer1->Interval = Speed*1000/360;
}
//---------------------------------------------------------------------------
Void __fastcall tForm1::Edit2Change(tObject *Sender)
{
Sam[0].kx = atof(Edit2->Text.c_str());
}
//---------------------------------------------------------------------------
void __fastcall TForm1::Edit4Change(TObject *Sender)
{
Sam[0].Speed = atof(Edit4->Text.c_str());
}
//---------------------------------------------------------------------------
void __fastcall TForm1::Edit3Change(TObject *Sender)
{
Sam[1].kx = atof(Edit3->Text.c_str());
}
//---------------------------------------------------------------------------
void __fastcall TForm1::Edit5Change(TObject *Sender)
{
Sam[1].Speed = atof(Edit5->Text.c_str());
}
//---------------------------------------------------------------------------