Advertisement
Guest User

Untitled

a guest
Mar 3rd, 2021
274
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 20.01 KB | None | 0 0
  1.  
  2.  
  3.  
  4.  
  5. //---------------------------------------------------------------------------
  6.  
  7. #include <vcl.h>
  8. #pragma hdrstop
  9.  
  10. #include "mondelbrot.h"
  11. #include "ShellAPI.h"
  12.  
  13. #define mycomplex
  14.  
  15. //---------------------------------------------------------------------------
  16. #pragma package(smart_init)
  17. #pragma resource "*.dfm"
  18. TForm1 *Form1;
  19.  
  20. //---------------------------------------------------------------------------
  21. __fastcall TForm1::TForm1(TComponent* Owner)
  22.         : TForm(Owner)
  23. {
  24. }
  25. //---------------------------------------------------------------------------
  26. //  Subprogram for displaying and counting points of a set
  27. void __fastcall TForm1::Print_mald(double lCoord=-2, double rCoord=2, double bCoord=-2, double hCoord=2)
  28. {
  29.       // Variable declaration
  30.         int power;        // The power of a polynomial
  31.         int PX, PY;       // Coordinates in pixels
  32.         double realCoord, imagCoord; // Real and imaginary coefficient
  33.         double realTemp, imagTemp, realTemp2, imagTemp2, realTemp3, imagTemp3;// Time variables for the real and imaginary coefficient.
  34.         double arg;                 // The square of a modulus of a complex number
  35.         int iterations;     // Iteration counter
  36.         int maxIterations;  // Maximum number of iterations
  37.        // Initiating variables
  38.         maxIterations=TrackBar1->Position; // Setting the maximum number of iterations
  39.         power=UpDown1->Position;           // Degree of a polynomial
  40.         Label7->Caption="Current power = "+IntToStr(power);
  41.         Label12->Caption=IntToStr(power);
  42.         Label8->Caption="";
  43.         Image1->Picture=0;                 // Resetting a picture in the Image1 object
  44.  
  45.         for (imagCoord = hCoord, PY=0 ; PY < (Image1->Height); PY++)  // Cycle through the pixels of the Y-axis
  46.         {
  47.                 for (realCoord = lCoord, PX=0; PX<(Image1->Width);  PX++) // Cycle through the pixels of the X-axis
  48.                 {
  49.                     iterations = 0;                                       // Initializing the iteration count counter
  50.                     realTemp = realCoord;
  51.                     imagTemp = imagCoord;
  52.                     realTemp2 = realCoord;
  53.                     imagTemp2 = imagCoord;
  54.                     arg = (realTemp * realTemp) + (imagTemp * imagTemp);     // Calculating the square of a modulus of a complex number
  55.                     while ((arg < 4) && (iterations < maxIterations))        // Check modulus square<4 and iter.<max
  56.                     {
  57.                         realTemp3 = (realTemp2 * realTemp) - (imagTemp2 * imagTemp);// Calculation of the real coefficient
  58.                         imagTemp3 = (realTemp * imagTemp2) + (imagTemp * realTemp2);// Calculation of the imaginary coefficient
  59.                         for (int i=2; i<power;i++)                                  // Cycle from 2 to a degree of a polynomial
  60.                         {
  61.                                 realTemp2 = realTemp3;                              // Saving the previous value in a temporary variable
  62.                                 imagTemp2 = imagTemp3;
  63.                                 realTemp3 = (realTemp2 * realTemp) - (imagTemp2 * imagTemp); // Calculation of the real coefficient
  64.                                 imagTemp3 = (realTemp * imagTemp2) + (imagTemp * realTemp2); // Calculation of the imaginary coefficient
  65.                         }
  66.                         realTemp = realTemp3 + realCoord; // Final calculation of the real coefficient
  67.                         imagTemp = imagTemp3 + imagCoord; // Final calculation of the imaginary coefficient
  68.                         realTemp2 = realTemp;             // Saving the previous value in a temporary variable
  69.                         imagTemp2 = imagTemp;
  70.                         arg = (realTemp * realTemp) + (imagTemp * imagTemp);// Calculating the square of a modulus of a complex number
  71.                         iterations++;                                       // Increment (+1) of the iteration counter
  72.                     }
  73.                    realCoord = lCoord+PX*((double)(rCoord-lCoord))/((Image1->Width));// X coordinate conversion in pixels to the Cartesian plane
  74.                     // Displaying a pixel in the black and white palette
  75.                    if  (RadioButton1->Checked)
  76.                    {
  77.                          if (arg<=4) Image1->Canvas->Pixels[PX][PY]=clBlack;
  78.                          else  Image1->Canvas->Pixels[PX][PY]=clWhite;
  79.                    }
  80.                      // Displaying a pixel in grayscale
  81.                    if  (RadioButton2->Checked)
  82.                    {
  83.                        if (arg<=4) Image1->Canvas->Pixels[PX][PY]=clBlack;
  84.                        else   Image1->Canvas->Pixels[PX][PY]=(TColor)(0x00000000+6*iterations+
  85.                                                                   256*6*iterations+65536*6*iterations);
  86.                    }
  87.                    // Displaying a pixel in "RGB" coloring
  88.                    if  (RadioButton3->Checked)
  89.                    {  switch (iterations % 4)
  90.                       {
  91.                         case 0:
  92.                             Image1->Canvas->Pixels[PX][PY]=clBlack;
  93.                             break;
  94.                         case 1:
  95.                             Image1->Canvas->Pixels[PX][PY]=clRed;
  96.                             break;
  97.                         case 2:
  98.                             Image1->Canvas->Pixels[PX][PY]=clBlue;
  99.                             break;
  100.                         case 3:
  101.                             Image1->Canvas->Pixels[PX][PY]=clGreen;
  102.                             break;
  103.                       }
  104.                     }
  105.                    // Pixel display in 16 colors
  106.                    if (RadioButton4->Checked)
  107.                    {  switch (iterations % 16)
  108.                       {
  109.                         case 0:
  110.                             Image1->Canvas->Pixels[PX][PY]=clBlack;
  111.                             break;
  112.                         case 1:
  113.                             Image1->Canvas->Pixels[PX][PY]=clMaroon;
  114.                             break;
  115.                         case 2:
  116.                             Image1->Canvas->Pixels[PX][PY]=clGreen;
  117.                             break;
  118.                         case 3:
  119.                             Image1->Canvas->Pixels[PX][PY]=clOlive;
  120.                             break;
  121.                         case 4:
  122.                              Image1->Canvas->Pixels[PX][PY]=clNavy;
  123.                              break;
  124.                         case 5:
  125.                              Image1->Canvas->Pixels[PX][PY]=clPurple;
  126.                              break;
  127.                         case 6:
  128.                               Image1->Canvas->Pixels[PX][PY]=clTeal;
  129.                               break;
  130.                         case 7:
  131.                               Image1->Canvas->Pixels[PX][PY]=clGray;
  132.                               break;
  133.                         case 8:
  134.                               Image1->Canvas->Pixels[PX][PY]=clSilver;
  135.                               break;
  136.                         case 9:
  137.                               Image1->Canvas->Pixels[PX][PY]=clRed;
  138.                               break;
  139.                         case 10:
  140.                               Image1->Canvas->Pixels[PX][PY]=clLime;
  141.                               break;
  142.                         case 11:
  143.                               Image1->Canvas->Pixels[PX][PY]=clBlue;
  144.                               break;
  145.                         case 12:
  146.                               Image1->Canvas->Pixels[PX][PY]=clYellow;
  147.                               break;
  148.                         case 13:
  149.                               Image1->Canvas->Pixels[PX][PY]=clFuchsia;
  150.                               break;
  151.                         case 14:
  152.                               Image1->Canvas->Pixels[PX][PY]=clAqua;
  153.                               break;
  154.                         case 15:
  155.                               Image1->Canvas->Pixels[PX][PY]=clWhite;
  156.                               break;
  157.                       }
  158.                    }
  159.                    // Displaying a pixel in "Acid" coloring
  160.                    if (RadioButton5->Checked)
  161.                    {
  162.                         int colorIndex = 0;
  163.                         if (iterations < maxIterations)
  164.                         {
  165.                             colorIndex = (int)(768*(iterations) / maxIterations);
  166.                             if (colorIndex >= 768) colorIndex = 0;
  167.                             if (colorIndex < 0) colorIndex = 0;
  168.                             Image1->Canvas->Pixels[PX][PY]= (TColor)Colors[colorIndex];
  169.                         }
  170.                         else   Image1->Canvas->Pixels[PX][PY]= clBlack;
  171.                    }
  172.                    // Pixel display in "Olivia" coloring
  173.                    if  (RadioButton6->Checked)
  174.                    {
  175.                        if (arg<4) Image1->Canvas->Pixels[PX][PY]=clBlack;
  176.                        else  Image1->Canvas->Pixels[PX][PY]=(TColor)(0x00000000+255*iterations/maxIterations+
  177.                                                                   65535*iterations/maxIterations+65535*256*iterations/maxIterations);
  178.                    }
  179.                    // Pixel display in "Chess" coloring
  180.                    if  (RadioButton9->Checked)
  181.                    {
  182.                         BYTE a = iterations == maxIterations ? 0 : imagTemp > 0? 255 : 0; //шахматы
  183.                         Image1->Canvas->Pixels[PX][PY]=(TColor)RGB(a,a,a);
  184.                    }
  185.                     // Pixel display in "Hell" coloring
  186.                    if  (RadioButton10->Checked)
  187.                    {
  188.                         if (arg<4) Image1->Canvas->Pixels[PX][PY]=clBlack;
  189.                         else  Image1->Canvas->Pixels[PX][PY]=(TColor)((iterations*iterations));
  190.                    }
  191.                   // Pixel display in "Legs" coloring
  192.                    if  (RadioButton11->Checked)
  193.                    {
  194.                         if (arg<4) Image1->Canvas->Pixels[PX][PY]=clBlack;
  195.                         else Image1->Canvas->Pixels[PX][PY]=(TColor)((iterations*iterations*iterations));
  196.                    }
  197.                     // Pixel display in "Winter" coloring
  198.                    if  (RadioButton12->Checked)
  199.                    {
  200.                         BYTE edge, co_color,r=0,g=0,b=0;
  201.                         edge = (1170 * (maxIterations - iterations) / maxIterations) / 256;
  202.                         co_color = (1170 * (maxIterations - iterations) / maxIterations) % 256;
  203.                         if (edge == 1) {g = 255; r = 255 - co_color;}
  204.                         if (edge == 2) {g = 255; b = co_color;}
  205.                         if (edge == 3) {b = 255; g = 255 - co_color;}
  206.                         if (edge == 4) {r = co_color; b = 255 - co_color;}
  207.                         Image1->Canvas->Pixels[PX][PY]=(TColor)RGB(r,g,b);
  208.                   }
  209.  
  210.              }
  211.              imagCoord = hCoord-PY*((double)(hCoord-bCoord))/((Image1->Height)); // Y-axis coordinate conversion in pixels to the Cartesian plane
  212.              ProgressBar1->Position=100*PY/(Image1->Height);                     // Displaying the drawing process
  213.         }
  214. }
  215.  
  216. //---------------------------------------------------------------------------
  217.  // Creating a color palette array
  218. void __fastcall TForm1::PalleteMake()
  219. {
  220.   int red, green, blue;
  221.  
  222.         for (int i=0;i<768;i++)
  223.         {
  224.                 red=0;
  225.                 green=0;
  226.                 blue=0;
  227.                 if (i >= 512)
  228.                 {
  229.                         red = i - 512;
  230.                         green = 255 - red;
  231.                 }
  232.                 else if (i >= 256)
  233.                 {
  234.                         green = i - 256;
  235.                         blue = 255 - green;
  236.                 }
  237.                 else
  238.                 {
  239.                         blue = i;
  240.                 }
  241.                 Colors[i] = (TColor)(blue+256*green+65536*red);
  242.         }
  243. }
  244.  
  245. //---------------------------------------------------------------------------
  246. // Actions when the "Restart" button is pressed
  247. void __fastcall TForm1::Button1Click(TObject *Sender)
  248. {
  249.         TForm1::count=1;
  250.         curx=0;
  251.         cury=0;
  252.         Print_mald();                  // Calling a subroutine for drawing a set
  253.         Label6->Caption= AnsiString(count);
  254.         Label2->Caption = "0";
  255.         Label1->Caption = "0";
  256. }
  257.  
  258. //---------------------------------------------------------------------------
  259. // Actions when clicking on Image1
  260. void __fastcall TForm1::Image1Click(TObject *Sender)
  261. {
  262.  int PX,PY;
  263.  TPoint pt;
  264.  
  265.       GetCursorPos(&pt);  // Determining the absolute coordinates of the mouse cursor on the screen
  266.       // Recalculating coordinates into relative windows Image1
  267.       PX=pt.x - Form1->Left - Panel1->Left - Image1->Left-(int)((Form1->Width-Form1->ClientWidth)/2);
  268.       PY=pt.y - Form1->Top - Panel1->Top - Image1->Top- (int)((Form1->Height-Form1->ClientHeight));
  269.       Label1->Caption = "(" + IntToStr(PX) + ")(" + IntToStr(PY) + ")";  // Displaying coordinates in pixels on the screen
  270.       curx=PX*(double)4/(count*(Image1->Width))-(double)2/count+curx;    // Coordinate conversion in pixels to Cartesian X
  271.       cury=((double)((Image1->Height)-PY)*4)/(count*(Image1->Height))-((double)2)/count+cury; // Coordinate translation in pixels to Cartesian in Y
  272.       if  ((RadioButton7->Checked) && (count<(0x4000000000000)))  count=count*2; // If ZoomIn increases the scale factor by a factor of 2
  273.       if  ((RadioButton8->Checked) && (count>1)) count=count/2; // If ZoomOut decreases the scale factor by a factor of 2
  274.       Label6->Caption=AnsiString(count);  // If ZoomOut decreases the scale factor by a factor of 2Displays the current scale factor      Print_mald(curx-((double)2)/count,curx+((double)2)/count,cury-((double)2)/count,cury+((double)2)/count); // Calling a drawing subprogram
  275.       Label2->Caption = "(" + FloatToStr(curx) + ")(" + FloatToStr(cury) + ")"; // Displaying Cartesian coordinates
  276. }
  277.  
  278. //---------------------------------------------------------------------------
  279. // Actions when pressing the "Open" button in the main menu
  280. void __fastcall TForm1::N1Click(TObject *Sender)
  281. {
  282. if ( OpenPictureDialog1->Execute() )
  283.     {
  284.      try
  285.      {
  286.           Image1->Picture->LoadFromFile( OpenPictureDialog1->FileName );
  287.      }
  288.      catch (EInvalidGraphic&) {ShowMessage("Error");}
  289.     }
  290. }
  291.  
  292. //---------------------------------------------------------------------------
  293. // Actions when pressing the "Save" button in the main menu
  294. void __fastcall TForm1::Save1Click(TObject *Sender)
  295. {
  296. if ( SavePictureDialog1->Execute() )
  297.     {
  298.     try
  299.     {
  300.       Image1->Picture->SaveToFile("MandelbrotImage.bmp");
  301.     }
  302.   catch (EInvalidGraphic&) {ShowMessage("Error");}
  303.   }
  304. }
  305.  
  306. //---------------------------------------------------------------------------
  307. // Actions when pressing the "Exit" button in the main menu
  308. void __fastcall TForm1::Exit1Click(TObject *Sender)
  309. {
  310.         if (Application->MessageBox(
  311.         "Do you want to close the program ?",
  312.         "Confirm exit",
  313.         MB_YESNOCANCEL+MB_ICONQUESTION) == IDYES)
  314.         Form1->Close();
  315. }
  316.  
  317. //---------------------------------------------------------------------------
  318. // Display a tooltip when hovering over TrackBar1
  319. void __fastcall TForm1::About1Click(TObject *Sender)
  320. {
  321.         ShowMessage("Mandelbrot Set v0.001 Alpha ");
  322. }
  323.  
  324. //---------------------------------------------------------------------------
  325. // Вывод подсказки при наводке на TrackBar1
  326. void __fastcall TForm1::TrackBar1Change(TObject *Sender)
  327. {
  328.         TrackBar1->Hint=TrackBar1->Position;
  329.  
  330. }
  331.  
  332. //---------------------------------------------------------------------------
  333. // Actions when creating a form
  334. void __fastcall TForm1::FormCreate(TObject *Sender)
  335. {
  336.         PalleteMake();
  337.         TForm1::count=1; // Initialization of the scale coeff..
  338.         curx=0;          // Initializing the current Cartesian coordinates on X
  339.         cury=0;          // Initializing the current Cartesian coordinates to Y
  340.         Print_mald();
  341. }
  342.  
  343. //---------------------------------------------------------------------------
  344. // Actions when the "Redraw" button is pressed
  345. void __fastcall TForm1::Button2Click(TObject *Sender)
  346. {
  347.        Print_mald(curx-2.0/count,curx+2.0/count,cury-2.0/count,cury+2.0/count);
  348. }
  349.  
  350. //---------------------------------------------------------------------------
  351. // Help subroutine when pressing the "FAQ" button in the main menu
  352. void __fastcall TForm1::FAQ1Click(TObject *Sender)
  353. {
  354.    ShellExecute(Handle, "open", "Test.chm", NULL, NULL, SW_RESTORE);
  355. }
  356.  
  357. //---------------------------------------------------------------------------
  358. // Displaying the current degree to which a complex number is erected
  359. void __fastcall TForm1::UpDown1Click(TObject *Sender, TUDBtnType Button)
  360. {
  361.         Label8->Caption="Next power = "+IntToStr(UpDown1->Position);
  362. }
  363. //---------------------------------------------------------------------------
  364.  
  365. #ifndef mondelbrotH
  366. #define mondelbrotH
  367. //---------------------------------------------------------------------------
  368. #include <Classes.hpp>
  369. #include <Controls.hpp>
  370. #include <StdCtrls.hpp>
  371. #include <Forms.hpp>
  372. #include <ExtCtrls.hpp>
  373. #include <ComCtrls.hpp>
  374. #include <Dialogs.hpp>
  375. #include <ExtDlgs.hpp>
  376. #include <math.h>
  377. #include <Menus.hpp>
  378. #include <ActnList.hpp>
  379. #include <Graphics.hpp>
  380.  
  381. //---------------------------------------------------------------------------
  382. class TForm1 : public TForm
  383. {
  384. __published:    // IDE-managed Components
  385.         TPanel *Panel1;
  386.         TButton *Button1;
  387.         TImage *Image1;
  388.         TProgressBar *ProgressBar1;
  389.         TLabel *Label1;
  390.         TSavePictureDialog *SavePictureDialog1;
  391.         TOpenPictureDialog *OpenPictureDialog1;
  392.         TLabel *Label2;
  393.         TLabel *Label4;
  394.         TMainMenu *MainMenu1;
  395.         TLabel *Label5;
  396.         TActionList *ActionList1;
  397.         TMenuItem *ass1;
  398.         TMenuItem *N1;
  399.         TMenuItem *Save1;
  400.         TMenuItem *Exit1;
  401.         TMenuItem *Info1;
  402.         TMenuItem *FAQ1;
  403.         TMenuItem *About1;
  404.         TGroupBox *GroupBox1;
  405.         TTrackBar *TrackBar1;
  406.         TGroupBox *GroupBox2;
  407.         TRadioButton *RadioButton1;
  408.         TRadioButton *RadioButton2;
  409.         TRadioButton *RadioButton3;
  410.         TRadioButton *RadioButton4;
  411.         TRadioButton *RadioButton5;
  412.         TRadioButton *RadioButton6;
  413.         TGroupBox *GroupBox3;
  414.         TRadioButton *RadioButton7;
  415.         TRadioButton *RadioButton8;
  416.         TLabel *Label3;
  417.         TLabel *Label6;
  418.         TRadioButton *RadioButton9;
  419.         TRadioButton *RadioButton10;
  420.         TRadioButton *RadioButton11;
  421.         TButton *Button2;
  422.         TRadioButton *RadioButton12;
  423.         TPanel *Panel2;
  424.         TLabel *Label9;
  425.         TLabel *Label10;
  426.         TLabel *Label11;
  427.         TLabel *Label12;
  428.         TLabel *Label7;
  429.         TLabel *Label8;
  430.         TUpDown *UpDown1;
  431.         void __fastcall Button1Click(TObject *Sender);
  432.         void __fastcall Image1Click(TObject *Sender);
  433.         void __fastcall N1Click(TObject *Sender);
  434.         void __fastcall Save1Click(TObject *Sender);
  435.         void __fastcall Exit1Click(TObject *Sender);
  436.         void __fastcall About1Click(TObject *Sender);
  437.         void __fastcall TrackBar1Change(TObject *Sender);
  438.         void __fastcall FormCreate(TObject *Sender);
  439.         void __fastcall Button2Click(TObject *Sender);
  440.         void __fastcall FAQ1Click(TObject *Sender);
  441.         void __fastcall UpDown1Click(TObject *Sender, TUDBtnType Button);
  442. private:    // User declarations
  443. public:     // User declarations
  444.         __fastcall TForm1(TComponent* Owner);
  445.         void __fastcall TForm1::Print_mald(double, double, double, double);
  446.         void __fastcall TForm1::PalleteMake();
  447.         long Colors[768];
  448.         unsigned __int64 count;
  449.         double  curx, cury;
  450. };
  451. //---------------------------------------------------------------------------
  452. extern PACKAGE TForm1 *Form1;
  453. //---------------------------------------------------------------------------
  454. #endif
  455.  
  456.  
  457.  
  458.  
  459.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement