Delphi-int.ru:

| ?

: 672e4156 : : 6 2010, 13:57 (bizon_all)

  1. unit Unit1;
  2.  
  3. interface
  4.  
  5. uses
  6. Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  7. Dialogs, StdCtrls, ExtCtrls;
  8.  
  9. type
  10. TFunc = function (x:real): real; //Ñîçäàåì òèï-ôóíêöèþ, äëÿ òîãî
  11. //÷òîáû èìÿ ôóíêöèè ìîæíî áûëî ïåðåäàâàòü â ïðîöåäóðó
  12. ïîñòðîåíèÿ ãðàôèêà.
  13.  
  14. TForm1 = class(TForm)
  15. Image1: TImage;
  16. Button1: TButton;
  17. Button2: TButton;
  18. Button3: TButton;
  19. procedure Button1Click(Sender: TObject);
  20. procedure Button2Click(Sender: TObject);
  21. procedure Button3Click(Sender: TObject);
  22. private
  23. { Private declarations }
  24. public
  25. { Public declarations }
  26. end;
  27.  
  28. var
  29. Form1: TForm1;
  30.  
  31. implementation
  32.  
  33. {$R *.dfm}
  34.  
  35. Function f(x:real):real;
  36. begin
  37. if x<>-3 then result:=(x+1)/(x+3);
  38. end;
  39.  
  40. Function f1(x:real):real;
  41. begin
  42. Result:=sin(x);
  43. end;
  44.  
  45. Function f2(x:real):real;
  46. begin
  47. result:=exp(-x);
  48. end;
  49.  
  50. procedure DrawGraph (f:TFunc; a:integer; b:integer; C: TCanvas);
  51. //Ïàðàìåòðû: f - ôóíêöèÿ, ãðàôèê êîòîðîé áóäåì ñòðîèòü.
  52. //a - íà÷àëüíîå çíà÷åíèå ïåðåìåííîé "x", b - êîíå÷íîå çíà÷åíèå
  53. ïåðåìåííîé "x".
  54. //Ñ - êàíâà, íà êîòîðîé áóäåì ðèñîâàòü.
  55. var x,y,h,t: real;
  56. max, min: real;
  57. sx,sy: real;
  58. x0,y0,i:integer;
  59. begin
  60. {Ñ÷èòàåì ìàñøòàáû ïî îñÿì êîîðäèíàò, è ñðåäíåå çíà÷åíèå âûñîòû è
  61. øèðèíû êàíâû, ÷òîáû îòîáðàçèòü îñè êîîðäèíàò.}
  62. sx:= (c.ClipRect.Right)/(b-a); //øàã ïî x
  63. h:=1/sx;
  64. // êîîðäèíàòû íà÷àëà êîîðäèíàòíûõ îñåé
  65. x0:=c.ClipRect.Right div 2;
  66. y0:=c.ClipRect.Bottom div 2;
  67. x:=a;
  68. max:=f(x);
  69. min:=max;
  70. while x<=b do
  71. begin
  72. y:=f(x);
  73. if y<min then min:=y;
  74. if y>max then max:=y;
  75. x:=x+h;
  76. end;
  77. sy:=c.ClipRect.Bottom/(max-min); //øàã ïî y
  78.  
  79. {çàëèâàåì âñþ êàíâó ñåðåáðÿíûì öâåòîì}
  80.  
  81. c.Brush.Color:=clSilver;
  82. c.FillRect(Rect(0,0,c.ClipRect.Right,c.ClipRect.Bottom));
  83.  
  84. {ðèñóåì ñåòêó}
  85.  
  86. c.Pen.Style:=psdot;
  87. c.Pen.Color:=clblue;
  88. for i:=a to c.ClipRect.Right do
  89. begin
  90. c.MoveTo(round(i*sx),0);
  91. c.LineTo(round(i*sx),c.ClipRect.Right);
  92. end;
  93. for i:=a to c.ClipRect.Bottom do
  94. begin
  95. c.MoveTo(0,Round(i*sy));
  96. c.LineTo(c.ClipRect.Right,round(i*sy));
  97. end;
  98. {ðèñóåì æåëòûì öâåòîì îñè êîîðäèíàò}
  99. c.Pen.Style:=psSolid;
  100. c.Pen.Color:=clYellow;
  101. c.MoveTo(0,y0);
  102. c.LineTo(c.ClipRect.Right,y0);
  103. c.MoveTo(x0,0);
  104. c.LineTo(x0,c.ClipRect.Bottom);
  105. {ðèñóåì ñòðåëî÷êè}
  106. c.MoveTo(x0,0); c.LineTo(x0-10,10);
  107. c.MoveTo(x0,0); c.LineTo(x0+10,10);
  108. c.MoveTo(c.ClipRect.Right,y0); c.LineTo(c.ClipRect.Right-10,y0-10);
  109. c.MoveTo(c.ClipRect.Right,y0); c.LineTo(c.ClipRect.Right-10,y0+10);
  110. {íàäïèñè íà îñÿõ êîîðäèíàò}
  111. c.TextOut(x0-10,10,'y');
  112. c.TextOut(c.ClipRect.Right-10,y0+10,'x');
  113. {ðèñóåì ãðàôèê ôóíêöèè}
  114. x:=a;
  115. while x<=b do
  116. begin
  117. y:=f(x);
  118. c.Pixels[x0+round(sx*x), y0-round(sy*y)]:=clWhite;
  119. x:=x+h;
  120. end;
  121. end;
  122.  
  123. procedure TForm1.Button1Click(Sender: TObject);
  124. begin
  125. DrawGraph(f,-120,120, image1.Canvas);
  126. end;
  127.  
  128. procedure TForm1.Button2Click(Sender: TObject);
  129. begin
  130. DrawGraph(f1,-10,10, image1.Canvas);
  131. end;
  132.  
  133. procedure TForm1.Button3Click(Sender: TObject);
  134. begin
  135. DrawGraph(f2,-3,3, image1.Canvas);
  136. end;
  137.  
  138. end.

:

»