Лабораторная работа №1
Постановка задачи:
Построение графических образов стандартными средствами, на языке высокого уровня.
Листинг программы:
#include <vcl.h>
#pragma hdrstop
#include "main.h"
#include <math.h>
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"
TForm1 *Form1;
struct Krug
{
int x, y, r;
TColor color;
};
const int NUM_KRUGOV = 25;
const int SPEED = 10;
const int MAX_R = 50;
Krug krugi[NUM_KRUGOV];
TCanvas* c;
int x;
int y;
void Compute(int i)
{
float vX, vY, d, evX, evY, sX, sY;
if(i < NUM_KRUGOV-1)
{
vX = krugi[i+1].x - krugi.x;
vY = krugi[i+1].y - krugi.y;
d = sqrt(pow(vX, 2) + pow(vY, 2));
evX = vX / d;
evY = vY / d;
sX = SPEED * evX;
sY = SPEED * evY;
krugi.x += sX;
krugi.y += sY;
}
else
{
vX = x - krugi.x;
vY = y - krugi.y;
d = sqrt(pow(vX, 2) + pow(vY, 2));
evX = vX / d;
evY = vY / d;
sX = SPEED * evX;
sY = SPEED * evY;
krugi.x += sX;
krugi.y += sY;
}
}
void DrawKrug(int i)
{
c->Pen->Color = krugi.color;
c->Brush->Color = krugi.color;
c->Ellipse(krugi.x - krugi.r/2,
krugi.y - krugi.r/2,
krugi.x + krugi.r/2,
krugi.y + krugi.r/2);
}
void InitKrug(int i)
{
krugi.x = rand()%Form1->Width;
krugi.y = rand()%Form1->Height;
krugi.r = MAX_R - (MAX_R/NUM_KRUGOV)*i;
krugi.color = RGB(rand()%255, rand()%255, rand()%255);
}
void Clr()
{
c->Pen->Color = clWhite;
c->Brush->Color = clWhite;
c->Rectangle(0, 0, Form1->Width, Form1->Height);
}
//---------------------------------------------------------------------------
__fastcall TForm1::TForm1(TComponent* Owner)
: TForm(Owner)
{
}
//---------------------------------------------------------------------------
void __fastcall TForm1::FormCreate(TObject *Sender)
{
c = Form1->Canvas;
x = Form1->Width/2;
y = Form1->Height/2;
srand(time(0));
for(int i = 0; i < NUM_KRUGOV; i++)
{
InitKrug(i);
}
}
//---------------------------------------------------------------------------
void __fastcall TForm1::Timer1Timer(TObject *Sender)
{
Clr();
for(int i = 0; i < NUM_KRUGOV; i++)
{
Compute(i);
DrawKrug(i);
}
}
//---------------------------------------------------------------------------
void __fastcall TForm1::FormMouseMove(TObject *Sender, TShiftState Shift,
int X, int Y)
{
x = X;
y = Y;
}
//---------------------------------------------------------------------------
Результат работы программы: