Advertisement
Guest User

Untitled

a guest
Jul 22nd, 2017
40
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.70 KB | None | 0 0
  1. unit Unit1;
  2. interface
  3. uses
  4. Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  5. Dialogs, ExtCtrls, StdCtrls;
  6. type TRGB = record
  7. b, g, r: byte;
  8. end;
  9. ARGB = array[0..0] of TRGB;
  10. PARGB = ^ARGB;
  11. type
  12. TForm1 = class(TForm)
  13. Image1: TImage;
  14. GroupBox1: TGroupBox;
  15. Memo1: TMemo;
  16. Button1: TButton;
  17. procedure Image1MouseDown(Sender: TObject; Button: TMouseButton; Shift: TShiftState; X, Y: Integer);
  18. procedure Image1MouseMove(Sender: TObject; Shift: TShiftState; X, Y: Integer);
  19. procedure Image1MouseUp(Sender: TObject; Button: TMouseButton; Shift: TShiftState; X, Y: Integer);
  20. procedure FormCreate(Sender: TObject);
  21. procedure Button1Click(Sender: TObject);
  22. end;
  23. var
  24. Form1: TForm1;
  25. figure_state: array [0..3] of boolean=(false,false,false,false);
  26. coords: array [0..5] of integer=(200,100,50,100,100,50);
  27. points: array [0..3] of integer;
  28. implementation
  29. {$R *.dfm}
  30. const block=10;
  31. //------------------------------------------------------------------------------
  32. function gipotenus(x,y,z:word):word;
  33. begin
  34. if z=1 then result:=round(sqrt(sqr(x-coords[0])+sqr(y-coords[1])))
  35. else result:=round(sqrt(sqr(x-coords[3])+sqr(y-coords[4])));
  36. if result=0 then result:=1;
  37. end;
  38. //------------------------------------------------------------------------------
  39. function check_tochka_circle(x,y:word):boolean;
  40. begin
  41. if gipotenus(x,y,1)>coords[2] then result:=false
  42. else result:=true;
  43. end;
  44. //------------------------------------------------------------------------------
  45. function check_tochka_kardioda(x,y:word):boolean;
  46. begin
  47. if gipotenus(x,y,2)>coords[5]*(0.5+(x-coords[3]) / gipotenus(x,y,2)) then result:=false
  48. else result:=true;
  49. end;
  50. //------------------------------------------------------------------------------
  51. procedure draw_circle(x,y,r:word);
  52. Var i:integer;
  53. begin
  54. with Form1.Image1.Canvas do
  55. begin
  56. MoveTo(x+r,y);
  57. for i:=0 to 360 do
  58. begin
  59. LineTo(round(r*cos(i*pi/180))+x,round(r*sin(i*pi/180))+y);
  60. MoveTo(round(r*cos(i*pi/180))+x,round(r*sin(i*pi/180))+y);
  61. end;
  62. brush.Color:=clActiveCaption;
  63. Rectangle(x-r-block,y-r,x-r,y-r+block);
  64. brush.Color:=clBlack;
  65. Rectangle(x+r+block,y+r,x+r,y+r+block);
  66. end;
  67. coords[0]:=x;
  68. coords[1]:=y;
  69. coords[2]:=r;
  70. end;
  71. //------------------------------------------------------------------------------
  72. procedure draw_kardioida(x,y,r:word);
  73. Var i:integer;
  74. begin
  75. with Form1.Image1.Canvas do
  76. begin
  77. MoveTo(x+r,y);
  78. for i:=0 to 360 do
  79. begin
  80. if i=0 then MoveTo(round(r*(cos(i*pi/180)*(0.5+cos(i*pi/180))))+x,
  81. round(r*(sin(i*pi/180)*(0.5+cos(i*pi/180))))+y);
  82. LineTo(round(r*(cos(i*pi/180)*(0.5+cos(i*pi/180))))+x,
  83. round(r*(sin(i*pi/180)*(0.5+cos(i*pi/180))))+y);
  84. end;
  85. Brush.Color:=clyellow;
  86. Rectangle(x-block-r div 2,y-r-r div 2,x-r div 2,y-r+block-r div 2);
  87. brush.Color:=clBlack;
  88. Rectangle(x+2*r+block,y+r+r div 2-block,x+2*r,y+r+r div 2);
  89. end;
  90. coords[3]:=x;
  91. coords[4]:=y;
  92. coords[5]:=r;
  93. end;
  94. //------------------------------------------------------------------------------
  95. procedure draw_figures;
  96. Var i,j:integer;
  97. p: PARGB;
  98. square:integer;
  99. begin
  100. square:=0;
  101. Form1.Image1.Canvas.Pixels[0,0]:=clwhite;
  102. Form1.Image1.Picture.Bitmap.PixelFormat:=pf24bit;
  103. for j:=0 to Form1.Image1.Picture.Bitmap.Height -1 do
  104. begin
  105. p := Form1.Image1.Picture.Bitmap.Scanline[j];
  106. for i:=0 to Form1.Image1.Picture.Bitmap.Width -1 do
  107. begin
  108. begin
  109. p[i].r:=255;
  110. p[i].g:=255;
  111. p[i].b:=255;
  112. end;
  113. end;
  114. end;
  115. for j:=0 to Form1.Image1.Picture.Bitmap.Height -1 do
  116. begin
  117. p := Form1.Image1.Picture.Bitmap.Scanline[j];
  118. for i:=0 to Form1.Image1.Picture.Bitmap.Width -1 do
  119. begin
  120. if (check_tochka_circle(i,j) = true) and ( check_tochka_kardioda(i,j)=true) then
  121. begin
  122. inc(square);
  123. p[i].r:=255;
  124. p[i].g:=205;
  125. p[i].b:=205;
  126. end;
  127. end;
  128. end;
  129. draw_circle(coords[0],coords[1],coords[2]);
  130. draw_kardioida(coords[3],coords[4],coords[5]);
  131. Form1.Memo1.Lines.Clear;
  132. Form1.Memo1.Lines.Add('Площадь пересечения: '+IntToStr(square)+'px');
  133. Form1.Memo1.Lines.Add('X центра круга: '+IntToStr(coords[0]));
  134. Form1.Memo1.Lines.Add('Y центра круга: '+IntToStr(coords[1]));
  135. Form1.Memo1.Lines.Add('Радиус круга: '+IntToStr(coords[2]));
  136. Form1.Memo1.Lines.Add('X центра кардиоиды: '+IntToStr(coords[3]));
  137. Form1.Memo1.Lines.Add('Y центра кардиоиды: '+IntToStr(coords[4]));
  138. Form1.Memo1.Lines.Add('Радиус кардиоиды: '+IntToStr(coords[5]));
  139. end;
  140. //------------------------------------------------------------------
  141. procedure TForm1.Image1MouseDown(Sender: TObject; Button: TMouseButton;
  142. Shift: TShiftState; X, Y: Integer);
  143. begin
  144. if (x<=(coords[0]-coords[2])) and (x>=(coords[0]-coords[2]-block)) and (y<=(coords[1]-coords[2]+block)) and (y>=(coords[1]-coords[2]))
  145. then
  146. begin
  147. figure_state[0]:=true;
  148. figure_state[1]:=false;
  149. figure_state[2]:=true;
  150. figure_state[3]:=false;
  151. end;
  152. if (x<=(coords[3]-coords[5] div 2)) and (x>=(coords[3]-coords[5] div 2-block)) and (y<=(coords[4]-coords[5]+block-coords[5] div 2)) and (y>=(coords[4]-coords[5]-coords[5] div 2))
  153. then
  154. begin
  155. figure_state[0]:=false;
  156. figure_state[1]:=true;
  157. figure_state[2]:=true;
  158. figure_state[3]:=false;
  159. end;
  160. if (x>=(coords[0]+coords[2])) and (x<=(coords[0]+coords[2]+block)) and (y<=(coords[1]+coords[2]+block)) and (y>=(coords[1]+coords[2]))
  161. then
  162. begin
  163. figure_state[0]:=true;
  164. figure_state[1]:=false;
  165. figure_state[2]:=false;
  166. figure_state[3]:=true;
  167. end;
  168. if (x>=(coords[3]+2*coords[5])) and (x<=(coords[3]+2*coords[5]+block)) and (y<=(coords[4]+coords[5]+coords[5] div 2)) and (y>=(coords[4]+coords[5]-block+coords[5] div 2))
  169. then
  170. begin
  171. figure_state[0]:=false;
  172. figure_state[1]:=true;
  173. figure_state[2]:=false;
  174. figure_state[3]:=true;
  175. end;
  176. end;
  177. //------------------------------------------------------------------
  178. procedure TForm1.Image1MouseMove(Sender: TObject; Shift: TShiftState; X,
  179. Y: Integer);
  180. begin
  181. if (figure_state[3]=true) and (figure_state[0]=true) then
  182. begin
  183. coords[2]:=round(sqrt(sqr(X-coords[0])+sqr(Y-coords[1])));
  184. draw_figures;
  185. end;
  186. if (figure_state[3]=true) and (figure_state[1]=true) then
  187. begin
  188. coords[5]:=round(sqrt(sqr(X-coords[3])+sqr(Y-coords[4])));
  189. draw_figures;
  190. end;
  191. if (figure_state[2]=true) and (figure_state[0]=true) then
  192. begin
  193. coords[0]:=X+coords[2];
  194. coords[1]:=Y+coords[2];
  195. draw_figures;
  196. end;
  197. if (figure_state[2]=true) and (figure_state[1]=true) then
  198. begin
  199. coords[3]:=X+coords[5];
  200. coords[4]:=Y+coords[5];
  201. draw_figures;
  202. end;
  203. end;
  204. //------------------------------------------------------------------
  205. procedure TForm1.Image1MouseUp(Sender: TObject; Button: TMouseButton;
  206. Shift: TShiftState; X, Y: Integer);
  207. begin
  208. figure_state[2]:=false;
  209. figure_state[3]:=false;
  210. end;
  211. //------------------------------------------------------------------
  212. procedure TForm1.FormCreate(Sender: TObject);
  213. begin
  214. draw_figures;
  215. end;
  216. //------------------------------------------------------------------
  217. procedure TForm1.Button1Click(Sender: TObject);
  218. begin
  219. coords[0]:=200;
  220. coords[1]:=100;
  221. coords[2]:=50;
  222. coords[3]:=100;
  223. coords[4]:=100;
  224. coords[5]:=50;
  225. draw_figures;
  226. end;
  227. //------------------------------------------------------------------
  228. end.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement