pointView.cpp
上传用户:rabbit5101
上传日期:2022-07-29
资源大小:1887k
文件大小:12k
源码类别:

图形/文字识别

开发平台:

Visual C++

  1. // pointView.cpp : implementation of the CPointView class
  2. //
  3. #include "stdafx.h"
  4. #include "point.h"
  5. #include "pointDoc.h"
  6. #include "pointView.h"
  7. #include "iostream.h"
  8. #include "math.h"
  9. #include "stdio.h"
  10. #include "conio.h"
  11. #ifdef _DEBUG
  12. #define new DEBUG_NEW
  13. #undef THIS_FILE
  14. static char THIS_FILE[] = __FILE__;
  15. #endif
  16. /////////////////////////////////////////////////////////////////////////////
  17. // CPointView
  18. IMPLEMENT_DYNCREATE(CPointView, CView)
  19. BEGIN_MESSAGE_MAP(CPointView, CView)
  20. //{{AFX_MSG_MAP(CPointView)
  21. ON_WM_CREATE()
  22. ON_WM_LBUTTONDOWN()
  23. ON_COMMAND(ID_Run, OnRun)
  24. //}}AFX_MSG_MAP
  25. // Standard printing commands
  26. ON_COMMAND(ID_FILE_PRINT, CView::OnFilePrint)
  27. ON_COMMAND(ID_FILE_PRINT_DIRECT, CView::OnFilePrint)
  28. ON_COMMAND(ID_FILE_PRINT_PREVIEW, CView::OnFilePrintPreview)
  29. END_MESSAGE_MAP()
  30. /////////////////////////////////////////////////////////////////////////////
  31. // CPointView construction/destruction
  32. Stsp::init()
  33. {
  34.    CT[0].x=3639;  CT[0].y=1315;
  35. CT[1].x=4177;  CT[1].y=2244;
  36. CT[2].x=3712;  CT[2].y=1399;
  37. CT[3].x=3569;  CT[3].y=1438;
  38. CT[4].x=3757;  CT[4].y=1187;
  39. CT[5].x=3493;  CT[5].y=1696;
  40. CT[6].x=3904;  CT[6].y=1289;
  41. CT[7].x=3488;  CT[7].y=1535;
  42. CT[8].x=3791;  CT[8].y=1339;
  43. CT[9].x=3506;  CT[9].y=1221;
  44. CT[10].x=3374;  CT[10].y=1750;
  45. CT[11].x=3376;  CT[11].y=1306;
  46. CT[12].x=3237;  CT[12].y=1764;
  47. CT[13].x=3326;  CT[13].y=1556;
  48. CT[14].x=3188;  CT[14].y=1881;
  49. CT[15].x=3089;  CT[15].y=1251;
  50. CT[16].x=3258;  CT[16].y=911;
  51. CT[17].x=3814;  CT[17].y=261;
  52. CT[18].x=3238;  CT[18].y=1229;
  53. CT[19].x=3646;  CT[19].y=234;
  54. CT[20].x=3583;  CT[20].y=864;
  55. CT[21].x=4172;  CT[21].y=1125;
  56. CT[22].x=4089;  CT[22].y=1387;
  57. CT[23].x=4297;  CT[23].y=1218;
  58. CT[24].x=4020;  CT[24].y=1142;
  59. CT[25].x=4196;  CT[25].y=1044;
  60. CT[26].x=4116;  CT[26].y=1187;
  61. CT[27].x=4095;  CT[27].y=626;
  62. CT[28].x=4312;  CT[28].y=790;
  63. CT[29].x=4252;  CT[29].y=882;
  64. CT[30].x=4403;  CT[30].y=1022;
  65. CT[31].x=4685;  CT[31].y=830;
  66. CT[32].x=4386;  CT[32].y=570;
  67. CT[33].x=4361;  CT[33].y=73;
  68. CT[34].x=4720;  CT[34].y=557;
  69. CT[35].x=4643;  CT[35].y=404;
  70. CT[36].x=4634;  CT[36].y=654;
  71. CT[37].x=4153;  CT[37].y=426;
  72. CT[38].x=4784;  CT[38].y=270;
  73. CT[39].x=2846;  CT[39].y=1951;
  74. CT[40].x=2831;  CT[40].y=2099;
  75. CT[41].x=3007;  CT[41].y=1970;
  76. CT[42].x=3054;  CT[42].y=1710;
  77. CT[43].x=3086;  CT[43].y=1516;
  78. CT[44].x=1828;  CT[44].y=1210;
  79. CT[45].x=2562;  CT[45].y=1756;
  80. CT[46].x=2716;  CT[46].y=1924;
  81. CT[47].x=2061;  CT[47].y=1277;
  82. CT[48].x=2291;  CT[48].y=1403;
  83. CT[49].x=2751;  CT[49].y=1559;
  84. CT[50].x=2788;  CT[50].y=1491;
  85. CT[51].x=2012;  CT[51].y=1552;
  86. CT[52].x=1779;  CT[52].y=1626;
  87. CT[53].x=2381;  CT[53].y=1676;
  88. CT[54].x=682;   CT[54].y=825;
  89. CT[55].x=1478;  CT[55].y=267;
  90. CT[56].x=1777;  CT[56].y=892;
  91. CT[57].x=518;   CT[57].y=1251;
  92. CT[58].x=278;   CT[58].y=890;
  93. CT[59].x=1064;  CT[59].y=284;
  94. CT[60].x=1332;  CT[60].y=695;
  95. CT[61].x=3715;  CT[61].y=1678;
  96. CT[62].x=3688;  CT[62].y=1818;
  97. CT[63].x=4016;  CT[63].y=1715;
  98. CT[64].x=4181;  CT[64].y=1574;
  99. CT[65].x=3896;  CT[65].y=1656;
  100. CT[66].x=4087;  CT[66].y=1546;
  101. CT[67].x=3929;  CT[67].y=1692;
  102. CT[68].x=3918;  CT[68].y=2179;
  103. CT[69].x=4062;  CT[69].y=2220;
  104. CT[70].x=3751;  CT[70].y=1945;
  105. CT[71].x=3972;  CT[71].y=2136;
  106. CT[72].x=4061;  CT[72].y=2370;
  107. CT[73].x=4207;  CT[73].y=2533;
  108. CT[74].x=4029;  CT[74].y=2498;
  109. CT[75].x=4201;  CT[75].y=2397;
  110. CT[76].x=4139;  CT[76].y=2615;
  111. CT[77].x=3766;  CT[77].y=2364;
  112. CT[78].x=3777;  CT[78].y=2095;
  113. CT[79].x=3780;  CT[79].y=2212;
  114. CT[80].x=3896;  CT[80].y=2443;
  115. CT[81].x=3888;  CT[81].y=2261;
  116. CT[82].x=3594;  CT[82].y=2900;
  117. CT[83].x=3796;  CT[83].y=2499;
  118. CT[84].x=3678;  CT[84].y=2463;
  119. CT[85].x=3676;  CT[85].y=2578;
  120. CT[86].x=3478;  CT[86].y=2705;
  121. CT[87].x=3789;  CT[87].y=2620;
  122. CT[88].x=4029;  CT[88].y=2838;
  123. CT[89].x=3810;  CT[89].y=2969;
  124. CT[90].x=3862;  CT[90].y=2839;
  125. CT[91].x=3928;  CT[91].y=3029;
  126. CT[92].x=4167;  CT[92].y=3206;
  127. CT[93].x=4263;  CT[93].y=2931;
  128. CT[94].x=4186;  CT[94].y=3037;
  129. CT[95].x=3486;  CT[95].y=1755;
  130. CT[96].x=3492;  CT[96].y=1901;
  131. CT[97].x=3322;  CT[97].y=1916;
  132. CT[98].x=3334;  CT[98].y=2107;
  133. CT[99].x=3479;  CT[99].y=2198;
  134. CT[100].x=3429; CT[100].y=1908;
  135. CT[101].x=3587; CT[101].y=2417;
  136. CT[102].x=3318; CT[102].y=2408;
  137. CT[103].x=3176; CT[103].y=2150;
  138. CT[104].x=3507; CT[104].y=2376;
  139. CT[105].x=3296; CT[105].y=2217;
  140. CT[106].x=3229; CT[106].y=2367;
  141. CT[107].x=3264; CT[107].y=2551;
  142. CT[108].x=3394; CT[108].y=2643;
  143. CT[109].x=3402; CT[109].y=2912;
  144. CT[110].x=3360; CT[110].y=2792;
  145. CT[111].x=3101; CT[111].y=2721;
  146. CT[112].x=3402; CT[112].y=2510;
  147. CT[113].x=3439; CT[113].y=3201;
  148. CT[114].x=3792; CT[114].y=3156;
  149. CT[115].x=3468; CT[115].y=3018;
  150. CT[116].x=3526; CT[116].y=3263;
  151. CT[117].x=3142; CT[117].y=3421;
  152. CT[118].x=3356; CT[118].y=3212;
  153. CT[119].x=3012; CT[119].y=3394;
  154. CT[120].x=3130; CT[120].y=2973;
  155. CT[121].x=3044; CT[121].y=3081;
  156. CT[122].x=2935; CT[122].y=3240;
  157. CT[123].x=2765; CT[123].y=3321;
  158. CT[124].x=3140; CT[124].y=3558;
  159. CT[125].x=3053; CT[125].y=3739;
  160. CT[126].x=2545; CT[126].y=2357;
  161. CT[127].x=2769; CT[127].y=2492;
  162. CT[128].x=2284; CT[128].y=2803;
  163. CT[129].x=2611; CT[129].y=2275;
  164. CT[130].x=2348; CT[130].y=2652;
  165. CT[131].x=2577; CT[131].y=2574;
  166. CT[132].x=2860; CT[132].y=2862;
  167. CT[133].x=2778; CT[133].y=2826;
  168. CT[134].x=2592; CT[134].y=2820;
  169. CT[135].x=2801; CT[135].y=2700;
  170. CT[136].x=2126; CT[136].y=2896;
  171. CT[137].x=2401; CT[137].y=3164;
  172. CT[138].x=2370; CT[138].y=2975;
  173. CT[139].x=1890; CT[139].y=3033;
  174. CT[140].x=1304; CT[140].y=2312;
  175. CT[141].x=1084; CT[141].y=2313;
  176. CT[142].x=3538; CT[142].y=3298;
  177. CT[143].x=3470; CT[143].y=3304;
  178. for(int i=0;i<144;i++)
  179.    CT[i].init=i;
  180. CT[144]=CT[0];
  181. for(i=0;i<144;i++)
  182. {
  183. for(int j=0;j<144;j++)
  184. {
  185. dis[i][j]=(int)sqrt((CT[i].x-CT[j].x)*(CT[i].x-CT[j].x)+(CT[i].y-CT[j].y)*(CT[i].y-CT[j].y));
  186. }
  187. }
  188. return 0;
  189. }
  190. Stsp::sort()
  191. {
  192. M=10295;
  193. int i=0,j=0;
  194.     for(int m=0;m<144;m++)
  195. {
  196. for(int n=0;n<m;n++)
  197. {
  198. dissort[i].x1=CT[m].x;
  199. dissort[i].x2=CT[n].x;
  200. dissort[i].y1=CT[m].y;
  201. dissort[i].y2=CT[n].y;
  202. dissort[i].length=dis[m][n];
  203. dissort[i].u=CT[m].init;
  204. dissort[i].v=CT[n].init;
  205. i++;
  206. }
  207. }
  208. for(j=0;j<10295;j++)
  209. {
  210. for(i=0;i<M;i++)
  211. {
  212. if(dissort[i].length>dissort[i+1].length)
  213. {
  214. temp.length=dissort[i].length;
  215. dissort[i].length=dissort[i+1].length;
  216. dissort[i+1].length=temp.length;
  217. temp.u=dissort[i].u;
  218. dissort[i].u=dissort[i+1].u;
  219. dissort[i+1].u=temp.u;
  220. temp.v=dissort[i].v;
  221. dissort[i].v=dissort[i+1].v;
  222. dissort[i+1].v=temp.v;
  223. }
  224. }
  225. M--;
  226. }
  227. return 0;
  228. }
  229. Stsp:: tsp()
  230. {
  231.      int m=0;
  232.     int cycle=0;
  233. for(int i=0;i<144;i++)
  234. {
  235. E[i].x=145;
  236. E[i].y=145;
  237. E[i].sign=0;
  238. C[i]=145;
  239. T[i]=0;
  240. }
  241.          int vertex=0;
  242. for(i=0;i<10296;i++)
  243. {
  244. /* for(j=0;j<=vertex;j++)
  245. {
  246. if(C[j]==dissort[i].u)
  247. {
  248. sameu++;
  249. }
  250. if(C[j]==dissort[i].v)
  251. {
  252. samev++;
  253. }
  254. }*/
  255. if((T[dissort[i].u]==0)&&(T[dissort[i].v]==0))
  256. {
  257. E[m].x=dissort[i].u;
  258. E[m].y=dissort[i].v;
  259. C[vertex++]=dissort[i].u;
  260. C[vertex++]=dissort[i].v;
  261. T[dissort[i].u]++;
  262. length=length+dissort[i].length;
  263. //cout<<"dissort[i].u="<<dissort[i].u<<endl;
  264. //cout<<"T[dissort[i].u]="<<T[dissort[i].u]<<endl;
  265. T[dissort[i].v]++;
  266. m++;
  267.                 
  268. }
  269.  else if((T[dissort[i].u]==0)&&(T[dissort[i].v]==1))
  270. {
  271. E[m].x=dissort[i].u;
  272. E[m].y=dissort[i].v;
  273.                 C[vertex++]=dissort[i].u;
  274.                 T[dissort[i].u]++;
  275.                  T[dissort[i].v]++;
  276. length=length+dissort[i].length;
  277. m++;
  278. }
  279.  else if((T[dissort[i].u]==1)&&(T[dissort[i].v]==0))
  280. {
  281. E[m].x=dissort[i].u;
  282. E[m].y=dissort[i].v;
  283. C[vertex++]=dissort[i].v;
  284. T[dissort[i].u]++;
  285. T[dissort[i].v]++;
  286. length=length+dissort[i].length;
  287. m++;
  288. }
  289.  else if((T[dissort[i].u]==1)&&(T[dissort[i].v]==1))
  290.  {
  291.  E[m].x=dissort[i].u;
  292.  E[m].y=dissort[i].v;
  293.  for(int j=0;j<m+1;j++)
  294.  {
  295.  if((E[j].x==E[m].x)&&E[j].sign==0)
  296.  {
  297.  E[j].sign=1;
  298.  if(E[j].y==E[m].y)
  299.  { 
  300.  cycle=1;
  301.  break;
  302.  }
  303.  else
  304.  {
  305. E[m].x=E[j].y;
  306.  j=0;
  307.  continue;
  308.  }
  309.  }
  310.  if((E[j].y==E[m].x)&&(E[j].sign==0))
  311.  {
  312.  E[j].sign=1;
  313.  if(E[j].x==E[m].y)
  314.  {
  315.  cycle=1;
  316.  break;
  317.  }
  318.  else
  319.  {
  320.  E[m].x=E[j].x;
  321.  j=0;
  322. continue;
  323.  }
  324.  }
  325.  }
  326.  if(cycle)
  327.  cycle=0;
  328.  else
  329.  {
  330.                     E[m].x=dissort[i].u;
  331.     E[m].y=dissort[i].v;
  332. T[dissort[i].u]++;
  333.     T[dissort[i].v]++;
  334.               
  335.  m++;
  336.  }
  337.  } 
  338.  for(int s=0;s<m;s++)
  339.  E[s].sign=0;
  340.  
  341. }
  342. for(i=0;i<144;i++)
  343. {
  344. if(T[E[i].y]==1)
  345. {
  346. E[143].x=E[i].y;
  347.     T[E[i].y]++;
  348. break;
  349. }
  350. }
  351. for(i=0;i<144;i++)
  352. {
  353. if(T[E[i].y]==1)
  354. {
  355. E[143].y=E[i].y;
  356.     T[E[i].y]++;
  357. break;
  358. }
  359. }
  360. return 0;
  361. }          
  362. CPointView::CPointView()
  363. {
  364. // TODO: add construction code here
  365. }
  366. CPointView::~CPointView()
  367. {
  368. }
  369. BOOL CPointView::PreCreateWindow(CREATESTRUCT& cs)
  370. {
  371. // TODO: Modify the Window class or styles here by modifying
  372. //  the CREATESTRUCT cs
  373. return CView::PreCreateWindow(cs);
  374. }
  375. /////////////////////////////////////////////////////////////////////////////
  376. // CPointView drawing
  377. void CPointView::OnDraw(CDC* pDC)
  378. {
  379. init();
  380. CPointDoc* pDoc = GetDocument();
  381. ASSERT_VALID(pDoc);
  382. /*CString str;
  383. str.Format("%d",E[0].x);
  384. MessageBox(str);*/
  385. CBrush newbrush,*pOldBrush;
  386. newbrush.CreateSolidBrush(RGB(255,0,0));
  387. pOldBrush=pDC->SelectObject(&newbrush);
  388. for(int i=0;i<144;i++)
  389. {
  390. // CRect rcView; 
  391.       // GetClientRect (rcView); 
  392. pDC->SelectStockObject (WHITE_PEN);
  393.         
  394. pDC->Ellipse(CT[i].x/8-3,CT[i].y/8-3,CT[i].x/8+3,CT[i].y/8+3);
  395. }
  396. pDC->SelectObject(pOldBrush);
  397. if(tsped)
  398. {
  399. CPen newpen,*pOldpen;
  400. newpen.CreatePen(PS_SOLID,1,RGB(255,0,0));
  401. pOldpen=pDC->SelectObject(&newpen);
  402. for(int s=0;s<144;s++)
  403. {
  404. pDC->MoveTo(CT[E[s].x].x/8,CT[E[s].x].y/8);
  405. pDC->LineTo(CT[E[s].y].x/8,CT[E[s].y].y/8);
  406. _sleep(100);
  407. }
  408. pDC->SelectObject(pOldpen);
  409. }
  410. }
  411. /////////////////////////////////////////////////////////////////////////////
  412. // CPointView printing
  413. BOOL CPointView::OnPreparePrinting(CPrintInfo* pInfo)
  414. {
  415. // default preparation
  416. return DoPreparePrinting(pInfo);
  417. }
  418. void CPointView::OnBeginPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)
  419. {
  420. // TODO: add extra initialization before printing
  421. }
  422. void CPointView::OnEndPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)
  423. {
  424. // TODO: add cleanup after printing
  425. }
  426. /////////////////////////////////////////////////////////////////////////////
  427. // CPointView diagnostics
  428. #ifdef _DEBUG
  429. void CPointView::AssertValid() const
  430. {
  431. CView::AssertValid();
  432. }
  433. void CPointView::Dump(CDumpContext& dc) const
  434. {
  435. CView::Dump(dc);
  436. }
  437. CPointDoc* CPointView::GetDocument() // non-debug version is inline
  438. {
  439. ASSERT(m_pDocument->IsKindOf(RUNTIME_CLASS(CPointDoc)));
  440. return (CPointDoc*)m_pDocument;
  441. }
  442. #endif //_DEBUG
  443. /////////////////////////////////////////////////////////////////////////////
  444. // CPointView message handlers
  445. int CPointView::OnCreate(LPCREATESTRUCT lpCreateStruct) 
  446. {
  447. if (CView::OnCreate(lpCreateStruct) == -1)
  448. return -1;
  449. // TODO: Add your specialized creation code here
  450. tsped=FALSE;
  451. return 0;
  452. }
  453. void CPointView::OnLButtonDown(UINT nFlags, CPoint point) 
  454. {
  455. // TODO: Add your message handler code here and/or call default
  456. CView::OnLButtonDown(nFlags, point);
  457. }
  458. void CPointView::OnRun() 
  459. {
  460. // TODO: Add your command handler code here
  461. sort();
  462. tsp();
  463. //sort();
  464. //tsp();
  465. tsped=TRUE;
  466. Invalidate();
  467. }