sobota, 21 lutego 2026

Moon phases - C++ Builder

 


private: // User declarations
    double __fastcall get_moon_phase(int year, int month, int day);
    void __fastcall DrawMoon(TCanvas *Canvas, int x, int y, int radius, double phase);

#include <vcl.h>
#include <System.SysUtils.hpp>
#include <time.h>
#include <math.h>
#pragma hdrstop

#include "p_Moon.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"
TForm1 *Form1;
//---------------------------------------------------------------------------
__fastcall TForm1::TForm1(TComponent* Owner)
: TForm(Owner)
{
}
double __fastcall TForm1::get_moon_phase(int year, int month, int day)
{


if (month < 3) {
year--;
month += 12;
}
int A = year / 100;
int B = A / 4;
int C = 2 - A + B;
int E = (int)(365.25 * (year + 4716));
int F = (int)(30.6001 * (month + 1));
double jd = C + day + E + F - 1524.5;

double known_new_moon = 2451550.26;
double lunar_cycle = 29.530588853;
double days_since_new = jd - known_new_moon;
double cycles = days_since_new / lunar_cycle;

double phase = cycles - (long)cycles;
if (phase < 0) phase += 1;

return phase;

}
void __fastcall TForm1::DrawMoon(TCanvas *Canvas, int x, int y, int radius, double phase)
{

Canvas->Pen->Style = psSolid;
Canvas->Pen->Color = clGray;
Canvas->Brush->Color = clBlack;
Canvas->Brush->Style = bsSolid;
Canvas->Ellipse(x - radius, y - radius, x + radius, y + radius);


Canvas->Pen->Color = clYellow;
Canvas->Brush->Color = clYellow;


for (int i = -radius; i <= radius; i++) {

double w = sqrt((double)radius * radius - (double)i * i);


double terminatorX = w * cos(2.0 * M_PI * phase);

int xStart, xEnd;

if (phase <= 0.5) {

xStart = (int)(x + terminatorX);
xEnd   = (int)(x + w);
} else {

xStart = (int)(x - w);
xEnd   = (int)(x + terminatorX);
}


if (xStart < xEnd) {
Canvas->MoveTo(xStart, y + i);
Canvas->LineTo(xEnd, y + i);
}
}


Canvas->Brush->Style = bsClear;
Canvas->Pen->Color = clGray;
Canvas->Ellipse(x - radius, y - radius, x + radius, y + radius);
}
//---------------------------------------------------------------------------
void __fastcall TForm1::Timer1Timer(TObject *Sender)
{
  Label1->Caption=DateToStr(Date());
  Label4->Caption=TimeToStr(Time());

}
//---------------------------------------------------------------------------
void __fastcall TForm1::FormCreate(TObject *Sender)
{
 Button1->Caption="&Start";
 Button2->Caption="&Close";
}
//---------------------------------------------------------------------------
void __fastcall TForm1::Button2Click(TObject *Sender)
{
 Close();
}
//---------------------------------------------------------------------------
void __fastcall TForm1::Button1Click(TObject *Sender)
{
 int y,d,m;
 double phase, illumination;
 Word year,month,day;
 TDateTime today=Date();
 DecodeDate(today,year,month,day);
 y=(int)year;
 d=(int)day;
 m=(int)month;
 phase=get_moon_phase(y, m, d);
 illumination = (1 - cos(2 * M_PI * phase)) / 2 * 100;
 Label2->Caption="Lighting: "+FloatToStrF(illumination,ffGeneral,4,2)+"%";
 DrawMoon(Canvas, 200, 200, 100, phase);

}


środa, 18 lutego 2026

Valarray - C++ Builder

 

    


private: // User declarations

float tab[5];

#include <vcl.h>

#include <valarray>

#include <stdlib.h>

#include <time.h>

#pragma hdrstop


#include "p_aarray.h"

//---------------------------------------------------------------------------

#pragma package(smart_init)

#pragma resource "*.dfm"

TForm1 *Form1;

//---------------------------------------------------------------------------

__fastcall TForm1::TForm1(TComponent* Owner)

: TForm(Owner)

{

 srand(time(NULL));

}

//---------------------------------------------------------------------------

void __fastcall TForm1::FormCreate(TObject *Sender)

{

 StringGrid1->ColCount=5;

 StringGrid1->RowCount=1;

 StringGrid1->FixedCols=0;

 StringGrid1->FixedRows=0;

}

//---------------------------------------------------------------------------

void __fastcall TForm1::Button1Click(TObject *Sender)

{

 int i;

 const float a=50.00;

 for(i=0;i<5;i++)

 {

tab[i]=(1+rand()%100)/a;

StringGrid1->Cells[i][0]=FloatToStrF(tab[i],ffGeneral,4,4);

 }

 std::valarray<float> bb {tab[0],tab[1], tab[2], tab[3], tab[4]};

 Label1->Caption="sum() = "+FloatToStrF(bb.sum(),ffGeneral,4,4);

 Label2->Caption="min() = "+FloatToStrF(bb.min(),ffGeneral,4,4);

 Label3->Caption="max() = "+FloatToStrF(bb.max(),ffGeneral,4,4);

 std::valarray<float> temp_Sqrt=sqrt(bb);

 Label4->Caption="sqrt() = "+FloatToStrF(temp_Sqrt[0],ffGeneral,4,4)+"  "+

 FloatToStrF(temp_Sqrt[1],ffGeneral,4,4)+"  "+

 FloatToStrF(temp_Sqrt[2],ffGeneral,4,4)+"  "+

 FloatToStrF(temp_Sqrt[3],ffGeneral,4,4)+"  "+

 FloatToStrF(temp_Sqrt[4],ffGeneral,4,4)+"  ";


}