![]() |
||
| Programme |
Addition
Lineare Gleichung
Sieb des Eratosthenes
|
|
|
Plotter |
Es soll ein einfacher Funktionsplotter geschrieben werden.
Ein Grundproblem der Grafik-Programmierung ist die Transformation sogenannter Welt-Koordinaten (x/y) auf Bildschirm-Koordinaten (u/v). Im Folgenden soll anhand eines Beispiels die Entwicklung der Transformation gezeigt werden:

u = a*x + b und v = c*y + d
gesucht: Koeffizienten a, b, c, d
|
|
III - I:
umax - 0 = a(xmax - xmin) => a = umax/(xmax-xmin)
a in I:
b = - a*xmin
IV - II:
vmax - 0 = c(ymin - ymax) => c = vmax/(ymin-ymax)
c in II:
d = - c*ymax
Das sieht dann so aus:
...
private
a,b,c,d,xmin,xmax,ymin,ymax : real;
procedure berechne_Koeffizienten(pxmin,pxmax,pymin,pymax : real; umax,vmax : integer);
function ut(x : real) : integer;
function vt(y : real) : integer;
public
{ Public-Deklarationen }
...
Das sieht dann so aus:
... procedure TForm1.berechne_Koeffizienten(pxmin,pxmax,pymin,pymax : real; umax,vmax : integer); begin // Grenzen in Attributen 'merken', 'p' steht für 'Parameter' xmin := pxmin; xmax := pxmax; ymin := pymin; ymax := pymax; // Koeffizienten berechnen a := umax/(xmax-xmin); b := -a*xmin; c := vmax/(ymin-ymax); d := -c*ymax; end; function TForm1.ut(x : real) : integer; begin result := Round(a*x+b); end; function TForm1.vt(y : real) : integer; begin result := Round(c*y+d); end; ...
Das sieht dann so aus:
... procedure TForm1.FormCreate(Sender: TObject); begin berechne_Koeffizienten(-2,4,-1,3,639,479); // iBild.width wurde auf 640, iBild.height auf 480 eingestellt end; ...
Die Ereignisbehandlungsroutine könnte etwa so aussehen:
procedure TForm1.bZeichneAchsenClick(Sender: TObject); begin iBild.canvas.MoveTo(ut(-2),vt(0)); iBild.canvas.LineTo(ut(4),vt(0)); // .... end;
procedure TForm1.Linie(x1,y1,x2,y2 : real); // zeichnet eine Linie von (x1/y1) nach (x2/y2) begin iBild.canvas.MoveTo(ut(x1),vt(y1)); iBild.canvas.LineTo(ut(x2),vt(y2)); end;