Вот есть код для C++ Builder, опиши подробно, что там происходит: в Unit1.h -…
- User
Вот есть код для C++ Builder, опиши подробно, что там происходит: в Unit1.h - //--------------------------------------------------------------------------- #ifndef Unit1H #define Unit1H //--------------------------------------------------------------------------- #include <System.Classes.hpp> #include <Vcl.Controls.hpp> #include <Vcl.StdCtrls.hpp> #include <Vcl.Forms.hpp> #include <Vcl.ExtCtrls.hpp> #include <Vcl.Grids.hpp> #include //--------------------------------------------------------------------------- class TForm1 : public TForm { __published: // IDE-managed Components TPaintBox *PaintBox1; void __fastcall PaintBox1Click(TObject Sender); private: // User declarations public: // User declarations __fastcall TForm1(TComponent Owner); }; struct TPoint_2D { double x, y; int number; }; struct TPoint2D { TPoint_2D Vertex; }; std::vector Points; class CalculationNormal { public: double dx, dy, length, direction; void Calculation (double x1, double x2, double y1, double y2, double &normal_x, double &normal_y); }; std::vector<std::vector> Layers; //--------------------------------------------------------------------------- extern PACKAGE TForm1 Form1; //--------------------------------------------------------------------------- #endif В Unit1.cpp - //--------------------------------------------------------------------------- #include <vcl.h> #pragma hdrstop #include <math.h> #include "Unit1.h" //--------------------------------------------------------------------------- #pragma package(smart_init) #pragma resource ".dfm" TForm1 *Form1;
void CalculationNormal::Calculation(double x1, double x2, double y1, double y2, double &normal_x, double &normal_y) { dx = x2 - x1; dy = y2 - y1; length = sqrt(dx * dx + dy * dy); normal_x = -dy / length; normal_y = dx / length; direction = (x1 + x2) / 2 * normal_x + (y1 + y2) / 2 * normal_y; if (direction < 0) { normal_x = -normal_x; normal_y = -normal_y; } } //--------------------------------------------------------------------------- __fastcall TForm1::TForm1(TComponent* Owner) : TForm(Owner) { } //--------------------------------------------------------------------------- void __fastcall TForm1::PaintBox1Click(TObject *Sender) { Canvas->Pen->Color = clBlack; Canvas->Pen->Width = 2; double centerX = PaintBox1->Width / 2; double centerY = PaintBox1->Height / 2; int scale = 8; double r1 = 10; double r2 = 50; double total_area = M_PI * (r2 * r2 - r1 * r1); double filled_area = 0; double length = 6.28; // длина стороны треугольника
Points.clear(); Layers.clear(); int number_layers = ceil((r2 - r1) / length) + 1; double h = (r2 - r1) / (number_layers - 1); for (int i = 0; i < number_layers; i++) { double radius_layer = r1 + i * h; int number_points = ceil(2 * M_PI * radius_layer / length); double alpha = 2 * M_PI / number_points; std::vector<TPoint2D> Points_layer; for (int j = 0; j < number_points; j++) { TPoint2D Point; Point.Vertex.number = Points.size(); Point.Vertex.x = radius_layer * cos(j * alpha); Point.Vertex.y = radius_layer * sin(j * alpha); Points.push_back(Point); Points_layer.push_back(Point); } Layers.push_back(Points_layer); } for (int i = 0; i < number_layers - 1; i++) { auto& Layer1 = Layers[i]; auto& Layer2 = Layers[i + 1]; int number_points_1 = Layer1.size(); int number_points_2 = Layer2.size(); for (int j = 0; j < number_points_1; j++) { int next_j = (j + 1) % number_points_1; double min_distance = sqrt(pow(Layer2[0].Vertex.x - Layer1[j].Vertex.x, 2) + pow(Layer2[0].Vertex.y - Layer1[j].Vertex.y, 2)); int nearest_k = 0; for (int k = 1; k < number_points_2; k++) { double distance = sqrt(pow(Layer2[k].Vertex.x - Layer1[j].Vertex.x, 2) + pow(Layer2[k].Vertex.y - Layer1[j].Vertex.y, 2)); if (distance < min_distance) { min_distance = distance; nearest_k = k; } } Canvas->MoveTo(centerX + scale * Layer1[j].Vertex.x, centerY + scale * Layer1[j].Vertex.y); Canvas->LineTo(centerX + scale * Layer2[nearest_k].Vertex.x, centerY + scale * Layer2[nearest_k].Vertex.y); Canvas->LineTo(centerX + scale * Layer1[next_j].Vertex.x, centerY + scale * Layer1[next_j].Vertex.y); Canvas->LineTo(centerX + scale * Layer1[j].Vertex.x, centerY + scale * Layer1[j].Vertex.y); int next_k = (nearest_k + 1) % number_points_2; if (j == number_points_1 - 1) { Canvas->MoveTo(centerX + scale * Layer2[nearest_k].Vertex.x, centerY + scale * Layer2[nearest_k].Vertex.y); Canvas->LineTo(centerX + scale * Layer2[next_k].Vertex.x, centerY + scale * Layer2[next_k].Vertex.y); Canvas->LineTo(centerX + scale * Layer1[0].Vertex.x, centerY + scale * Layer1[0].Vertex.y); Canvas->LineTo(centerX + scale * Layer2[nearest_k].Vertex.x, centerY + scale * Layer2[nearest_k].Vertex.y); } else { Canvas->MoveTo(centerX + scale * Layer2[nearest_k].Vertex.x, centerY + scale * Layer2[nearest_k].Vertex.y); Canvas->LineTo(centerX + scale * Layer2[next_k].Vertex.x, centerY + scale * Layer2[next_k].Vertex.y); Canvas->LineTo(centerX + scale * Layer1[next_j].Vertex.x, centerY + scale * Layer1[next_j].Vertex.y); Canvas->LineTo(centerX + scale * Layer2[nearest_k].Vertex.x, centerY + scale * Layer2[nearest_k].Vertex.y); } } if (i == number_layers - 2) { for (int j = 0; j < number_points_2; j++) { int next_j = (j + 1) % number_points_2; Canvas->MoveTo(centerX + scale * Layer2[j].Vertex.x, centerY + scale * Layer2[j].Vertex.y); Canvas->LineTo(centerX + scale * Layer2[next_j].Vertex.x, centerY + scale * Layer2[next_j].Vertex.y); Canvas->MoveTo(centerX + scale * Layer2[j].Vertex.x, centerY + scale * Layer2[j].Vertex.y); } } } - Assistant
- User
- Assistant
- User
- Assistant