unit U_ClockAngles;

{Copyright  © 2001-2003, Gary Darby,  www.DelphiForFun.org
 This program may be used or modified for any non-commercial purpose
 so long as this original notice remains in place.
 All other rights are reserved
 }

interface

uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
  StdCtrls, NumEdit, ExtCtrls, ComCtrls, ShellAPI;

type
TForm1 = class(TForm)
    LHA: TLabel;
    LMA: TLabel;
    LDa: TLabel;
    Label1: TLabel;
    Label2: TLabel;
    Label3: TLabel;
    Label4: TLabel;
    Label5: TLabel;
    Image1: TImage;
    Edit1: TEdit;
    Edit2: TEdit;
    HourVal: TUpDown;
    MinuteVal: TUpDown;
    Memo1: TMemo;
    Button1: TButton;
    CountLbl: TLabel;
    StaticText1: TStaticText;
procedure ValClick(Sender: TObject; Button: TUDBtnType);
procedure FormActivate(Sender: TObject);
procedure Button1Click(Sender: TObject);
procedure StaticText1Click(Sender: TObject);
public
center:TPoint;
    mhandlen,hhandlen:integer;
    dotloc,i:integer;
    lastmn,lasthr:TPoint;
    hourangle,minuteangle,difangle:single;
    oldangle:single;
    count:integer;
procedure timechanged;
procedure drawtime(hourangle,minuteangle:single);
end;

var
Form1: TForm1;

implementation

{$R *.DFM}

{*************** TimeChanged ***********}
procedure TForm1.TimeChanged;
{Update all displays}
var
hour,minute:integer;
  hourtime,minutetime:single;

begin
if minuteval.position>59 then
begin
minuteval.position:=minuteval.position mod 60;
    hourval.position:=hourval.position+1;
end;
if hourval.position>12 then hourval.position:=hourval.position-12;
  hour:=hourval.position mod 12;
  minute:=minuteval.position;
  hourtime:=(hour+minute/60)/12;
  hourangle:=hourtime*360;
if hourangle<0.01 then hourangle:=0;
  minutetime:=minute/60;
  minuteangle:=minutetime*360;
if minuteangle<0.01 then minuteangle:=0;
  difangle:={abs}(minuteangle-hourangle);
if difangle>180 then difangle:=difangle-360;
if difangle<= -180 then difangle:=difangle+360;
  lha.caption:=floattostrf(hourangle,ffgeneral,5,2);
  lma.caption:=floattostrf(minuteangle,ffgeneral,5,2);
  lda.caption:=floattostrf(difangle,ffgeneral,5,2);
  drawtime(hourangle,minuteangle);
end;

{***************** DrawTime ****************}
procedure TForm1.drawtime(hourangle,minuteangle:single);
{Redraw the clock}
begin
with image1.picture.bitmap{clock},canvas do
begin
{erase old hands}
pen.width:=1;
    pen.color:=clwhite;
    moveto(center.x,center.y);
    lineto(lastmn.x,lastmn.y);
    pen.width:=3;
    moveto(center.x,center.y);
    lineto(lasthr.x,lasthr.y);
{drawnew hands}
pen.width:=1;
    pen.color:=clblack;
    moveto(center.x,center.y);
    lastmn.x:=trunc(center.x+mhandlen*cos((minuteangle-90)*pi/180));
    lastmn.y:=trunc(center.y+mhandlen*sin((minuteangle-90)*pi/180));
    lineto(lastmn.x,lastmn.y);
    pen.width:=3;
    moveto(center.x,center.y);
    lasthr.x:=trunc(center.x+hhandlen*cos((hourangle-90)*pi/180));
    lasthr.y:=trunc(center.y+hhandlen*sin((hourangle-90)*pi/180));
    lineto(lasthr.x,lasthr.Y);
end;
end;

{************** ValClick **************}
procedure TForm1.ValClick(Sender: TObject; Button: TUDBtnType);
{Hour or minute changed - update displays}
begin
timechanged;
end;

{********** FormActivate **********}
procedure TForm1.FormActivate(Sender: TObject);
var i:integer;
begin
countlbl.caption:='';
with image1.picture.bitmap,canvas do
begin
width:=image1.width;
    height:=image1.height;
    center.x:=image1.picture.bitmap.width div 2;
    center.y:=height div 2;
    mhandLen:=3*width div 8;
    hhandlen:= width div 4;
    canvas.ellipse(0,0,width,height);
    dotloc:=9*width div 20;
for i:= 1 to 12 do
ellipse(trunc(center.x+dotloc*cos(2*pi*i/12)-3),
              trunc(center.y+dotloc*sin(2*pi*i/12)-3),
              trunc(center.x+dotloc*cos(2*pi*i/12)+3),
              trunc(center.y+dotloc*sin(2*pi*i/12)+3)  );
    pen.width:=1;
    lastmn:=center;
    lasthr:=center;
end;
  timechanged;
  doublebuffered:=true;
end;

{**************** Buton1Click *************}
procedure TForm1.Button1Click(Sender: TObject);
var m:integer;

begin
button1.enabled:=false;
    oldangle:=0;
    count:=0;
for m:=0 to 12*60 do
begin
sleep(50);
      application.processmessages;
      minuteval.position:=(m-1) mod 60+1;

      timechanged;
if ((oldangle<=90) and (difangle>90))
or  ((oldangle<=-90) and (difangle>-90))
then
begin
inc(count);
       countLbl.caption := inttostr(count)+' times';
end;
      oldangle:=difangle;
end;
    countLbl.caption := inttostr(count)+' times  in 12 hours';
    button1.enabled:=true;
end;

{************** StaticText1Click ************}
procedure TForm1.StaticText1Click(Sender: TObject);
begin
ShellExecute(Handle, 'open', 'http://www.delphiforfun.org/',
nil, nil, SW_SHOWNORMAL) ;
end;

end.