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);

}


Brak komentarzy:

Prześlij komentarz