// Teilkreis
(Tortenstück, Pie) mittels Prozent- oder Winkelangabe zeichnen. procedure DrawPie(cnv: TCanvas; x1, y1, x2, y2: integer; background, PiePiece, Border: TColor; Value: single); var w, h, dx, dy, x, y, bx, by: integer; begin with cnv do begin brush.color := background; pen.color := border; dx := x2 - x1; dy := y2 - y1; w := dx div 2; h := dy div 2; bx := x1 + dx; by := y1 + dy; fillrect(rect(x1, y1, bx, by)); brush.color := PiePiece; x := round(w * sin(value) + w); y := round(-h * cos(value) + h); // --------------- bei Bedarf --------------------------- if (w = x) and (y = 0) then begin if value > 0 then ellipse(x1, y1, bx, by); end else // ------------------------------------------------------ pie(x1, y1, bx, by, x + x1, y + y1, w + x1, y1); end; end; procedure DrawPiePercent(cnv: TCanvas; x1, y1, x2, y2: integer; Hintergrund, PiePart, Border: TColor; Percent: single); var value: single; begin if (Percent > 100) or (Percent < 0) then raise exception.create('Prozentangabe muss zwischen 0 und 100 liegen.'); value := (Percent / 50) * pi; DrawPie(cnv, x1, y1, x2, y2, Hintergrund, PiePart, Border, value); end; procedure DrawPieAngle(cnv: TCanvas; x1, y1, x2, y2: integer; Hintergrund, PiePart, Border: TColor; Angle: single); var value: single; begin while Angle < 0 do Angle := Angle + 360; while Angle > 360 do Angle := Angle - 360; value := (Angle / 180) * pi; DrawPie(cnv, x1, y1, x2, y2, Hintergrund, PiePart, Border, value); end; procedure DrawPieCommon(cnv: TCanvas; x1, y1, x2, y2: integer; Hintergrund, PiePart, Border: TColor; min, max, position: single); var dff, value: single; begin dff := (max - min) / 2; value := ((position - min) / dff) * pi; DrawPie(cnv, x1, y1, x2, y2, Hintergrund, PiePart, Border, value); end; // Beispielaufrufe procedure TForm1.Button3Click(Sender: TObject); begin DrawPieAngle(canvas, 160, 10, 220, 100, color, clLime, clBlack, 276.9); end; procedure TForm1.Button5Click(Sender: TObject); begin DrawPiePercent(canvas, 220, 10, 350, 100, color, clYellow, clBlue, 66.67); end; procedure TForm1.TrackBar1Change(Sender: TObject); begin DrawPieCommon(canvas, 20, 10, 110, 100, color, clRed, color, Trackbar1.min, Trackbar1.max, Trackbar1.position); end; |
Zugriffe seit 6.9.2001 auf Delphi-Ecke