/
yu_dev
/
tokm
Обзор
Документация
Войти
/
yu_dev
/
tokm
Код
Запросы
0
Задачи
Вики
Пакеты
0
Релизы
0
CI/CD
Аналитика
Безопасность
master
Lab1/fRandomStars.cpp
313 строк
9 KB
Юрий Щепилов
upload files
03 мар 2026, 14:58
Верифицирован
03 мар 2026, 14:58
d52646d
Код
Авторство
О чём код?
//--------------------------------------------------------------------------- #include <vcl.h> #include <cstdlib> #include <ctime> #include <typeinfo> #include <iostream> #include <cmath> #pragma hdrstop #include "fRandomStars.h" //--------------------------------------------------------------------------- #pragma package(smart_init) #pragma link "GLS.SceneViewer" #pragma link "GLS.Cadencer" #pragma link "GLS.Scene" #pragma link "GLS.Objects" #pragma link "GLS.BaseClasses" #pragma link "GLS.Coordinates" #pragma resource "*.dfm" TForm1* Form1; // ���������� ��� ������������ ���� int mouseXpos, mouseYpos; // ���������� ������� unsigned long timeStart, timeStop; // ������ ������ TStringList* CatalogStars = new TStringList; TStringList* CatalogColors = new TStringList; // ������� unsigned char colorIdx, posXidx, posYidx, posZidx; //--------------------------------------------------------------------------- // ��������� ��� �������� ��������� //--------------------------------------------------------------------------- struct Coord3D { float x; float y; float z; Coord3D(float x = 0, float y = 0, float z = 0) : x(x), y(y), z(z) {} }; //--------------------------------------------------------------------------- // ��������� ������� � ���� //--------------------------------------------------------------------------- Coord3D GetCubePosition() { float minVal = -0.5f; float maxVal = 0.5f; float rx = (float)rand() / RAND_MAX; float ry = (float)rand() / RAND_MAX; float rz = (float)rand() / RAND_MAX; float delta = maxVal - minVal; return Coord3D( minVal + rx * delta, minVal + ry * delta, minVal + rz * delta ); } //--------------------------------------------------------------------------- // ��������� ������� � ����� //--------------------------------------------------------------------------- Coord3D GetSpherePosition() { float radius = 0.5f * ((float)rand() / RAND_MAX); float angle1 = 2.0f * M_PI * ((float)rand() / RAND_MAX); float angle2 = M_PI * ((float)rand() / RAND_MAX); float sinA = sin(angle2); return Coord3D( radius * sinA * cos(angle1), radius * sinA * sin(angle1), radius * cos(angle2) ); } //--------------------------------------------------------------------------- // ��������� ������� � �������� //--------------------------------------------------------------------------- Coord3D GetCylinderPosition() { float cylR = 0.4f; float cylH = 0.8f; float angle = 2.0f * M_PI * ((float)rand() / RAND_MAX); float dist = cylR * ((float)rand() / RAND_MAX); float height = cylH * ((float)rand() / RAND_MAX); return Coord3D( dist * cos(angle), height - cylH * 0.5f, dist * sin(angle) ); } //--------------------------------------------------------------------------- // ��������� ������� � ������ //--------------------------------------------------------------------------- Coord3D GetConePosition() { float level = (float)rand() / RAND_MAX; float maxR = 0.5f * (1.0f - level); float dist = maxR * sqrt((float)rand() / RAND_MAX); float angle = 2.0f * M_PI * ((float)rand() / RAND_MAX); return Coord3D( dist * cos(angle), dist * sin(angle), level - 0.5f ); } //--------------------------------------------------------------------------- // ����� ������� ��������� �� ������� //--------------------------------------------------------------------------- Coord3D (*GetGeneratorByIndex(int index))() { switch(index) { case 0: return GetCubePosition; case 1: return GetSpherePosition; case 2: return GetCylinderPosition; case 3: return GetConePosition; default: return GetCubePosition; } } //--------------------------------------------------------------------------- // ������������� ����� //--------------------------------------------------------------------------- __fastcall TForm1::TForm1(TComponent* Owner) : TForm(Owner) {} //--------------------------------------------------------------------------- void __fastcall TForm1::FormCreate(TObject* Sender) { randomize(); StatusBar1->Panels->Items[0]->Text = L"�������� ������"; StatusBar1->Panels->Items[1]->Text = L"FPS: ---"; } //--------------------------------------------------------------------------- // ��������� ���� //--------------------------------------------------------------------------- void __fastcall TForm1::GLSceneViewer1MouseDown( TObject* Sender, TMouseButton Button, TShiftState Shift, int X, int Y) { mouseXpos = X; mouseYpos = Y; } //--------------------------------------------------------------------------- void __fastcall TForm1::GLSceneViewer1MouseMove( TObject* Sender, TShiftState Shift, int X, int Y) { if(Shift.Contains(ssLeft)) { int dx = mouseXpos - X; int dy = mouseYpos - Y; GLCamera1->MoveAroundTarget(dy, dx); mouseXpos = X; mouseYpos = Y; } } //--------------------------------------------------------------------------- void __fastcall TForm1::FormMouseWheel( TObject* Sender, TShiftState Shift, int WheelDelta, TPoint &MousePos, bool &Handled) { if(GLSceneViewer1->MouseInControl) { if(WheelDelta > 0) GLCamera1->AdjustDistanceToTarget(0.9f); else GLCamera1->AdjustDistanceToTarget(1.1f); } } //--------------------------------------------------------------------------- // ������ ��� FPS //--------------------------------------------------------------------------- void __fastcall TForm1::Timer1Timer(TObject* Sender) { float fps = GLSceneViewer1->FramesPerSecond(); StatusBar1->Panels->Items[1]->Text = L"FPS: " + FloatToStrF(fps, ffFixed, 5, 1); GLSceneViewer1->ResetPerformanceMonitor(); } //--------------------------------------------------------------------------- void __fastcall TForm1::GLCadencer1Progress( TObject* Sender, const double deltaTime, const double newTime) { GLSceneViewer1->Invalidate(); } //--------------------------------------------------------------------------- // �������� ����� //--------------------------------------------------------------------------- void __fastcall TForm1::btnDrawClick(TObject* Sender) { // ��������� �������� rgBlock->Enabled = false; btnDraw->Caption = "�������..."; // �������� ����� timeStart = clock(); // �������� �������-��������� auto generator = GetGeneratorByIndex(rgBlock->ItemIndex); // ������������� ������ ����� Stars->Size = 8; // ���������� ����� int count = seNStars->Value; for(int i = 0; i < count; i++) { Coord3D pos = generator(); Stars->Positions->Add(pos.x, pos.y, pos.z); // ��������� ���� float r = (rand() % 256) / 255.0f; float g = (rand() % 256) / 255.0f; float b = (rand() % 256) / 255.0f; Stars->Colors->AddPoint(r, g, b); } // ������� ����� timeStop = clock(); float elapsed = (timeStop - timeStart) / (float)CLOCKS_PER_SEC; // ��������� ������ StatusBar1->Panels->Items[0]->Text = "������������� " + IntToStr(count) + " �������� �� " + FormatFloat("0.000", elapsed) + " ���"; // �������� �������� btnDraw->Caption = "�������������"; rgBlock->Enabled = true; } //--------------------------------------------------------------------------- // ������� ����� //--------------------------------------------------------------------------- void __fastcall TForm1::ButtonClearClick(TObject* Sender) { // ���������� �������� TGLDummyCube* parent = dynamic_cast<TGLDummyCube*>(Stars->Parent); if(parent) { // ������� ������ ����� Stars->Free(); // ������� ����� Stars = static_cast<TGLPoints*>(parent->AddNewChild(__classid(TGLPoints))); Stars->Size = 8; } // ���������� ��������� rgBlock->Enabled = true; // ������ ���� ������ �� ��������� ButtonClear->Font->Color = clGreen; ButtonClear->Font->Style = ButtonClear->Font->Style << fsBold; // ������ ��� �������� ����� class TColorTimer : public TTimer { public: TColorTimer(TButton* btn) : TTimer(NULL) { FButton = btn; Interval = 300; OnTimer = &RestoreColor; } private: TButton* FButton; void __fastcall RestoreColor(TObject* Sender) { FButton->Font->Color = clWindowText; FButton->Font->Style = FButton->Font->Style >> fsBold; ((TTimer*)Sender)->Enabled = false; } }; new TColorTimer(ButtonClear); StatusBar1->Panels->Items[0]->Text = "����� �������"; } //---------------------------------------------------------------------------