// img must be square!

procedure DrawMoon (Img: TImage; Phase : single);

var

radius,MidXY: Integer;

p1,p2,p3,p4 : TPointF;

tmp : TBGRABitMap;

Xpos, YPos, Rpos, Xpos1, Xpos2, Percent: single;

R : Trect;

sign : String;

begin

tmp := TBGRABitmap.create(Img.Width,Img.Height,cssBlack);

MidXY := Img.width div 2;

radius := MidXY - 4;

// Ganzer Mond in Blau

tmp.FillEllipseAntialias(MidXY,MidXY,radius,radius,CssBlue);

tmp.EllipseAntialias(MidXY, MidXY,radius,radius,CssYellow,0.5);

tmp.LineCap := pecRound;

Ypos := 0.0;

// Gelben Teil des Mondes zeichnen - yellow part of moon

while YPos <= radius do

begin

Xpos := Sqrt (sqr(radius) - sqr(Ypos));

RPos := 2*Xpos;

if Phase < 0.5 then // abnehmed

begin

Xpos1 := -Xpos;

XPos2 := Rpos-2*Phase*Rpos-Xpos;

end else // zunehmend

begin

XPos1 := +XPos;

XPos2 := Rpos-2*Phase*Rpos+Xpos;

end;

p1 := PointF (Xpos1+MidXY, MidXY-Ypos);

p2 := PointF (XPos2+MidXY, MidXY-Ypos);

p3 := PointF (XPos1+MidXY, MidXY+YPos);

p4 := PointF (XPos2+MidXY, MidXY+YPos);

tmp.DrawLineAntialias(p1.x,p1.y,p2.x,p2.y,cssYellow,1);

tmp.DrawLineAntialias(p3.x,p3.y,p4.x,p4.y,cssYellow,1);

yPos := yPos + 0.5;

end;// Ypos

R := Rect (0,0,Img.Width,Img.Height);

Img.Canvas.CopyRect(R,tmp.Canvas,R);

tmp.free;

if phase > 0.5 then sign := '[+]' else sign := '[-]';

if phase > 0.5 then Percent := 1.0 - phase else Percent := phase;

Percent := 100 -Percent * 100 *2;

Img.Hint := 'Moon '+ intToStr(round(Percent))+'% '+sign;

end;