First commit [09/03/2018]
This commit is contained in:
@@ -0,0 +1,187 @@
|
||||
//+------------------------------------------------------------------+
|
||||
//| Sphere.mqh |
|
||||
//| Copyright 2009-2017, MetaQuotes Software Corp. |
|
||||
//| http://www.mql5.com |
|
||||
//+------------------------------------------------------------------+
|
||||
#include <Arrays\ArrayObj.mqh>
|
||||
#include <ChartObjects\ChartObjectsTxtControls.mqh>
|
||||
//+------------------------------------------------------------------+
|
||||
//| Class CSphere. |
|
||||
//| Appointment: Class of the graphical object "Sphere". |
|
||||
//+------------------------------------------------------------------+
|
||||
class CSphere : public CArrayObj
|
||||
{
|
||||
private:
|
||||
int m_id; // sphere idintifier
|
||||
color m_color; // sphere color
|
||||
int m_num_parallel; // number of parallels
|
||||
int m_num_meridian; // number of meridians
|
||||
int m_radius; // sphere radius in pixels
|
||||
int m_center_X; // coordinate of the center X
|
||||
int m_center_Y; // coordinate of the center Y
|
||||
CSphere *m_orbite_center; // "sun"
|
||||
double m_orbite_radius; // orbital radius
|
||||
double m_orbite_fi_X; // angle of inclination to the axis X
|
||||
double m_orbite_fi_Y; // angle of inclination to the axis Y
|
||||
double m_orbite_fi_Z; // angle of inclination to the axis Z
|
||||
double m_d_fi_orb; // angular velocity of the orbit
|
||||
//--- working variables
|
||||
double m_fi_orb;
|
||||
double m_fi_x;
|
||||
double m_fi_y;
|
||||
double m_fi_z;
|
||||
|
||||
public:
|
||||
CSphere(void);
|
||||
~CSphere(void);
|
||||
//--- methods of access to protected data
|
||||
int CenterX(void) const { return(m_center_X); }
|
||||
int CenterY(void) const { return(m_center_Y); }
|
||||
//---
|
||||
bool Create(const int id,const color c,const int x,const int y,const int r,const int p,const int m,const string str);
|
||||
void SetOrbite(CSphere *sun,const double fi_x,const double fi_y,const double fi_z,const double d_fi_orb);
|
||||
void Recalculate(void);
|
||||
};
|
||||
//+------------------------------------------------------------------+
|
||||
//| Constructor |
|
||||
//+------------------------------------------------------------------+
|
||||
CSphere::CSphere(void) : m_id(0),
|
||||
m_color(0),
|
||||
m_num_parallel(0),
|
||||
m_num_meridian(0),
|
||||
m_radius(0),
|
||||
m_center_X(0),
|
||||
m_center_Y(0),
|
||||
m_orbite_center(NULL),
|
||||
m_orbite_radius(0.0),
|
||||
m_orbite_fi_X(0.0),
|
||||
m_orbite_fi_Y(0.0),
|
||||
m_orbite_fi_Z(0.0),
|
||||
m_d_fi_orb(0.0),
|
||||
m_fi_orb(0.0),
|
||||
m_fi_x(0.0),
|
||||
m_fi_y(0.0),
|
||||
m_fi_z(0.0)
|
||||
{
|
||||
}
|
||||
//+------------------------------------------------------------------+
|
||||
//| Destructor |
|
||||
//+------------------------------------------------------------------+
|
||||
CSphere::~CSphere(void)
|
||||
{
|
||||
}
|
||||
//+------------------------------------------------------------------+
|
||||
//| Create object |
|
||||
//+------------------------------------------------------------------+
|
||||
bool CSphere::Create(const int id,const color c,const int x,const int y,const int r,const int p,const int m,const string str)
|
||||
{
|
||||
CArrayObj *arr;
|
||||
CChartObjectLabel *label;
|
||||
//---
|
||||
m_id=id;
|
||||
m_color =c;
|
||||
m_num_parallel=(p>r/3) ? r/3 : p;
|
||||
m_num_meridian=(m>r/3) ? r/3 : m;
|
||||
m_radius =r;
|
||||
m_center_X =x;
|
||||
m_center_Y =y;
|
||||
if(!Reserve(p)) return(false);
|
||||
//--- loop parallels
|
||||
for(int i=0;i<p;i++)
|
||||
{
|
||||
arr=new CArrayObj;
|
||||
if(arr==NULL)
|
||||
return(false);
|
||||
if(!arr.Reserve(m))
|
||||
return(false);
|
||||
//--- loop meridians
|
||||
for(int j=0;j<m;j++)
|
||||
{
|
||||
label=new CChartObjectLabel;
|
||||
if(label==NULL)
|
||||
return(false);
|
||||
label.Create(0,"ar"+string(id)+"_"+string(j)+"_"+string(i),0,0,0);
|
||||
label.Color(m_color);
|
||||
label.Description(str);
|
||||
arr.Add(label);
|
||||
}
|
||||
Add(arr);
|
||||
}
|
||||
return(true);
|
||||
}
|
||||
//+------------------------------------------------------------------+
|
||||
//| Setting |
|
||||
//+------------------------------------------------------------------+
|
||||
void CSphere::SetOrbite(CSphere *sun,const double fi_x,const double fi_y,const double fi_z,const double d_fi_orb)
|
||||
{
|
||||
m_orbite_center=sun;
|
||||
m_orbite_fi_X=fi_x;
|
||||
m_orbite_fi_Y=fi_y;
|
||||
m_d_fi_orb=d_fi_orb;
|
||||
m_orbite_fi_Z=fi_z;
|
||||
m_orbite_radius=MathSqrt(MathPow(m_center_X-m_orbite_center.CenterX(),2)+MathPow(m_center_Y-m_orbite_center.CenterY(),2))/2;
|
||||
}
|
||||
//+------------------------------------------------------------------+
|
||||
//| Recalculation of the sphere. |
|
||||
//+------------------------------------------------------------------+
|
||||
void CSphere::Recalculate(void)
|
||||
{
|
||||
CArrayObj *arr;
|
||||
CChartObjectLabel *label;
|
||||
//---
|
||||
double d_fi_m,d_fi_p;
|
||||
double x,y,z;
|
||||
int i,q;
|
||||
double idx3=0;
|
||||
double idx4=0;
|
||||
//---
|
||||
d_fi_m=2*M_PI/m_num_meridian;
|
||||
d_fi_p=M_PI/m_num_parallel;
|
||||
//---
|
||||
idx3=idx4;
|
||||
if(m_orbite_center!=NULL)
|
||||
{
|
||||
//--- calculation of the coordinates of the dynamic center of the orbit
|
||||
m_fi_orb+=m_d_fi_orb;
|
||||
m_center_X=(int)(m_orbite_center.CenterX()+m_orbite_radius*(MathSin(m_fi_orb)+MathCos(m_orbite_fi_X)*MathSin(m_orbite_fi_Y)));
|
||||
m_center_Y=(int)(m_orbite_center.CenterY()+m_orbite_radius*(MathCos(m_fi_orb)+MathSin(m_orbite_fi_X)*MathCos(m_orbite_fi_Y)));
|
||||
}
|
||||
q=-m_num_parallel/2-1;
|
||||
//--- loop parallels
|
||||
for(int j=0;j<m_num_parallel;j++)
|
||||
{
|
||||
q++;
|
||||
arr=At(j);
|
||||
//--- loop meridians
|
||||
for(i=0;i<m_num_meridian;i++)
|
||||
{
|
||||
//--- calculation of the coordinates of the point of excluding traffic
|
||||
x=m_radius*MathSin(d_fi_m*i)*MathCos(d_fi_p*q);
|
||||
y=m_radius*MathSin(d_fi_p*q);
|
||||
z=m_radius*MathCos(d_fi_m*i)*MathCos(d_fi_p*q);
|
||||
//--- recalculation of the coordinates of the point of view of traffic
|
||||
label=arr.At(i);
|
||||
label.X_Distance(m_center_X+x2d_(x,y,z,m_fi_x,m_fi_y,m_fi_z));
|
||||
label.Y_Distance(m_center_Y+y2d_(x,y,z,m_fi_x,m_fi_y,m_fi_z));
|
||||
}
|
||||
idx3=idx3+0.5*m_id;
|
||||
}
|
||||
//---
|
||||
m_fi_z=m_fi_z+MathSin(idx3*0.08)/32+MathCos(idx3*0.16)*0.01;
|
||||
m_fi_x=m_fi_x+MathCos(idx3*0.08)*0.016+MathCos(idx3*0.12)*0.008;
|
||||
}
|
||||
//+------------------------------------------------------------------+
|
||||
//| auxiliary function |
|
||||
//+------------------------------------------------------------------+
|
||||
int x2d_(double x_,double y_,double z_,double fi_x_,double fi_y_,double fi_z_)
|
||||
{
|
||||
return((int)(x_*MathCos(fi_z_)+y_*MathSin(fi_z_)));
|
||||
}
|
||||
//+------------------------------------------------------------------+
|
||||
//| auxiliary function |
|
||||
//+------------------------------------------------------------------+
|
||||
int y2d_(double x_,double y_,double z_,double fi_x_,double fi_y_,double fi_z_)
|
||||
{
|
||||
return((int)((-x_*MathSin(fi_z_)+y_*MathCos(fi_z_))*MathCos(fi_x_)+z_*MathSin(fi_x_)));
|
||||
}
|
||||
//+------------------------------------------------------------------+
|
||||
@@ -0,0 +1,81 @@
|
||||
//+------------------------------------------------------------------+
|
||||
//| SphereSample.mq5 |
|
||||
//| Copyright 2009-2017, MetaQuotes Software Corp. |
|
||||
//| http://www.mql5.com |
|
||||
//+------------------------------------------------------------------+
|
||||
#property copyright "2009-2017, MetaQuotes Software Corp."
|
||||
#property link "http://www.mql5.com"
|
||||
//---
|
||||
#include "Sphere.mqh"
|
||||
//---
|
||||
string ArrowChar="*";
|
||||
int SleepTime=50;
|
||||
//---
|
||||
#define NUM_SPHERES 5
|
||||
#define VISIBLE 0
|
||||
#define INVISIBLE 1
|
||||
//---
|
||||
//+------------------------------------------------------------------+
|
||||
//| Script to demonstrate the use of arrays. |
|
||||
//+------------------------------------------------------------------+
|
||||
CSphere Sphere[NUM_SPHERES];
|
||||
//--- arrays to initialize spheres
|
||||
int arrX[NUM_SPHERES]={100,100,300,500,500};
|
||||
int arrY[NUM_SPHERES]={100,500,300,500,350};
|
||||
int arrR[NUM_SPHERES]={30,40,100,60,20};
|
||||
int arrP[NUM_SPHERES]={10,13,30,20,7};
|
||||
int arrM[NUM_SPHERES]={10,13,30,20,7};
|
||||
color arrC[NUM_SPHERES]={Red,Blue,Yellow,Green,Gray};
|
||||
//+------------------------------------------------------------------+
|
||||
//| Script initialization function |
|
||||
//+------------------------------------------------------------------+
|
||||
int Init(void)
|
||||
{
|
||||
int i;
|
||||
//--- creating objects
|
||||
for(i=0;i<NUM_SPHERES;i++)
|
||||
if(!Sphere[i].Create(i,arrC[i],arrX[i],arrY[i],arrR[i],arrP[i],arrM[i],ArrowChar))
|
||||
break;
|
||||
if(i!=NUM_SPHERES)
|
||||
{
|
||||
printf("Error creating sphere %d",i);
|
||||
return(-1);
|
||||
}
|
||||
//--- configuring orbits
|
||||
Sphere[0].SetOrbite(GetPointer(Sphere[2]),M_PI/4,-M_PI/8,0,0.1);
|
||||
Sphere[1].SetOrbite(GetPointer(Sphere[2]),-M_PI/8,-M_PI/16,M_PI/8,0.02);
|
||||
Sphere[3].SetOrbite(GetPointer(Sphere[2]),M_PI/8,M_PI/4,M_PI/8,0.05);
|
||||
Sphere[4].SetOrbite(GetPointer(Sphere[3]),M_PI/4,M_PI/8,M_PI/8,0.1);
|
||||
//---
|
||||
return(0);
|
||||
}
|
||||
//+------------------------------------------------------------------+
|
||||
//| Script deinitialization function |
|
||||
//+------------------------------------------------------------------+
|
||||
void Deinit(void)
|
||||
{
|
||||
}
|
||||
//+------------------------------------------------------------------+
|
||||
//| Script program start function |
|
||||
//+------------------------------------------------------------------+
|
||||
int OnStart(void)
|
||||
{
|
||||
//--- call init function
|
||||
if(Init()==0)
|
||||
{
|
||||
//--- cycle until the script is not halted
|
||||
while(!IsStopped())
|
||||
{
|
||||
//--- öèêë ïî îáúåêòàì
|
||||
for(int i=0;i<NUM_SPHERES;i++)
|
||||
Sphere[i].Recalculate();
|
||||
ChartRedraw();
|
||||
Sleep(SleepTime);
|
||||
}
|
||||
}
|
||||
//--- call deinit function
|
||||
Deinit();
|
||||
//---
|
||||
return(0);
|
||||
}
|
||||
//+------------------------------------------------------------------+
|
||||
Reference in New Issue
Block a user