trap.pas 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. Uses CRT,Graph;
  2. CONST
  3. minX = 59.0;
  4. maxX = 60.0;
  5. ifWrite = true;
  6. LenX = maxX-minX;
  7. writeFrames = 1000;
  8. BegX = 80;
  9. BegY = 400;
  10. SIZE_Y = 320;
  11. SIZE_X = 340;
  12. Eps = 0.0001;
  13. {---------------------------------------------------------------}
  14. VAR
  15. Area : real;
  16. maxY : real;
  17. Ntr : longint;
  18. {---------------------------------------------------------------}
  19. Function F_X(X:real):real;
  20. begin
  21. { F_X := sqrt(X)*(x);}
  22. { F_X := sqrt(40000-x*x);}
  23. F_X := sin(49*x*PI/180)*cos(12.5*x*PI/180)+1;
  24. { F_X := sin(X)*cos(X*PI/180)+1;}
  25. { F_X := 1/x+1;}
  26. end;{F_X}
  27. {---------------------------------------------------------------}
  28. Procedure Init;
  29. var
  30. grDriver : Integer;
  31. grMode : Integer;
  32. ErrCode : Integer;
  33. value : String;
  34. i : word;
  35. cur : real;
  36. begin
  37. grDriver := Detect;
  38. InitGraph(grDriver, grMode,' ');
  39. ErrCode := GraphResult;
  40. if ErrCode <> grOk then
  41. begin
  42. Writeln('Graphics error: ', GraphErrorMsg(ErrCode));
  43. Halt(ErrCode);
  44. end;
  45. SetFillStyle(XHatchFill,Blue);
  46. Bar(0,0,GetMaxX,GetMaxY);
  47. SetFillStyle(SolidFill,Black);
  48. Bar(10,10,GetMaxX-10,GetMaxY-10);
  49. SetColor(Green);
  50. Rectangle(10,10,500,469);
  51. SetColor(white);
  52. Line(79,401,79,81);
  53. Line(79,81,75,88);
  54. Line(79,81,83,88);
  55. Line(79,401,419,401);
  56. Line(419,401,412,405);
  57. Line(419,401,412,397);
  58. SetTextJustify(CenterText,TopText);
  59. Str(minX:0:0,value);
  60. OutTextXY(79,408,value);
  61. Str(maxX:0:0,value);
  62. OutTextXY(419,408,value);
  63. SetTextJustify(RightText,CenterText);
  64. maxY := -MaxInt;
  65. for i := 0 to 1000 do
  66. begin
  67. cur := F_X(((lenX*i)/1000)+minx);
  68. if cur > maxY then
  69. maxY := cur;
  70. { if cur < minY then
  71. minY := cur; }
  72. end;
  73. Str(maxY:0:2,value);
  74. OutTextXY(76,85,value);
  75. OutTextXY(76,401,'0');
  76. end;{Init}
  77. {--------------------------------------------------------------}
  78. Procedure WriteGraph;
  79. var
  80. i:word;
  81. x:real;
  82. y:real;
  83. begin
  84. SetColor(Magenta);
  85. MoveTo(BegX,round(BegY- ( (F_X(minX)/maxY)*SIZE_Y) ));
  86. for i := 1 to WriteFrames do
  87. begin
  88. x:=(lenX*i)/WriteFrames+minX;
  89. y:=F_X(X);
  90. LineTo(
  91. round(BegX + ((x-minX)/lenX)*SIZE_X ),
  92. round(BegY - (y/maxY)*SIZE_Y )
  93. );
  94. end;
  95. end;{WriteGraph}
  96. {--------------------------------------------------------------}
  97. Procedure ShowTrap(x1,x2,y1,y2:real);
  98. begin
  99. SetColor(Yellow);
  100. MoveTo(round(BegX + ((x1-minX)/lenX)*SIZE_X ),BegY);
  101. LineTo(round(BegX + ((x2-minX)/lenX)*SIZE_X ),BegY);
  102. LineTo(round(BegX + ((x2-minX)/lenX)*SIZE_X ),round(BegY - (y2/maxY)*SIZE_Y ));
  103. LineTo(round(BegX + ((x1-minX)/lenX)*SIZE_X ),round(BegY - (y1/maxY)*SIZE_Y ));
  104. LineTo(round(BegX + ((x1-minX)/lenX)*SIZE_X ),BegY);
  105. end;{ShowTrap}
  106. {--------------------------------------------------------------}
  107. FuncTion ReDraw:char;
  108. var
  109. val:string;
  110. begin
  111. SetColor(Red);
  112. Bar(505,30,630,100);
  113. OutTextXY(510,40,'Last Area:');
  114. Str(Area:0:9,val);
  115. OutTextXY(510,50,val);
  116. OutTextXY(510,70,'Number:');
  117. Str(NTr,val);
  118. OutTextXY(510,80,val);
  119. redRaw := ReadKey;
  120. Bar(BEGX,BEGY,BEGX+SIZE_X,BEGY-SIZE_Y);
  121. WriteGraph;
  122. end; {ReDraw}
  123. {--------------------------------------------------------------}
  124. Procedure CalcArea;
  125. var
  126. LastAr : real;
  127. i : longint;
  128. y1,y2 :real;
  129. c : char;
  130. begin
  131. LastAr := 0;
  132. Ntr := 1;
  133. if IfWrite then begin
  134. SetFillStyle(SolidFill,Black);
  135. SetTextJustify(LeftText,CenterText);
  136. ReDraw;
  137. end;
  138. repeat
  139. LastAr := Area;
  140. Area := 0;
  141. for i := 1 to Ntr do
  142. begin
  143. y1 := F_X((lenX*(i-1))/Ntr + minX);
  144. y2 := F_X((lenX*i)/Ntr + minX);
  145. if ifWrite then ShowTrap((lenX*(i-1))/Ntr + minX,(lenX*i)/Ntr + minX,y1,y2);
  146. Area := Area + ((y1+y2)/2)*(lenX/Ntr);
  147. end;
  148. if ifWrite then c:=ReDraw;
  149. Ntr:=Ntr shl 1;
  150. if ord(c)=27 then exit;
  151. until abs(Area-LastAr)<Eps;
  152. end;{CalcArea}
  153. {--------------------------------------------------------------}
  154. VAR
  155. i : word;
  156. BEGIN
  157. if ifWrite then
  158. begin
  159. Init;
  160. WriteGraph;
  161. end;
  162. CalcArea;
  163. if ifWrite then CloseGraph;
  164. WriteLn('Area: ',Area:0:4);
  165. END.