fork(1) download
  1. //---------------------------------------------------------------------------
  2. #include <vcl.h>
  3.  
  4. #pragma hdrstop
  5.  
  6. #include "math.h"
  7.  
  8. #include "Unit1.h"
  9.  
  10. //---------------------------------------------------------------------------
  11.  
  12. #pragma package(smart_init)
  13.  
  14. #pragma resource "*.dfm"
  15.  
  16. TForm1 *Form1;
  17.  
  18. CRITICAL_SECTION cs;
  19.  
  20. //---------------------------------------------------------------------------
  21.  
  22. __fastcall TForm1::TForm1(TComponent* Owner)
  23.  
  24. : TForm(Owner)
  25.  
  26. {
  27.  
  28. Form1->Image1->Canvas->Pen->Color=clWhite;
  29.  
  30. InitializeCriticalSection( &cs );
  31.  
  32. }
  33.  
  34. //---------------------------------------------------------------------------
  35. float k1=80,k2=160,c3=240,e=0.00001;
  36.  
  37. int zader = 5;
  38.  
  39. int Msh=1;
  40. float f(float x1,float x2){
  41.  
  42. return (float)(k1*x1*x1+k2*x2*x2+c3);
  43.  
  44. }
  45. float f_x1(float x2){
  46.  
  47. return (float)(2*k2*x2);
  48.  
  49. }
  50. float f_x2(float x1){
  51.  
  52. return (float)(2*k1*x1);
  53.  
  54. }
  55. struct xy_{float x,y;};
  56.  
  57. Graphics::TBitmap *bb = new Graphics::TBitmap();
  58.  
  59. DWORD PN2_ID=0;
  60.  
  61. HANDLE PN2=NULL;
  62.  
  63. DWORD CALLBACK Grad_metod(void* Ptr){
  64.  
  65. Form1->Memo1->Clear();
  66.  
  67. xy_* xy=(xy_*)Ptr;
  68.  
  69. AnsiString s;
  70.  
  71. int shi=0;
  72.  
  73. float x1[2],x2[2],p[2],Ak;
  74.  
  75. x1[0]=(float)(xy->x -Form1->Image1->Width/2)/Msh;
  76.  
  77. x1[1]=(float)(xy->y -Form1->Image1->Height/2)/Msh;
  78.  
  79. Form1->Image1->Canvas->MoveTo(0,0);
  80.  
  81. s=(String)"Хо = ["+(floor(x1[0]*1000000)/1000000)+" ; "+(floor(x1[1]*1000000)/1000000)+"]\r\n";
  82.  
  83. Form1->Memo1->Lines->Add(s);
  84.  
  85. p[0]=-(float)f_x1(x1[0]);
  86.  
  87. p[1]=-(float)f_x2(x1[1]);
  88.  
  89. float kz;
  90.  
  91. int tz,ttz;
  92.  
  93. while(1){
  94.  
  95. if(shi<3){
  96.  
  97. kz=(float)f(x1[1],x1[0]);
  98.  
  99. bb->Assign(Form1->Image1->Picture->Bitmap);
  100.  
  101. for(int j=-Form1->Image1->Width/2;j<Form1->Image1->Width/2;j++){
  102.  
  103. tz=(f(j/Msh,-200/Msh)>kz)?1:-1;
  104.  
  105. ttz=(f(-200/Msh,j/Msh)>kz)?1:-1;
  106.  
  107. for(int i=-Form1->Image1->Height/2;i<Form1->Image1->Height/2;i++){
  108.  
  109. if(f(j/Msh,i/Msh)>kz && tz<0 || f(j/Msh,i/Msh)<kz && tz>0){
  110.  
  111. bb->Canvas->Pixels[i+200][j+200]=clBlack;
  112.  
  113. tz+=-2*tz;
  114.  
  115. }
  116.  
  117. if(f(i/Msh,j/Msh)>kz && ttz<0 || f(i/Msh,j/Msh)<kz && ttz>0){
  118.  
  119. bb->Canvas->Pixels[j+200][i+200]=clBlack;
  120.  
  121. ttz+=-2*ttz;
  122.  
  123. }
  124.  
  125. }
  126.  
  127. }
  128.  
  129. Form1->Image1->Picture->Assign(bb);
  130.  
  131. }
  132.  
  133. shi++;
  134.  
  135. if(PN2==NULL){PN2_ID=0;return 0;}
  136.  
  137. Ak=-( f_x1(x1[0])*p[0] +f_x2(x1[1])*p[1] )/( p[0]*p[0]*f_x1(1) + p[1]*p[1]*f_x2(1));
  138.  
  139. x2[0]=x1[0]+Ak*p[0];
  140.  
  141. x2[1]=x1[1]+Ak*p[1];
  142.  
  143. Form1->Image1->Canvas->Pen->Color=(Form1->RadioButton2->Checked)?clBlue:clGreen;
  144.  
  145. Form1->Image1->Canvas->MoveTo(x1[0]*Msh+Form1->Image1->Width/2,x1[1]*Msh+Form1->Image1->Height/2);
  146.  
  147. Form1->Image1->Canvas->LineTo(x2[0]*Msh+Form1->Image1->Width/2,x2[1]*Msh+Form1->Image1->Height/2);
  148.  
  149. Form1->Image1->Repaint();
  150.  
  151. s=(String)"Шаг "+shi+".\r\n E = "+floor(sqrt( pow(x1[0]-x2[0],2)+pow(x1[1]-x2[1],2))*pow(10,13))/pow(10,13)+"\r\n X = ["+(floor(x2[0]*1000000)/1000000)+" : "+(floor(x2[1]*1000000)/1000000)+"]\r\n";
  152.  
  153. Form1->Memo1->Lines->Add(s);
  154.  
  155. SendMessage(Form1->Memo1->Handle, WM_VSCROLL, MAKEWPARAM(SB_BOTTOM,0), 0);
  156.  
  157. if(e>sqrt( pow(x1[0]-x2[0],2)+pow(x1[1]-x2[1],2) )){PN2_ID=0;return 0;}
  158.  
  159. Sleep(100*zader);
  160.  
  161. if(Form1->RadioButton1->Checked){
  162.  
  163. p[0]=-f_x1(x2[0]);
  164.  
  165. p[1]=-f_x2(x2[1]);
  166.  
  167. }else if(Form1->RadioButton2->Checked){
  168.  
  169. p[0]=-f_x1(x2[0])+p[0]*( pow(f_x1(x2[0]),2) +pow(f_x2(x2[1]),2) )/( pow(f_x1(x1[0]),2) +pow(f_x2(x1[1]),2) );
  170.  
  171. p[1]=-f_x2(x2[1])+p[1]*( pow(f_x1(x2[0]),2) +pow(f_x2(x2[1]),2) )/( pow(f_x1(x1[0]),2) +pow(f_x2(x1[1]),2) );
  172.  
  173. }
  174.  
  175. x1[0]=x2[0];
  176.  
  177. x1[1]=x2[1];
  178.  
  179. }
  180. Form1->Memo1->Lines->Add("Готово!!!");
  181.  
  182. }
  183. void VectorRotate(xy_* buf,xy_* t1,xy_* t2,float angle){
  184.  
  185. angle=acos(((t2->x-t1->x))/sqrt(pow(t2->x-t1->x,2) + pow(t2->y-t1->y,2)))+angle*3.14/180;
  186.  
  187. if(t2->y-t1->y<0)angle*=-1;
  188.  
  189. float len=sqrt(pow(t2->x-t1->x,2)+pow(t2->y-t1->y,2));
  190.  
  191. buf->x=len*cos(angle)+t1->x;
  192.  
  193. buf->y=len*sin(angle)+t1->y;
  194.  
  195. }
  196. struct TwoPoint{xy_ t1,t2;};
  197.  
  198. xy_ ptmp;
  199.  
  200. bool flag=0;
  201.  
  202. DWORD CALLBACK Treangle_metod(void* Ptr){
  203.  
  204. Form1->Memo1->Clear();
  205.  
  206. AnsiString s;
  207.  
  208. TwoPoint* ptr=(TwoPoint*)Ptr;
  209.  
  210. int shi=0;
  211.  
  212. xy_ t[4]={{(ptr->t1.x-Form1->Image1->Width/2)/Msh,
  213.  
  214. (ptr->t1.y-Form1->Image1->Height/2)/Msh},
  215.  
  216. {(ptr->t2.x-Form1->Image1->Width/2)/Msh,
  217.  
  218. (ptr->t2.y-Form1->Image1->Height/2)/Msh}
  219.  
  220. };
  221.  
  222. VectorRotate(&t[2],&t[0],&t[1],60);
  223.  
  224. s=(String)"Х0 = ["+(floor(t[0].x*1000000)/1000000)+" ; "+(floor(t[0].y*1000000)/1000000)+"]\r\nХ1 = ["+(floor(t[1].x*1000000)/1000000)+" ; "+(floor(t[1].y*1000000)/1000000)+"]\r\n";
  225.  
  226. Form1->Memo1->Lines->Add(s);
  227.  
  228. Form1->Image1->Canvas->Pen->Color=clBlack;
  229.  
  230. while(PN2!=NULL && e<sqrt(pow(t[1].x-t[0].x,2)+pow(t[1].y-t[0].y,2)) ){
  231.  
  232. Form1->Image1->Canvas->MoveTo(t[0].x*Msh+Form1->Image1->Width/2,t[0].y*Msh+Form1->Image1->Height/2);
  233.  
  234. Form1->Image1->Canvas->LineTo(t[1].x*Msh+Form1->Image1->Width/2,t[1].y*Msh+Form1->Image1->Height/2);
  235.  
  236. Form1->Image1->Canvas->LineTo(t[2].x*Msh+Form1->Image1->Width/2,t[2].y*Msh+Form1->Image1->Height/2);
  237.  
  238. Form1->Image1->Canvas->LineTo(t[0].x*Msh+Form1->Image1->Width/2,t[0].y*Msh+Form1->Image1->Height/2);
  239.  
  240. Form1->Image1->Repaint();
  241.  
  242. shi++;
  243.  
  244. s=(String)"Шаг "+shi+".\r\n E = "+floor(sqrt(pow(t[1].x-t[0].x,2)+pow(t[1].y-t[0].y,2))*pow(10,13))/pow(10,13)+"\r\n X = ["+(floor(t[2].x*1000000)/1000000)+" : "+(floor(t[2].y*1000000)/1000000)+"]\r\n";
  245.  
  246. Form1->Memo1->Lines->Add(s);
  247.  
  248. Sleep(100*zader);
  249.  
  250. //упорядочивание вершин
  251.  
  252. if(f(t[0].x,t[0].y)>f(t[1].x,t[1].y)){
  253.  
  254. t[3].x=t[0].x; t[3].y=t[0].y;
  255.  
  256. t[0].x=t[1].x; t[0].y=t[1].y;
  257.  
  258. t[1].x=t[3].x; t[1].y=t[3].y;
  259.  
  260. }
  261.  
  262. if(f(t[1].x,t[1].y)>f(t[2].x,t[2].y)){
  263.  
  264. t[3].x=t[1].x; t[3].y=t[1].y;
  265.  
  266. t[1].x=t[2].x; t[1].y=t[2].y;
  267.  
  268. t[2].x=t[3].x; t[2].y=t[3].y;
  269.  
  270. }
  271.  
  272. if(f(t[0].x,t[0].y)>f(t[1].x,t[1].y)){
  273.  
  274. t[3].x=t[0].x; t[3].y=t[0].y;
  275.  
  276. t[0].x=t[1].x; t[0].y=t[1].y;
  277.  
  278. t[1].x=t[3].x; t[1].y=t[3].y;
  279.  
  280. }
  281.  
  282. //определение 4 вершины
  283.  
  284. t[3].x=(t[0].x+t[1].x)/2;
  285.  
  286. t[3].y=(t[0].y+t[1].y)/2;
  287.  
  288. t[3].x+=t[3].x-t[2].x;
  289.  
  290. t[3].y+=t[3].y-t[2].y;
  291.  
  292. if(f(t[3].x,t[3].y)<f(t[2].x,t[2].y)){ t[2].x=t[3].x; t[2].y=t[3].y;
  293.  
  294. }else{
  295.  
  296. t[1].x=(t[0].x+t[1].x)/2;
  297.  
  298. t[1].y=(t[0].y+t[1].y)/2;
  299.  
  300. t[0].x=t[1].x-(t[1].x-t[2].x)/3;
  301.  
  302. t[0].y=t[1].y-(t[1].y-t[2].y)/3;
  303.  
  304. VectorRotate(&t[2],&t[0],&t[1],-60);
  305.  
  306. }
  307.  
  308. }
  309. Form1->Memo1->Lines->Add("Готово!!!");
  310.  
  311. PN2_ID=0;
  312.  
  313. return 0;
  314.  
  315. }
  316. //---------------------------------------------------------------------------
  317. void __fastcall TForm1::Image1MouseDown(TObject *Sender, TMouseButton Button, TShiftState Shift,
  318.  
  319. int X, int Y)
  320.  
  321. {
  322.  
  323. PN2=NULL;
  324.  
  325. while(PN2_ID!=0) Sleep(10);
  326.  
  327. if(RadioButton3->Checked){
  328.  
  329. Image1->Canvas->Pen->Color=clYellow;
  330.  
  331. Image1->Canvas->Pen->Width=5;
  332.  
  333. Image1->Canvas->Ellipse(X,Y,X+1,Y+1);
  334.  
  335. Image1->Canvas->Pen->Width=1;
  336.  
  337. flag=!flag;
  338.  
  339. if(flag){
  340.  
  341. ptmp.x=X;
  342.  
  343. ptmp.y=Y;
  344.  
  345. }else{
  346.  
  347. TwoPoint* tp = new TwoPoint ;
  348.  
  349. tp->t1.x=ptmp.x;
  350.  
  351. tp->t1.y=ptmp.y;
  352.  
  353. tp->t2.x=X;
  354.  
  355. tp->t2.y=Y;
  356.  
  357. PN2=CreateThread(0, 0, Treangle_metod, tp, 0, &PN2_ID);
  358.  
  359. }
  360.  
  361. }else{
  362.  
  363. Image1->Picture->Assign(0);
  364.  
  365. xy_* xy = new xy_ ;
  366.  
  367. xy->x=X;
  368.  
  369. xy->y=Y;
  370.  
  371. PN2=CreateThread(0, 0, Grad_metod, xy, 0, &PN2_ID);
  372.  
  373. }
  374.  
  375. }
  376.  
  377. //---------------------------------------------------------------------------
  378. void __fastcall TForm1::RadioButton3Click(TObject *Sender)
  379.  
  380. {
  381.  
  382. flag=0;
  383.  
  384. }
  385.  
  386. //---------------------------------------------------------------------------
  387.  
Compilation error #stdin compilation error #stdout 0s 0KB
stdin
Standard input is empty
compilation info
prog.cpp:2:17: fatal error: vcl.h: No such file or directory
 #include <vcl.h>
                 ^
compilation terminated.
stdout
Standard output is empty