Listing 14


//Erstellen eines 3D Tortendiagramms

//die Grafik verdeutlicht das Vorgehen

Torte





unit Torte;

interface

uses
  Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs;

type
  TForm1 = class(TForm)
    procedure FormPaint(Sender: TObject);
  private
    { Private-Deklarationen }
  public
    { Public-Deklarationen }
  end;

const
  Colors: array[0..10] of TColor = (clRed, clGreen, clBlue, clYellow,
                                     clMaroon, clTeal, clNavy, clOlive,
                                     clPurple, clLime, clFuchsia);

var
  Form1: TForm1;

implementation

{$R *.DFM}

function Min(a,b: Integer): LongInt;
begin
if a<=b then result:=a else result:=b;
end;

function Max(a,b: Integer): LongInt;
begin
if a>b then result:=a else result:=b;
end;

function GetArcPoint(cPoint: TPoint; radius, angle: Extended): TPoint;
begin
result.x:=Round(cPoint.x + radius * Cos(angle * 2 * pi / 360));
result.y:=Round(cPoint.y - radius * Sin(angle * 2 * pi / 360));
end;

function GetCentrePoint(oRect: TRect): TPoint;
begin
result.x:=oRect.left + (oRect.right-oRect.left) div 2;
result.y:=oRect.top + (oRect.bottom-oRect.top) div 2;
end;

procedure TForm1.FormPaint(Sender: TObject);
var cv, vv, za, va, co, sum, radius, angle, valuecount, valuesum: Integer;
    middlepoint, copypoint: TPoint;
    values, angles: array[0..10] of Integer;
    CirclePoints: array[0..365] of TPoint;
 
begin
values[0]:=15;
values[1]:=25;
values[2]:=20;
values[3]:=15;
values[4]:=15;
valuecount:=5;
valuesum:=0; for cv:=0 to valuecount-1 do valuesum:=valuesum+values[cv];

Canvas.Brush.Color:=clWhite;
Canvas.FillRect(ClientRect);

Canvas.Pen.Color:=clBlack;

radius:=(Min(width, height)-40) div 2;
middlepoint:=GetCentrePoint(ClientRect);

//Werte in Winkel konv.
for cv:=0 to valuecount-1 do
   angles[cv]:=Round(values[cv]*360/valuesum);

//Tortenrand ausgeben
za:=0; sum:=angles[0]; va:=1; co:=0;
for angle:=0 to 360 do
begin
   if angle<180 then
   begin
    if angle=sum then begin inc(co); sum:=sum+angles[va]; inc(va); end;
    Continue;
   end;

   CirclePoints[za]:=GetArcPoint(middlepoint, radius, angle);
   CirclePoints[za].y:=middlepoint.y-(middlepoint.y-CirclePoints[za].y) div 2 + radius div 5; //Tortenhoehe 20% des Radius

   if (angle=sum) or (angle=360) then
   begin
      Canvas.Brush.Color:=Colors[co]; inc(co);

      copypoint:=CirclePoints[za];
      CirclePoints[za+1].x:=CirclePoints[za].x;
      CirclePoints[za+1].y:=middlepoint.y;
      CirclePoints[za+2].x:=CirclePoints[0].x;
      CirclePoints[za+2].y:=middlepoint.y;
      Polygon(Canvas.Handle, CirclePoints[0], za+3);
      CirclePoints[0]:=copypoint;

      sum:=sum+angles[va]; inc(va);

      za:=1;
      Continue;
   end;

   inc(za);
end;
 
//obere Tortenfläche ausgeben
za:=0; sum:=angles[0]; va:=1; co:=0;
for angle:=0 to 360 do
begin
   CirclePoints[za]:=GetArcPoint(middlepoint, radius, angle);
   CirclePoints[za].y:=middlepoint.y-(middlepoint.y-CirclePoints[za].y) div 2;

   if (angle=sum) or (angle=360) then
   begin
      Canvas.Brush.Color:=Colors[co]; inc(co);
 
      copypoint:=CirclePoints[za];
      CirclePoints[za+1]:=middlepoint;
      Polygon(Canvas.Handle, CirclePoints[0], za+2);
      CirclePoints[0]:=copypoint;

      sum:=sum+angles[va]; inc(va);

      za:=1;
      Continue;
   end;

   inc(za);
end;

end;

end.

Zurück zur Hauptseite