aboutsummaryrefslogtreecommitdiff
path: root/Занимательное программирование/1/6_solar_system/unit1.pas
blob: c5e404ea4da1bef95ee68012c67fca326bb8bcd8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
unit Unit1;

{$mode objfpc}{$H+}

interface

uses
  Classes, SysUtils, Forms, Controls, Graphics, Dialogs, ExtCtrls, StdCtrls;

type

  { TForm1 }

  TForm1 = class(TForm)
    StartStopBtn: TButton;
    Screen: TPaintBox;
    procedure ScreenPaint(Sender: TObject);
    procedure StartStopBtnClick(Sender: TObject);
  private
    var EarthX, EarthY: Integer; { Координаты земли }
      EarthA: Real; { Текущий угол отклонения Земли }
      MoonX, MoonY: Integer; { Координаты Луны }
      MoonA: Real; { Текущий угол отклонения Луны }
      IsRunning: Boolean;

    const SunR: Integer = 60; { Радиус Солнца }
      EarthR: Integer = 20; { Радиус Земли }
      MoonR: Integer = 4; { Радиус Луны }
      EarthD: Integer = 140; { Расстояние от Солнца до Земли }
      MoonD: Integer = 40; { Радиус от Земли до Луны }
      EarthV: Real = 0.02; { Угловая скорость Земли }
      MoonV: Real = 0.1; { Угловая скорость Луны }
  public

  end;

var
  Form1: TForm1;

implementation

{$R *.lfm}

{ TForm1 }

procedure TForm1.StartStopBtnClick(Sender: TObject);
begin
  if IsRunning then
  begin
    IsRunning := false;
    StartStopBtn.Caption := 'Пуск';
    Exit;
  end;

  StartStopBtn.Caption := 'Стоп';
  IsRunning := true;

  { Инициализация переменных }
  EarthA := 0;
  MoonA := 0;
  EarthX := 0;
  EarthY := 0;
  MoonX := 0;
  MoonY := 0;

  while IsRunning do { Основной цикл }
  begin
    { Пересчитаем координаты Земли и Луны }
    EarthX := Round(Screen.Width div 2 + EarthD * Cos(EarthA));
    EarthY := Round(Screen.Height div 2 + EarthD * Sin(EarthA));
    MoonX := Round(EarthX + MoonD * Cos(MoonA));
    MoonY := Round(EarthY + MoonD * Sin(MoonA));

    EarthA := EarthA + EarthV; { Изменяем текущие углы отклонения }
    MoonA :=  MoonA + MoonV; { Земли и Луны }

    if EarthA > 2 * PI then EarthA := EarthA - 2 * PI; { Корректируем углы }
    if MoonA > 2 * PI then MoonA := MoonA - 2 * PI; { если требуется }

    Screen.Invalidate;
    Sleep(30); { Пауза }
    Application.ProcessMessages;
    if Application.Terminated then break;
  end;
end;

procedure TForm1.ScreenPaint(Sender: TObject);
begin
  if not IsRunning then Exit;

  Screen.Canvas.Pen.Width := 5; { Толщина линий - 5 пикселей }
  Screen.Canvas.Pen.Color := clRed; { Рисуем Солнце в центре экрана }
  Screen.Canvas.Ellipse(Screen.Width div 2 - SunR,
    Screen.Height div 2 - SunR,
    Screen.Width div 2 + SunR,
      Screen.Height div 2 + SunR);

  Screen.Canvas.Pen.Color := clGreen; { Рисуем Землю }
  Screen.Canvas.Ellipse(EarthX - EarthR, EarthY - EarthR,
    EarthX + EarthR, EarthY + EarthR); { и Луну }
  Screen.Canvas.Pen.Color := clBlue;
  Screen.Canvas.Ellipse(MoonX - MoonR, MoonY - MoonR,
    MoonX + MoonR, MoonY + MoonR);
end;

end.