//--------------------------------------------------------------------------- // BOB AND CAROL AND TED AND ALICE - A VECTOR CHASE //--------------------------------------------------------------------------- #include #pragma hdrstop #include "Unit1.h" #include "Math.h" //--------------------------------------------------------------------------- #pragma package(smart_init) #pragma resource "*.dfm" TFormBobCarolTedAlice *FormBobCarolTedAlice; //--------------------------------------------------------------------------- __fastcall TFormBobCarolTedAlice::TFormBobCarolTedAlice(TComponent* Owner) : TForm(Owner) { } //------------------------------------------ Declare Variables & Set Defaults float bobX, bobY; int bobsAttractionToCarol = 10; float carolX, carolY; int carolsAttractionToTed = 10; float tedX, tedY; int tedsAttractionToAlice = 10; float aliceX, aliceY; int alicesAttractionToBob = 10; float xDiff, yDiff; //--------------------------------------------- Send Everyone to Their Corner void disperse (void) { bobX=10; bobY=10; carolX=400; carolY=10; tedX=400; tedY=410; aliceX=10; aliceY=410; } //----------------------------------------------------------- Begin the Chase void Chase (void) { { // bob chases carol FormBobCarolTedAlice->Canvas->Pen->Color=clBlack; FormBobCarolTedAlice->Canvas->MoveTo(bobX, bobY); bobX=(carolX-bobX)/(100-bobsAttractionToCarol)+bobX; bobY=(carolY-bobY)/(100-bobsAttractionToCarol)+bobY; FormBobCarolTedAlice->Canvas->LineTo(bobX, bobY); // carol chases ted FormBobCarolTedAlice->Canvas->Pen->Color=clBlue; FormBobCarolTedAlice->Canvas->MoveTo(carolX, carolY); carolX=(tedX-carolX)/(100-carolsAttractionToTed)+carolX; carolY=(tedY-carolY)/(100-carolsAttractionToTed)+carolY; FormBobCarolTedAlice->Canvas->LineTo(carolX, carolY); // ted chases alice FormBobCarolTedAlice->Canvas->Pen->Color=clRed; FormBobCarolTedAlice->Canvas->MoveTo(tedX, tedY); tedX=(aliceX-tedX)/(100-tedsAttractionToAlice)+tedX; tedY=(aliceY-tedY)/(100-tedsAttractionToAlice)+tedY; FormBobCarolTedAlice->Canvas->LineTo(tedX, tedY); // alice chases bob FormBobCarolTedAlice->Canvas->Pen->Color=clGreen; FormBobCarolTedAlice->Canvas->MoveTo(aliceX, aliceY); aliceX=(bobX-aliceX)/(100-alicesAttractionToBob)+aliceX; aliceY=(bobY-aliceY)/(100-alicesAttractionToBob)+aliceY; FormBobCarolTedAlice->Canvas->LineTo(aliceX, aliceY); } } //----------------------------------------------------- When the Window Opens void __fastcall TFormBobCarolTedAlice::FormPaint(TObject *Sender) { disperse(); //-------------------- Set the Trackbars to Their Defaults TrackBarBob->Position = bobsAttractionToCarol; TrackBarCarol->Position = carolsAttractionToTed; TrackBarTed->Position = tedsAttractionToAlice; TrackBarAlice->Position = alicesAttractionToBob; } //-------------------------------------------------------------- Chase Button void __fastcall TFormBobCarolTedAlice::ButtonChaseClick(TObject *Sender) { for (int i=0; i<500; i++) { Chase(); } } //----------------------------------------------------------------Step Button void __fastcall TFormBobCarolTedAlice::ButtonStepClick(TObject *Sender) { Chase(); } //-------------------------------------------------------------- Clear Button void __fastcall TFormBobCarolTedAlice::ButtonClearClick( TObject *Sender) { Invalidate(); } //----------------------------------------------------------- Disperse Button void __fastcall TFormBobCarolTedAlice::ButtonDispeseClick(TObject *Sender) { disperse(); } //------------------------------------------------------------ Bob's Trackbar void __fastcall TFormBobCarolTedAlice::TrackBarBobChange(TObject *Sender) { bobsAttractionToCarol=TrackBarBob->Position; Application->ProcessMessages(); } //---------------------------------------------------------- Carol's Trackbar void __fastcall TFormBobCarolTedAlice::TrackBarCarolChange(TObject *Sender) { carolsAttractionToTed=TrackBarCarol->Position; Application->ProcessMessages(); } //------------------------------------------------------------ Ted's Trackbar void __fastcall TFormBobCarolTedAlice::TrackBarTedChange(TObject *Sender) { tedsAttractionToAlice=TrackBarTed->Position; Application->ProcessMessages(); } //---------------------------------------------------------- Alice's Trackbar void __fastcall TFormBobCarolTedAlice::TrackBarAliceChange(TObject *Sender) { alicesAttractionToBob=TrackBarAlice->Position; Application->ProcessMessages(); } //-----------------------------------------------------------That's All Folks