: 672e4156 : : 6 2010, 13:57 (bizon_all)
unit Unit1; interface uses Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms, Dialogs, StdCtrls, ExtCtrls; type TFunc = function (x:real): real; //Ñîçäàåì òèï-ôóíêöèþ, äëÿ òîãî //÷òîáû èìÿ ôóíêöèè ìîæíî áûëî ïåðåäàâàòü â ïðîöåäóðó ïîñòðîåíèÿ ãðàôèêà. TForm1 = class(TForm) Image1: TImage; Button1: TButton; Button2: TButton; Button3: TButton; procedure Button1Click(Sender: TObject); procedure Button2Click(Sender: TObject); procedure Button3Click(Sender: TObject); private { Private declarations } public { Public declarations } end; var Form1: TForm1; implementation {$R *.dfm} Function f(x:real):real; begin if x<>-3 then result:=(x+1)/(x+3); end; Function f1(x:real):real; begin Result:=sin(x); end; Function f2(x:real):real; begin result:=exp(-x); end; procedure DrawGraph (f:TFunc; a:integer; b:integer; C: TCanvas); //Ïàðàìåòðû: f - ôóíêöèÿ, ãðàôèê êîòîðîé áóäåì ñòðîèòü. //a - íà÷àëüíîå çíà÷åíèå ïåðåìåííîé "x", b - êîíå÷íîå çíà÷åíèå ïåðåìåííîé "x". //Ñ - êàíâà, íà êîòîðîé áóäåì ðèñîâàòü. var x,y,h,t: real; max, min: real; sx,sy: real; x0,y0,i:integer; begin {Ñ÷èòàåì ìàñøòàáû ïî îñÿì êîîðäèíàò, è ñðåäíåå çíà÷åíèå âûñîòû è øèðèíû êàíâû, ÷òîáû îòîáðàçèòü îñè êîîðäèíàò.} sx:= (c.ClipRect.Right)/(b-a); //øàã ïî x h:=1/sx; // êîîðäèíàòû íà÷àëà êîîðäèíàòíûõ îñåé x0:=c.ClipRect.Right div 2; y0:=c.ClipRect.Bottom div 2; x:=a; max:=f(x); min:=max; while x<=b do begin y:=f(x); if y<min then min:=y; if y>max then max:=y; x:=x+h; end; sy:=c.ClipRect.Bottom/(max-min); //øàã ïî y {çàëèâàåì âñþ êàíâó ñåðåáðÿíûì öâåòîì} c.Brush.Color:=clSilver; c.FillRect(Rect(0,0,c.ClipRect.Right,c.ClipRect.Bottom)); {ðèñóåì ñåòêó} c.Pen.Style:=psdot; c.Pen.Color:=clblue; for i:=a to c.ClipRect.Right do begin c.MoveTo(round(i*sx),0); c.LineTo(round(i*sx),c.ClipRect.Right); end; for i:=a to c.ClipRect.Bottom do begin c.MoveTo(0,Round(i*sy)); c.LineTo(c.ClipRect.Right,round(i*sy)); end; {ðèñóåì æåëòûì öâåòîì îñè êîîðäèíàò} c.Pen.Style:=psSolid; c.Pen.Color:=clYellow; c.MoveTo(0,y0); c.LineTo(c.ClipRect.Right,y0); c.MoveTo(x0,0); c.LineTo(x0,c.ClipRect.Bottom); {ðèñóåì ñòðåëî÷êè} c.MoveTo(x0,0); c.LineTo(x0-10,10); c.MoveTo(x0,0); c.LineTo(x0+10,10); c.MoveTo(c.ClipRect.Right,y0); c.LineTo(c.ClipRect.Right-10,y0-10); c.MoveTo(c.ClipRect.Right,y0); c.LineTo(c.ClipRect.Right-10,y0+10); {íàäïèñè íà îñÿõ êîîðäèíàò} c.TextOut(x0-10,10,'y'); c.TextOut(c.ClipRect.Right-10,y0+10,'x'); {ðèñóåì ãðàôèê ôóíêöèè} x:=a; while x<=b do begin y:=f(x); c.Pixels[x0+round(sx*x), y0-round(sy*y)]:=clWhite; x:=x+h; end; end; procedure TForm1.Button1Click(Sender: TObject); begin DrawGraph(f,-120,120, image1.Canvas); end; procedure TForm1.Button2Click(Sender: TObject); begin DrawGraph(f1,-10,10, image1.Canvas); end; procedure TForm1.Button3Click(Sender: TObject); begin DrawGraph(f2,-3,3, image1.Canvas); end; end.