Nadesłany przez Tomasz Lubiński, 15 maja 2007 01:00
Kod przedstawiony poniżej przedstawia główną część rozwiązania problemu.Pobierz pełne rozwiązanie.
Jeżeli nie odpowiada Ci sposób formatowania kodu przez autora skorzystaj z pretty printer'a i dostosuj go automatycznie do siebie.
Elipsa - Delphi/Unit1.pas:
//Tomasz Lubiński (C)2007 //www.algorytm.org //Algorytm kreslenia elipsy unit Unit1; interface uses Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs, StdCtrls, ComCtrls, ExtCtrls; type TForm1 = class(TForm) Image: TImage; Label4: TLabel; Label1: TLabel; Label2: TLabel; Label3: TLabel; Edit1: TEdit; UpDown1: TUpDown; Button1: TButton; Edit2: TEdit; UpDown2: TUpDown; Edit3: TEdit; UpDown3: TUpDown; Edit4: TEdit; UpDown4: TUpDown; procedure Button1Click(Sender: TObject); procedure ElipsePoints(x: Integer; y: Integer; x_move: Integer; y_move: Integer); private { Private declarations } public { Public declarations } end; var Form1: TForm1; implementation {$R *.DFM} procedure TForm1.ElipsePoints(x: Integer; y: Integer; x_move: Integer; y_move: Integer); begin Form1.Image.Canvas.Pixels[x+x_move, y+y_move] := clBlack; Form1.Image.Canvas.Pixels[x+x_move, -y+y_move] := clBlack; Form1.Image.Canvas.Pixels[-x+x_move, -y+y_move] := clBlack; Form1.Image.Canvas.Pixels[-x+x_move, y+y_move] := clBlack; end; procedure TForm1.Button1Click(Sender: TObject); var x, y, limit, d: Integer; x_move, y_move, radius_a, radius_b, radius_a2, radius_b2: Integer; begin x_move := StrToInt(Edit1.Text); y_move := StrToInt(Edit2.Text); radius_a := StrToInt(Edit3.Text); radius_b := StrToInt(Edit4.Text); radius_a2 := radius_a*radius_a; radius_b2 := radius_b*radius_b; d := 4*radius_b2 - 4*radius_b*radius_a2 + radius_a2; limit := (radius_a2*radius_a2) div (radius_a2+radius_b2); x := 0; y := radius_b; while (x*x < limit) do begin ElipsePoints(x,y, x_move,y_move); if (d > 0) then begin d := d + 8*radius_b2*x + 12*radius_b2 - 8*radius_a2*y + 8*radius_a2; x := x + 1; y := y - 1; end else begin d := d + 8*radius_b2*x + 12*radius_b2; x := x + 1; end; end; radius_a := StrToInt(Edit4.Text); radius_b := StrToInt(Edit3.Text); radius_a2 := radius_a*radius_a; radius_b2 := radius_b*radius_b; d := 4*radius_b2 - 4*radius_b*radius_a2 + radius_a2; limit := (radius_a2*radius_a2) div (radius_a2+radius_b2); x := 0; y := radius_b; while (x*x < limit) do begin ElipsePoints(y,x, x_move,y_move); if (d > 0) then begin d := d + 8*radius_b2*x + 12*radius_b2 - 8*radius_a2*y + 8*radius_a2; x := x + 1; y := y - 1; end else begin d := d + 8*radius_b2*x + 12*radius_b2; x := x + 1; end; end; end; end.