L2.PAS 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264
  1. Uses CRT,GRAPH;
  2. Const
  3. MinX : real = -10;
  4. MaxX : real = -9;
  5. MinY : real = MaxInt;
  6. MaxY : real = -MaxInt;
  7. {---------------------}
  8. LagrTable = 10;
  9. {---------------------}
  10. CStep = 1000;
  11. TVer = 0.01;
  12. ZX : longint = 250;
  13. ZY : longint = 200;
  14. Left = 30;
  15. Right = 400;
  16. Top = 20;
  17. Bottom = 400;
  18. {-----------------------------}
  19. Type
  20. LD = array [0..LagrTable] of record
  21. x,y : real;
  22. end;
  23. TB = array [0..LagrTable] of boolean;
  24. var
  25. Thresh : real;
  26. Pts : LD;
  27. BegiP,
  28. EndiP : TB;
  29. Function Func(x: real): real;
  30. Begin
  31. { Func := ln(abs(x));}
  32. { Func := Sin(x)*x;}
  33. Func := 1.4*x+2;
  34. { Func := X*X/10 * sin (x/2)}
  35. End;
  36. {-----------------------------}
  37. Function Lagr(X:real; var T : TB):real;
  38. var
  39. i,j,n : word;
  40. c,ret : real;
  41. begin
  42. n := LagrTable;
  43. ret:=0;
  44. for i := 0 to n do if T[i] then
  45. begin
  46. c:=1;
  47. for j := 0 to n do
  48. if (j <> i) AND T[j] then C := C * (X-Pts[j].X) / (Pts[i].X-Pts[j].X);
  49. ret := ret + Pts[i].Y*C;
  50. end;
  51. Lagr := ret;
  52. end;
  53. {-----------------------------}
  54. Procedure SetMinMax(var T:tb);
  55. var
  56. c:real;
  57. i:word;
  58. begin
  59. if MaxX < MinX then
  60. begin
  61. C := MaxX;MaxX := MinX;MinX := C;
  62. end;
  63. for i := 0 to Cstep do
  64. begin
  65. C := Lagr(MinX+i/CStep*(MaxX-MinX),T);
  66. if C > MaxY then MaxY:=C;
  67. if C < MinY then MinY:=C;
  68. end;
  69. end;
  70. {-----------------------------}
  71. Procedure InitG;
  72. Var
  73. grDriver: Integer;
  74. grMode: Integer;
  75. i : integer;
  76. Begin
  77. grDriver := Detect;
  78. InitGraph(grDriver, grMode,'');
  79. if GraphResult <> grOk then
  80. begin
  81. WriteLn('Graph error!');
  82. Halt(255);
  83. end;
  84. End;{InitG}
  85. {---------------------------------}
  86. Procedure DrawAxis;
  87. var
  88. i : integer;
  89. s : string;
  90. ZX1,ZY1 : longint;
  91. begin
  92. if MinX > 0 then
  93. ZX := round(Left - MinX/(MaxX-MinX)*(Right-Left))
  94. else
  95. if MaxX < 0 then
  96. ZX := round(Right - MaxX/(MaxX-MinX)*(Right-Left))
  97. else
  98. ZX := round(Left + abs(MinX)/(MaxX-MinX)*(Right-Left));
  99. if MinY > 0 then
  100. ZY := round(Bottom + MinY/(MaxY-MinY)*(Bottom-Top))
  101. else
  102. if MaxY < 0 then
  103. ZY := round(Top + MaxY/(MaxY-MinY)*(Bottom-Top))
  104. else
  105. ZY := round(Bottom - abs(MinY)/(MaxY-MinY)*(Bottom-Top));
  106. if (ZX > Left) AND (ZX < Right) then ZX1 := ZX
  107. else
  108. if ZX < LEFT then ZX1 := Left
  109. else ZX1 := Right;
  110. if (ZY > Top) AND (ZY < Bottom) then ZY1 := ZY
  111. else
  112. if ZY < Top then ZY1 := Top
  113. else ZY1 := Bottom;
  114. SetColor(White);
  115. Line(Left,ZY1,Right+10,ZY1);
  116. Line(Right-5,ZY1-5,Right+10,ZY1);
  117. Line(Right-5,ZY1+5,Right+10,ZY1);
  118. Line(ZX1,Top-10,ZX1,Bottom);
  119. Line(ZX1,Top-10,ZX1-5,Top+5);
  120. Line(ZX1,Top-10,ZX1+5,Top+5);
  121. OutTextXY(ZX1-10,ZY1-10,'0');
  122. OutTextXY(ZX1-15,Top,'Y');
  123. OutTextXY(Right,ZY1+10,'X');
  124. Str(MaxX:0:2,S);
  125. OutTextXY(Right,ZY1-10,S);
  126. Str(MinX:0:2,S);
  127. if MinX<0 then OutTextXY(Left,ZY1-10,S);
  128. Str(MaxY:0:2,S);
  129. OutTextXY(ZX1+10,Top,S);
  130. Str(MinY:0:2,S);
  131. if MinY<0 then OutTextXY(ZX1+10,Bottom,S);
  132. end;
  133. {--------------------}
  134. Procedure LoadData;
  135. var
  136. i : longint;
  137. begin
  138. for i := 0 to LagrTable do
  139. begin
  140. Pts[i].x := MinX + i/LagrTable*(MaxX-MinX);
  141. Pts[i].y := Func(Pts[i].x);
  142. BegiP[i] := true;
  143. EndiP[i] := true;
  144. end;
  145. end;
  146. {--------------------}
  147. Procedure FillTable(var T : TB);
  148. var
  149. i:word;
  150. ax,ay : string;
  151. begin
  152. SetFillStyle(SolidFill,Black);
  153. Bar(440,30,630,380);
  154. SetColor(Cyan);
  155. Rectangle(440,30,630,380);
  156. SetColor(Blue);
  157. for i := 0 to LagrTable do if T[i] then
  158. begin
  159. Str(Pts[i].x:0:2,aX); aX:='X'+Chr(i+$30)+'='+aX;
  160. Str(Pts[i].y:0:2,aY); aY:='Y'+Chr(i+$30)+'='+aY;
  161. SetTextJustify(LeftText,CenterText);
  162. OutTextXY(450,round(50 + i/LagrTable*300),aX);
  163. OutTextXY(550,round(50 + i/LagrTable*300),aY);
  164. end;
  165. end;
  166. {-----------------------------}
  167. Procedure DrawLagr(var T : TB);
  168. Var
  169. C : real;
  170. i : word;
  171. x, y : real;
  172. Begin
  173. { if (MaxY-MinY) > (MaxX-MinX) then
  174. C := (Bottom-Top)/(MaxY-MinY)
  175. else
  176. C := (Right-Left)/(MaxX-MinX);}
  177. if (MaxY-MinY) > (MaxX-MinX) then
  178. begin
  179. if MaxY*MinY < 0 then
  180. C := (Bottom-Top)/(MaxY-MinY)
  181. else
  182. if MaxY < 0 then C:=(Bottom-Top)/abs(MinY)
  183. else C:=(Bottom-Top)/MaxY;
  184. end
  185. else
  186. begin
  187. if MaxX*MinX < 0 then
  188. C := (Right-Left)/(MaxX-MinX)
  189. else
  190. if MaxX < 0 then C := (Right-Left)/abs(MinX)
  191. else C := (Right-Left)/MaxX;
  192. end;
  193. MoveTo(round(ZX + MinX*C),round(ZY-Lagr(MinX,T)*C));
  194. for i := 1 to CStep do
  195. begin
  196. x:=MinX + i/CStep*(MaxX-MinX);
  197. y:=Lagr(X,T);
  198. LineTo(round(ZX+X*C),round(ZY-Y*C));
  199. end;
  200. End;{DrawLagr}
  201. {-------------}
  202. Function Check(var T1, T2 : TB):boolean;
  203. var
  204. i : word;
  205. x, y1, y2 : real;
  206. Begin
  207. Check := true;
  208. for i := 0 to CStep do
  209. begin
  210. x:=MinX + i/CStep*(MaxX-MinX);
  211. y1:=Lagr(X,T1);
  212. y2:=Lagr(X,T2);
  213. if abs(y1-y2) > Thresh then
  214. begin
  215. Check := false;
  216. Exit;
  217. end;
  218. end;
  219. end;
  220. var
  221. CKick : word;
  222. ifDel : boolean;
  223. cS : word;
  224. Begin
  225. LoadData;
  226. InitG;
  227. SetMinMax(BegIp);
  228. Thresh := TVer * (MaxY-MinY);
  229. DrawAxis;
  230. { WriteGraph(CStep);}
  231. FillTable(BegIp);
  232. SetColor(Green);
  233. DrawLagr(BegIp);
  234. CKick :=0;
  235. repeat
  236. ifDel := false;
  237. for cS := 0 to LagrTable do if EndiP[cS] then
  238. begin
  239. EndiP[cS] := false;
  240. if Check(BegiP,EndiP) then
  241. begin
  242. inc(CKick);
  243. ifDel := true;
  244. end
  245. else
  246. EndiP[cs] := true;
  247. end;
  248. until not ifDel;
  249. ReadKey;
  250. FillTable(EndiP);
  251. SetColor(White);
  252. DrawLagr(EndiP);
  253. ReadLn;
  254. CloseGraph;
  255. WriteLn('Выкинуто точек - ',CKick);
  256. End.