/
Reznik
/
VRLabs
Обзор
Документация
Войти
/
Reznik
/
VRLabs
Код
Запросы
0
Задачи
Вики
Пакеты
0
Релизы
0
Аналитика
Безопасность
main
Lab2_ViewPoints/fcViewPoints.cpp
187 строк
6 KB
Reznik
Мои лабы
10 май 2026, 18:49
10 май 2026, 18:49
c5dfe83
Код
Авторство
О чём код?
//--------------------------------------------------------------------------- #include <vcl.h> #include <tchar.h> #include <stdlib.h> #pragma hdrstop #include "fcViewPoints.h" //--------------------------------------------------------------------------- #pragma package(smart_init) #pragma link "GLS.SceneViewer" #pragma link "GLS.Coordinates" #pragma link "GLS.Objects" #pragma link "GLS.SceneViewer" #pragma link "GLS.Scene" #pragma link "GLS.Cadencer" #pragma resource "*.dfm" TForm1* Form1; //--------------------------------------------------------------------------- __fastcall TForm1::TForm1(TComponent* Owner) : TForm(Owner) { FPointsCube = new TGLPoints(GLDummyCube1); FPointsSphere = new TGLPoints(GLSphere1); } //--------------------------------------------------------------------------- void __fastcall TForm1::FormMouseWheel(TObject* Sender, TShiftState Shift, int WheelDelta, TPoint &MousePos, bool &Handled) { if (GLSceneViewer1->MouseInControl == true) { GLCamera1->AdjustDistanceToTarget(Power(1.1, -WheelDelta / 120)); } } //--------------------------------------------------------------------------- void __fastcall TForm1::Button1Click(TObject* Sender) { const int N = 10000; // ���������� ����� float sphereRadius = GLSphere1->Radius; // --- 1. ������� ������ ����� --- delete FPointsCube; delete FPointsSphere; // ������ ����� ������� FPointsCube = new TGLPoints(GLDummyCube1); FPointsSphere = new TGLPoints(GLSphere1); // ��������� �������� ���� ����� FPointsCube->Size = 3; FPointsCube->Style = psSmooth; FPointsSphere->Size = 3; FPointsSphere->Style = psSmooth; // --- 2. ��������� ����� � ���� --- for (int i = 0; i < N; i++) { // ���������� � ��������� [-0.5; 0.5] (��� �� �������� 1) float x = (float)rand() / RAND_MAX - 0.5f; float y = (float)rand() / RAND_MAX - 0.5f; float z = (float)rand() / RAND_MAX - 0.5f; // ��������� ����� FPointsCube->Positions->Add(x, y, z); // ��������� ���� FPointsCube->Colors->Add( (float)(rand() % 256) / 255.0f, (float)(rand() % 256) / 255.0f, (float)(rand() % 256) / 255.0f, 0.8f); } // --- 3. ��������� ����� � ����� ������� �����-����� (� ������� ������) --- int pointsInside = 0; // ������� �����, �������� � ����� for (int i = 0; i < N; i++) { // ���������� ����� � ���������� ���� [-R; R] float x = (2.0f * rand() / RAND_MAX - 1.0f) * sphereRadius; float y = (2.0f * rand() / RAND_MAX - 1.0f) * sphereRadius; float z = (2.0f * rand() / RAND_MAX - 1.0f) * sphereRadius; // ��������� ��������� � ����� float distance = x*x + y*y + z*z; if (distance <= sphereRadius * sphereRadius) { pointsInside++; // ��������� ����� � ������������ FPointsSphere->Positions->Add(x, y, z); FPointsSphere->Colors->Add( (float)(rand() % 256) / 255.0f, (float)(rand() % 256) / 255.0f, (float)(rand() % 256) / 255.0f, 0.8f); } } // --- 4. ������ ������ ����� �� ������ �����-����� --- float cubeVolume = 8.0f * sphereRadius * sphereRadius * sphereRadius; float estimatedVolume = (float)pointsInside / N * cubeVolume; float exactVolume = (4.0f / 3.0f) * M_PI * sphereRadius * sphereRadius * sphereRadius; float error = fabs(estimatedVolume - exactVolume) / exactVolume * 100.0f; // ����� ����������� this->Caption = String().sprintf(L"�����-�����: ����� ����� = %.4f (������ = %.4f, ������ = %.2f%%)", estimatedVolume, exactVolume, error); } //--------------------------------------------------------------------------- void __fastcall TForm1::TrackBar1Change(TObject* Sender) { GLDummyCube1->PitchAngle = TrackBar1->Position; GLSphere1->PitchAngle = TrackBar1->Position; } //--------------------------------------------------------------------------- void __fastcall TForm1::TrackBar2Change(TObject* Sender) { GLDummyCube1->RollAngle = TrackBar2->Position; GLSphere1->RollAngle = TrackBar2->Position; } //--------------------------------------------------------------------------- void __fastcall TForm1::TrackBar3Change(TObject* Sender) { GLDummyCube1->TurnAngle = TrackBar3->Position; GLSphere1->TurnAngle = TrackBar3->Position; } //--------------------------------------------------------------------------- void __fastcall TForm1::GLSceneViewer1MouseDown( TObject* Sender, TMouseButton Button, TShiftState Shift, int X, int Y) { mx = X; my = Y; } //--------------------------------------------------------------------------- void __fastcall TForm1::GLSceneViewer1MouseMove( TObject* Sender, TShiftState Shift, int X, int Y) { if (Shift.Contains(ssLeft)) GLCamera1->MoveAroundTarget(my - Y, mx - X); else if (Shift.Contains(ssRight)) GLCamera1->RotateTarget(my - Y, mx - X, 0); mx = X; my = Y; } void __fastcall TForm1::GLCadencer1Progress(TObject *Sender, const double DeltaTime, const double NewTime) { bool currentLeft = GetAsyncKeyState(VK_LEFT) & 0x8000; bool currentRight = GetAsyncKeyState(VK_RIGHT) & 0x8000; if (currentRight && !FKeyRightPressed) { if (GLSceneViewer1->Camera == GLCamera1) GLSceneViewer1->Camera = GLCamera2; else if (GLSceneViewer1->Camera == GLCamera2) GLSceneViewer1->Camera = GLCamera3; else GLSceneViewer1->Camera = GLCamera1; } if (currentLeft && !FKeyLeftPressed) { if (GLSceneViewer1->Camera == GLCamera1) GLSceneViewer1->Camera = GLCamera3; else if (GLSceneViewer1->Camera == GLCamera2) GLSceneViewer1->Camera = GLCamera1; else GLSceneViewer1->Camera = GLCamera2; } FKeyLeftPressed = currentLeft; FKeyRightPressed = currentRight; } //--------------------------------------------------------------------------- void __fastcall TForm1::TrackBar3KeyDown(TObject *Sender, WORD &Key, TShiftState Shift) { Key = 0; } //--------------------------------------------------------------------------- void __fastcall TForm1::TrackBar2KeyDown(TObject *Sender, WORD &Key, TShiftState Shift) { Key = 0; } //--------------------------------------------------------------------------- void __fastcall TForm1::TrackBar1KeyDown(TObject *Sender, WORD &Key, TShiftState Shift) { Key = 0; } //---------------------------------------------------------------------------