Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //---------------------------------------------------------------------------
- #include <vcl.h>
- #pragma hdrstop
- #include "mondelbrot.h"
- #include "ShellAPI.h"
- #define mycomplex
- //---------------------------------------------------------------------------
- #pragma package(smart_init)
- #pragma resource "*.dfm"
- TForm1 *Form1;
- //---------------------------------------------------------------------------
- __fastcall TForm1::TForm1(TComponent* Owner)
- : TForm(Owner)
- {
- }
- //---------------------------------------------------------------------------
- // Subprogram for displaying and counting points of a set
- void __fastcall TForm1::Print_mald(double lCoord=-2, double rCoord=2, double bCoord=-2, double hCoord=2)
- {
- // Variable declaration
- int power; // The power of a polynomial
- int PX, PY; // Coordinates in pixels
- double realCoord, imagCoord; // Real and imaginary coefficient
- double realTemp, imagTemp, realTemp2, imagTemp2, realTemp3, imagTemp3;// Time variables for the real and imaginary coefficient.
- double arg; // The square of a modulus of a complex number
- int iterations; // Iteration counter
- int maxIterations; // Maximum number of iterations
- // Initiating variables
- maxIterations=TrackBar1->Position; // Setting the maximum number of iterations
- power=UpDown1->Position; // Degree of a polynomial
- Label7->Caption="Current power = "+IntToStr(power);
- Label12->Caption=IntToStr(power);
- Label8->Caption="";
- Image1->Picture=0; // Resetting a picture in the Image1 object
- for (imagCoord = hCoord, PY=0 ; PY < (Image1->Height); PY++) // Cycle through the pixels of the Y-axis
- {
- for (realCoord = lCoord, PX=0; PX<(Image1->Width); PX++) // Cycle through the pixels of the X-axis
- {
- iterations = 0; // Initializing the iteration count counter
- realTemp = realCoord;
- imagTemp = imagCoord;
- realTemp2 = realCoord;
- imagTemp2 = imagCoord;
- arg = (realTemp * realTemp) + (imagTemp * imagTemp); // Calculating the square of a modulus of a complex number
- while ((arg < 4) && (iterations < maxIterations)) // Check modulus square<4 and iter.<max
- {
- realTemp3 = (realTemp2 * realTemp) - (imagTemp2 * imagTemp);// Calculation of the real coefficient
- imagTemp3 = (realTemp * imagTemp2) + (imagTemp * realTemp2);// Calculation of the imaginary coefficient
- for (int i=2; i<power;i++) // Cycle from 2 to a degree of a polynomial
- {
- realTemp2 = realTemp3; // Saving the previous value in a temporary variable
- imagTemp2 = imagTemp3;
- realTemp3 = (realTemp2 * realTemp) - (imagTemp2 * imagTemp); // Calculation of the real coefficient
- imagTemp3 = (realTemp * imagTemp2) + (imagTemp * realTemp2); // Calculation of the imaginary coefficient
- }
- realTemp = realTemp3 + realCoord; // Final calculation of the real coefficient
- imagTemp = imagTemp3 + imagCoord; // Final calculation of the imaginary coefficient
- realTemp2 = realTemp; // Saving the previous value in a temporary variable
- imagTemp2 = imagTemp;
- arg = (realTemp * realTemp) + (imagTemp * imagTemp);// Calculating the square of a modulus of a complex number
- iterations++; // Increment (+1) of the iteration counter
- }
- realCoord = lCoord+PX*((double)(rCoord-lCoord))/((Image1->Width));// X coordinate conversion in pixels to the Cartesian plane
- // Displaying a pixel in the black and white palette
- if (RadioButton1->Checked)
- {
- if (arg<=4) Image1->Canvas->Pixels[PX][PY]=clBlack;
- else Image1->Canvas->Pixels[PX][PY]=clWhite;
- }
- // Displaying a pixel in grayscale
- if (RadioButton2->Checked)
- {
- if (arg<=4) Image1->Canvas->Pixels[PX][PY]=clBlack;
- else Image1->Canvas->Pixels[PX][PY]=(TColor)(0x00000000+6*iterations+
- 256*6*iterations+65536*6*iterations);
- }
- // Displaying a pixel in "RGB" coloring
- if (RadioButton3->Checked)
- { switch (iterations % 4)
- {
- case 0:
- Image1->Canvas->Pixels[PX][PY]=clBlack;
- break;
- case 1:
- Image1->Canvas->Pixels[PX][PY]=clRed;
- break;
- case 2:
- Image1->Canvas->Pixels[PX][PY]=clBlue;
- break;
- case 3:
- Image1->Canvas->Pixels[PX][PY]=clGreen;
- break;
- }
- }
- // Pixel display in 16 colors
- if (RadioButton4->Checked)
- { switch (iterations % 16)
- {
- case 0:
- Image1->Canvas->Pixels[PX][PY]=clBlack;
- break;
- case 1:
- Image1->Canvas->Pixels[PX][PY]=clMaroon;
- break;
- case 2:
- Image1->Canvas->Pixels[PX][PY]=clGreen;
- break;
- case 3:
- Image1->Canvas->Pixels[PX][PY]=clOlive;
- break;
- case 4:
- Image1->Canvas->Pixels[PX][PY]=clNavy;
- break;
- case 5:
- Image1->Canvas->Pixels[PX][PY]=clPurple;
- break;
- case 6:
- Image1->Canvas->Pixels[PX][PY]=clTeal;
- break;
- case 7:
- Image1->Canvas->Pixels[PX][PY]=clGray;
- break;
- case 8:
- Image1->Canvas->Pixels[PX][PY]=clSilver;
- break;
- case 9:
- Image1->Canvas->Pixels[PX][PY]=clRed;
- break;
- case 10:
- Image1->Canvas->Pixels[PX][PY]=clLime;
- break;
- case 11:
- Image1->Canvas->Pixels[PX][PY]=clBlue;
- break;
- case 12:
- Image1->Canvas->Pixels[PX][PY]=clYellow;
- break;
- case 13:
- Image1->Canvas->Pixels[PX][PY]=clFuchsia;
- break;
- case 14:
- Image1->Canvas->Pixels[PX][PY]=clAqua;
- break;
- case 15:
- Image1->Canvas->Pixels[PX][PY]=clWhite;
- break;
- }
- }
- // Displaying a pixel in "Acid" coloring
- if (RadioButton5->Checked)
- {
- int colorIndex = 0;
- if (iterations < maxIterations)
- {
- colorIndex = (int)(768*(iterations) / maxIterations);
- if (colorIndex >= 768) colorIndex = 0;
- if (colorIndex < 0) colorIndex = 0;
- Image1->Canvas->Pixels[PX][PY]= (TColor)Colors[colorIndex];
- }
- else Image1->Canvas->Pixels[PX][PY]= clBlack;
- }
- // Pixel display in "Olivia" coloring
- if (RadioButton6->Checked)
- {
- if (arg<4) Image1->Canvas->Pixels[PX][PY]=clBlack;
- else Image1->Canvas->Pixels[PX][PY]=(TColor)(0x00000000+255*iterations/maxIterations+
- 65535*iterations/maxIterations+65535*256*iterations/maxIterations);
- }
- // Pixel display in "Chess" coloring
- if (RadioButton9->Checked)
- {
- BYTE a = iterations == maxIterations ? 0 : imagTemp > 0? 255 : 0; //шахматы
- Image1->Canvas->Pixels[PX][PY]=(TColor)RGB(a,a,a);
- }
- // Pixel display in "Hell" coloring
- if (RadioButton10->Checked)
- {
- if (arg<4) Image1->Canvas->Pixels[PX][PY]=clBlack;
- else Image1->Canvas->Pixels[PX][PY]=(TColor)((iterations*iterations));
- }
- // Pixel display in "Legs" coloring
- if (RadioButton11->Checked)
- {
- if (arg<4) Image1->Canvas->Pixels[PX][PY]=clBlack;
- else Image1->Canvas->Pixels[PX][PY]=(TColor)((iterations*iterations*iterations));
- }
- // Pixel display in "Winter" coloring
- if (RadioButton12->Checked)
- {
- BYTE edge, co_color,r=0,g=0,b=0;
- edge = (1170 * (maxIterations - iterations) / maxIterations) / 256;
- co_color = (1170 * (maxIterations - iterations) / maxIterations) % 256;
- if (edge == 1) {g = 255; r = 255 - co_color;}
- if (edge == 2) {g = 255; b = co_color;}
- if (edge == 3) {b = 255; g = 255 - co_color;}
- if (edge == 4) {r = co_color; b = 255 - co_color;}
- Image1->Canvas->Pixels[PX][PY]=(TColor)RGB(r,g,b);
- }
- }
- imagCoord = hCoord-PY*((double)(hCoord-bCoord))/((Image1->Height)); // Y-axis coordinate conversion in pixels to the Cartesian plane
- ProgressBar1->Position=100*PY/(Image1->Height); // Displaying the drawing process
- }
- }
- //---------------------------------------------------------------------------
- // Creating a color palette array
- void __fastcall TForm1::PalleteMake()
- {
- int red, green, blue;
- for (int i=0;i<768;i++)
- {
- red=0;
- green=0;
- blue=0;
- if (i >= 512)
- {
- red = i - 512;
- green = 255 - red;
- }
- else if (i >= 256)
- {
- green = i - 256;
- blue = 255 - green;
- }
- else
- {
- blue = i;
- }
- Colors[i] = (TColor)(blue+256*green+65536*red);
- }
- }
- //---------------------------------------------------------------------------
- // Actions when the "Restart" button is pressed
- void __fastcall TForm1::Button1Click(TObject *Sender)
- {
- TForm1::count=1;
- curx=0;
- cury=0;
- Print_mald(); // Calling a subroutine for drawing a set
- Label6->Caption= AnsiString(count);
- Label2->Caption = "0";
- Label1->Caption = "0";
- }
- //---------------------------------------------------------------------------
- // Actions when clicking on Image1
- void __fastcall TForm1::Image1Click(TObject *Sender)
- {
- int PX,PY;
- TPoint pt;
- GetCursorPos(&pt); // Determining the absolute coordinates of the mouse cursor on the screen
- // Recalculating coordinates into relative windows Image1
- PX=pt.x - Form1->Left - Panel1->Left - Image1->Left-(int)((Form1->Width-Form1->ClientWidth)/2);
- PY=pt.y - Form1->Top - Panel1->Top - Image1->Top- (int)((Form1->Height-Form1->ClientHeight));
- Label1->Caption = "(" + IntToStr(PX) + ")(" + IntToStr(PY) + ")"; // Displaying coordinates in pixels on the screen
- curx=PX*(double)4/(count*(Image1->Width))-(double)2/count+curx; // Coordinate conversion in pixels to Cartesian X
- cury=((double)((Image1->Height)-PY)*4)/(count*(Image1->Height))-((double)2)/count+cury; // Coordinate translation in pixels to Cartesian in Y
- if ((RadioButton7->Checked) && (count<(0x4000000000000))) count=count*2; // If ZoomIn increases the scale factor by a factor of 2
- if ((RadioButton8->Checked) && (count>1)) count=count/2; // If ZoomOut decreases the scale factor by a factor of 2
- 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
- Label2->Caption = "(" + FloatToStr(curx) + ")(" + FloatToStr(cury) + ")"; // Displaying Cartesian coordinates
- }
- //---------------------------------------------------------------------------
- // Actions when pressing the "Open" button in the main menu
- void __fastcall TForm1::N1Click(TObject *Sender)
- {
- if ( OpenPictureDialog1->Execute() )
- {
- try
- {
- Image1->Picture->LoadFromFile( OpenPictureDialog1->FileName );
- }
- catch (EInvalidGraphic&) {ShowMessage("Error");}
- }
- }
- //---------------------------------------------------------------------------
- // Actions when pressing the "Save" button in the main menu
- void __fastcall TForm1::Save1Click(TObject *Sender)
- {
- if ( SavePictureDialog1->Execute() )
- {
- try
- {
- Image1->Picture->SaveToFile("MandelbrotImage.bmp");
- }
- catch (EInvalidGraphic&) {ShowMessage("Error");}
- }
- }
- //---------------------------------------------------------------------------
- // Actions when pressing the "Exit" button in the main menu
- void __fastcall TForm1::Exit1Click(TObject *Sender)
- {
- if (Application->MessageBox(
- "Do you want to close the program ?",
- "Confirm exit",
- MB_YESNOCANCEL+MB_ICONQUESTION) == IDYES)
- Form1->Close();
- }
- //---------------------------------------------------------------------------
- // Display a tooltip when hovering over TrackBar1
- void __fastcall TForm1::About1Click(TObject *Sender)
- {
- ShowMessage("Mandelbrot Set v0.001 Alpha ");
- }
- //---------------------------------------------------------------------------
- // Вывод подсказки при наводке на TrackBar1
- void __fastcall TForm1::TrackBar1Change(TObject *Sender)
- {
- TrackBar1->Hint=TrackBar1->Position;
- }
- //---------------------------------------------------------------------------
- // Actions when creating a form
- void __fastcall TForm1::FormCreate(TObject *Sender)
- {
- PalleteMake();
- TForm1::count=1; // Initialization of the scale coeff..
- curx=0; // Initializing the current Cartesian coordinates on X
- cury=0; // Initializing the current Cartesian coordinates to Y
- Print_mald();
- }
- //---------------------------------------------------------------------------
- // Actions when the "Redraw" button is pressed
- void __fastcall TForm1::Button2Click(TObject *Sender)
- {
- Print_mald(curx-2.0/count,curx+2.0/count,cury-2.0/count,cury+2.0/count);
- }
- //---------------------------------------------------------------------------
- // Help subroutine when pressing the "FAQ" button in the main menu
- void __fastcall TForm1::FAQ1Click(TObject *Sender)
- {
- ShellExecute(Handle, "open", "Test.chm", NULL, NULL, SW_RESTORE);
- }
- //---------------------------------------------------------------------------
- // Displaying the current degree to which a complex number is erected
- void __fastcall TForm1::UpDown1Click(TObject *Sender, TUDBtnType Button)
- {
- Label8->Caption="Next power = "+IntToStr(UpDown1->Position);
- }
- //---------------------------------------------------------------------------
- #ifndef mondelbrotH
- #define mondelbrotH
- //---------------------------------------------------------------------------
- #include <Classes.hpp>
- #include <Controls.hpp>
- #include <StdCtrls.hpp>
- #include <Forms.hpp>
- #include <ExtCtrls.hpp>
- #include <ComCtrls.hpp>
- #include <Dialogs.hpp>
- #include <ExtDlgs.hpp>
- #include <math.h>
- #include <Menus.hpp>
- #include <ActnList.hpp>
- #include <Graphics.hpp>
- //---------------------------------------------------------------------------
- class TForm1 : public TForm
- {
- __published: // IDE-managed Components
- TPanel *Panel1;
- TButton *Button1;
- TImage *Image1;
- TProgressBar *ProgressBar1;
- TLabel *Label1;
- TSavePictureDialog *SavePictureDialog1;
- TOpenPictureDialog *OpenPictureDialog1;
- TLabel *Label2;
- TLabel *Label4;
- TMainMenu *MainMenu1;
- TLabel *Label5;
- TActionList *ActionList1;
- TMenuItem *ass1;
- TMenuItem *N1;
- TMenuItem *Save1;
- TMenuItem *Exit1;
- TMenuItem *Info1;
- TMenuItem *FAQ1;
- TMenuItem *About1;
- TGroupBox *GroupBox1;
- TTrackBar *TrackBar1;
- TGroupBox *GroupBox2;
- TRadioButton *RadioButton1;
- TRadioButton *RadioButton2;
- TRadioButton *RadioButton3;
- TRadioButton *RadioButton4;
- TRadioButton *RadioButton5;
- TRadioButton *RadioButton6;
- TGroupBox *GroupBox3;
- TRadioButton *RadioButton7;
- TRadioButton *RadioButton8;
- TLabel *Label3;
- TLabel *Label6;
- TRadioButton *RadioButton9;
- TRadioButton *RadioButton10;
- TRadioButton *RadioButton11;
- TButton *Button2;
- TRadioButton *RadioButton12;
- TPanel *Panel2;
- TLabel *Label9;
- TLabel *Label10;
- TLabel *Label11;
- TLabel *Label12;
- TLabel *Label7;
- TLabel *Label8;
- TUpDown *UpDown1;
- void __fastcall Button1Click(TObject *Sender);
- void __fastcall Image1Click(TObject *Sender);
- void __fastcall N1Click(TObject *Sender);
- void __fastcall Save1Click(TObject *Sender);
- void __fastcall Exit1Click(TObject *Sender);
- void __fastcall About1Click(TObject *Sender);
- void __fastcall TrackBar1Change(TObject *Sender);
- void __fastcall FormCreate(TObject *Sender);
- void __fastcall Button2Click(TObject *Sender);
- void __fastcall FAQ1Click(TObject *Sender);
- void __fastcall UpDown1Click(TObject *Sender, TUDBtnType Button);
- private: // User declarations
- public: // User declarations
- __fastcall TForm1(TComponent* Owner);
- void __fastcall TForm1::Print_mald(double, double, double, double);
- void __fastcall TForm1::PalleteMake();
- long Colors[768];
- unsigned __int64 count;
- double curx, cury;
- };
- //---------------------------------------------------------------------------
- extern PACKAGE TForm1 *Form1;
- //---------------------------------------------------------------------------
- #endif
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement