// TestView.cpp : implementation of the CTestView class
//

#include "stdafx.h"
#include "Test.h"

#include "TestDoc.h"
#include "TestView.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

#define STEP pDoc->step
/////////////////////////////////////////////////////////////////////////////
// CTestView

IMPLEMENT_DYNCREATE(CTestView, CScrollView)

BEGIN_MESSAGE_MAP(CTestView, CScrollView)
	//{{AFX_MSG_MAP(CTestView)
	ON_WM_LBUTTONDBLCLK()
	ON_COMMAND(ID_POINT, OnPoint)
	ON_COMMAND(ID_FB, OnFb)
	ON_COMMAND(ID_CLEAR, OnClear)
	ON_UPDATE_COMMAND_UI(ID_POINT, OnUpdatePoint)
	ON_WM_LBUTTONUP()
	ON_WM_LBUTTONDOWN()
	ON_COMMAND(ID_NORM, OnNorm)
	ON_UPDATE_COMMAND_UI(ID_VIEW_TOOLBAR, OnUpdateViewToolbar)
	ON_UPDATE_COMMAND_UI(ID_NORM, OnUpdateNorm)
	ON_COMMAND(ID_ADD, OnAdd)
	ON_COMMAND(ID_START, OnStart)
	ON_COMMAND(ID_START_FIRSTSORTMETHOD, OnStartFirstsortmethod)
	ON_COMMAND(ID_START_SECONDSORTMETHOD, OnStartSecondsortmethod)
	ON_COMMAND(ID_START_THIRDSORTMETHOD, OnStartThirdsortmethod)
	ON_COMMAND(ID_STATISTIC, OnStatistic)
	ON_COMMAND(ID_OPTIONS_FIELDOPTIONS, OnOptionsFieldoptions)
	ON_COMMAND(ID_START_AUTOSORTRUN, OnStart)
	ON_WM_CREATE()
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CTestView construction/destruction

CTestView::CTestView()
{
    
}

CTestView::~CTestView()
{
}

BOOL CTestView::PreCreateWindow(CREATESTRUCT& cs)
{
	// TODO: Modify the Window class or styles here by modifying
	//  the CREATESTRUCT cs

	return CScrollView::PreCreateWindow(cs);
}

/////////////////////////////////////////////////////////////////////////////
// CTestView drawing

void CTestView::OnDraw(CDC* pDC)
{
//	CTestDoc* pDoc = GetDocument();
	ASSERT_VALID(pDoc);

    
    CSize docSize (pDoc->PltSizeX*STEP+10,pDoc->PltSizeY*STEP+10);  // Размер документа
	CRect rect;
	GetClientRect (&rect);
	CSize pageSize (rect.right,rect.bottom); // Размер страницы
    CSize lineSize (0,20);
	SetScrollSizes (MM_TEXT,docSize,pageSize,lineSize);
      


	CPen pen(PS_SOLID,1,RGB(200,200,200));
    CPen *oldPen=pDC->SelectObject(&pen);
	
    //MessageBeep(0xFFFFFFFF);

	for (int i=0;i<pDoc->PltSizeX+1;i++) //   Отрисовка сетки
	{ pDC->MoveTo (CPoint(5+i*STEP,5));
      pDC->LineTo (5+i*STEP,pDoc->PltSizeY*STEP+5); 
	}
	for (i=0;i<pDoc->PltSizeY+1;i++)
	{ pDC->MoveTo (CPoint(5,5+i*STEP));
      pDC->LineTo (pDoc->PltSizeX*STEP+5,5+i*STEP); 
	}
    pDC->SelectObject(oldPen); 

	for (i=0;i<pDoc->PltSizeY;i++)       //  Отрисовка данных ДРП
		for (int j=0;j<pDoc->PltSizeX;j++)
		{ 
			if (pDoc->Data[i][j])
			DrawElement(pDC,pDoc->Data[i][j],j,i,0);			  

		}
       
	//pDC->FillSolidRect(CRect(20,20, 30, 40), RGB(200, 0, 0));
}




void CTestView::OnInitialUpdate()
{
	CScrollView::OnInitialUpdate();
	CSize sizeTotal;
	// TODO: calculate the total size of this view
	sizeTotal.cx = sizeTotal.cy = 100;
	SetScrollSizes(MM_TEXT, sizeTotal);
	Invalidate();
//	pDoc=GetDocument();
//	pDoc->EditMode=0;
	//pDoc->PltSizeX=50;
//	pDoc->PltSizeY=30;
//	pDoc->OnNewDocument();
/*	pDoc->step=10;
	pDoc->Critery=1;
	pDoc->Pause=true;
	pDoc->StatNo=0;
	for(int i=0;i<3;i++)
	{ pDoc->UPercent[i]=100;
	  pDoc->USumma[i]=0;
	  pDoc->UTime[i]=0;
	}

	OptionWin.pDoc = pDoc;
	BeforeWin.pDoc = pDoc;
	AfterWin.pDoc  = pDoc;
    ResultDialog.pDoc = pDoc;*/
}

/////////////////////////////////////////////////////////////////////////////
// CTestView diagnostics

#ifdef _DEBUG
void CTestView::AssertValid() const
{
	CScrollView::AssertValid();
}

void CTestView::Dump(CDumpContext& dc) const
{
	CScrollView::Dump(dc);
}

CTestDoc* CTestView::GetDocument() // non-debug version is inline
{
	ASSERT(m_pDocument->IsKindOf(RUNTIME_CLASS(CTestDoc)));
	return (CTestDoc*)m_pDocument;
}
#endif //_DEBUG

/////////////////////////////////////////////////////////////////////////////
// CTestView message handlers

void CTestView::OnUpdate(CView* pSender, LPARAM lHint, CObject* pHint) 
{
	// TODO: Add your specialized code here and/or call the base class
/*
	CRect r;
	GetClientRect(&r);
	CSize pageSize(r.right, r.bottom);
	SetScrollSizes( MM_LOENGLISH, pageSize);
*/    
//	ResizeParentToFit( );   // Default bShrinkOnly argument
}


void CTestView::DrawElement(CDC *pDC,int type,int j,int i,int scr)
{ 
   CPen   pen(PS_SOLID,1,RGB(200,200,200));
   CBrush brush_C(RGB(250,100,0));
   CBrush brush_F(RGB(0,200,200));
   CBrush brush_Cl(RGB(255,255,255));
   CBrush brush_Bl(RGB(0,0,0));
   CBrush brush_TR(RGB(0,0,255));
   CBrush *oldBrush;
   CPen   *oldPen;
   
   CPoint Pos=GetScrollPosition(); 
   int x=Pos.x;
   int y=Pos.y;
   int code=type;

   if (!scr) {x=0; y=0;}
   int sm=STEP*4/10; 
   int smC=STEP*2/10;


   if((code>=870)&&(code<=892))   //  Участки трассы 
   {  
	  oldBrush=pDC->SelectObject(&brush_Bl); 
	  if (code>=885) 
	  {  pDC->Ellipse(j*STEP+5-x,i*STEP+5-y,(j+1)*STEP+5-x,(i+1)*STEP+5-y);  	                            
	     code=code-15;
	  }	  
   }  

   if((code>0) && (code<800))  
   { 
	  oldBrush=pDC->SelectObject(&brush_TR);
	  if (type<500)  //  Рабочие точки
	  { // oldBrush=pDC->SelectObject(&brush_TR);
	     pDC->Ellipse(j*STEP+5+smC-x,i*STEP+5+smC-y,(j+1)*STEP+5-smC-x,(i+1)*STEP+5-smC-y);  	                            
	  }
      else           //  Лучи
      {  //oldBrush=pDC->SelectObject(&brush_Bl);
		 pDC->Rectangle(j*STEP+5+smC-x,i*STEP+5+smC-y,(j+1)*STEP+5-smC-x,(i+1)*STEP+5-smC-y);
	  }
	}
 
   else			

   switch(code)
   { 
	 case 0:    if (!scr) break;     //  Empty
	            oldPen=pDC->SelectObject(&pen);	        
		        oldBrush=pDC->SelectObject(&brush_Cl);
				pDC->Rectangle(j*STEP+5-x,i*STEP+5-y,(j+1)*STEP+6-x,(i+1)*STEP+6-y);  	            
				pDC->SelectObject(oldPen);
				break;

     case 980:  oldBrush=pDC->SelectObject(&brush_C);   // New point
				pDC->Ellipse(j*STEP+6-x,i*STEP+6-y,(j+1)*STEP+4-x,(i+1)*STEP+4-y);  	                            
				break;	 

	 case 999:  oldBrush=pDC->SelectObject(&brush_F);  // Forbid. zone
				pDC->Rectangle(j*STEP+6-x,i*STEP+6-y,(j+1)*STEP+4-x,(i+1)*STEP+4-y);  	            
				pDC->MoveTo (CPoint(j*STEP+8-x,i*STEP+8-y));
				pDC->LineTo ((j+1)*STEP+2-x,(i+1)*STEP+2-y);
				pDC->MoveTo (CPoint((j+1)*STEP+1-x,i*STEP+8-y));
				pDC->LineTo (j*STEP+7-x,(i+1)*STEP+2-y);			
				break;     
				///////////////////   Trace Elements  /////////////////////////
	 
	 case 870:   pDC->Rectangle(j*STEP+5-x+STEP/2, i*STEP+5-y+sm,
	 				              (j+1)*STEP+5-x, (i+1)*STEP+5-y-sm);
				break;
	 case 871:   pDC->Rectangle(j*STEP+5-x+sm, i*STEP+5-y,
	 				       (j+1)*STEP+5-x-sm, i*STEP+5-y+STEP/2);
				break;
	 case 872:   pDC->Rectangle(j*STEP+5-x, i*STEP+5-y+sm,
	 				    j*STEP+5-x+STEP/2, (i+1)*STEP+5-y-sm);
		        break; 
	 case 873:   pDC->Rectangle(j*STEP+5-x+sm, i*STEP+5-y+STEP/2,
	 				       (j+1)*STEP+5-x-sm, (i+1)*STEP+5-y)
						   ;
				break;
	 case 874:   pDC->Rectangle(j*STEP+5-x+STEP/2, i*STEP+5-y+sm,
	 				              (j+1)*STEP+5-x, (i+1)*STEP+5-y-sm);
		        pDC->Rectangle(j*STEP+5-x+sm, i*STEP+5-y+sm,
	 				       (j+1)*STEP+5-x-sm, (i+1)*STEP+5-y);
		        break; 
	 case 875:   pDC->Rectangle(j*STEP+5-x+STEP/2, i*STEP+5-y+sm,
	 				              (j+1)*STEP+5-x, (i+1)*STEP+5-y-sm);
		        pDC->Rectangle(j*STEP+5-x+sm, i*STEP+5-y,
	 				       (j+1)*STEP+5-x-sm, (i+1)*STEP+5-y-sm);
				break;
	 case 876:   pDC->Rectangle(j*STEP+5-x, i*STEP+5-y+sm,
	 			        (j+1)*STEP+5-x-sm, (i+1)*STEP+5-y-sm);
		        pDC->Rectangle(j*STEP+5-x+sm, (i+1)*STEP+5-y,
	 				       (j+1)*STEP+5-x-sm, (i+1)*STEP+5-y-sm);
				break;
	 case 877:   pDC->Rectangle(j*STEP+5-x, i*STEP+5-y+sm,
	 			        (j+1)*STEP+5-x-sm, (i+1)*STEP+5-y-sm);
		        pDC->Rectangle(j*STEP+5-x+sm, i*STEP+5-y,
	 				       (j+1)*STEP+5-x-sm, (i+1)*STEP+5-y-sm);
				break;
	 case 878:   pDC->Rectangle(j*STEP+5-x+sm, i*STEP+5-y,
	 				       (j+1)*STEP+5-x-sm, (i+1)*STEP+5-y);
				break;
	 case 879:   pDC->Rectangle(j*STEP+5-x, i*STEP+5-y+sm,
	 			        (j+1)*STEP+5-x, (i+1)*STEP+5-y-sm);
		        break;
	 case 880:   pDC->Rectangle(j*STEP+5-x+sm, i*STEP+5-y,
	 				       (j+1)*STEP+5-x-sm, (i+1)*STEP+5-y);
		        pDC->Rectangle(j*STEP+5-x, i*STEP+5-y+sm,
	 			        (j+1)*STEP+5-x, (i+1)*STEP+5-y-sm);
		        break;	 		 
   }   

   pDC->SelectObject(oldBrush);  
 
}



void CTestView::OnLButtonDblClk(UINT nFlags, CPoint point) 
{
	CScrollView::OnLButtonDblClk(nFlags, point);
}

void CTestView::OnPoint() 
{
		// TODO: Add your command handler code here
	pDoc->EditMode=1; // Add point to list mode
	    
}

void CTestView::OnFb() 
{
	// TODO: Add your command handler code here
	pDoc->EditMode=2; // Add forbidden zone  	
}

void CTestView::OnClear() 
{
	// TODO: Add your command handler code here
	pDoc->EditMode=3; // Clear mode
}

void CTestView::OnUpdatePoint(CCmdUI* pCmdUI) 
{
	// TODO: Add your command update UI handler code here
//	pCmdUI->SetButtonStyle(3,TBBS_CHECKBOX);
	//pCmdUI->WindowEnable(0);
}

void CTestView::OnLButtonUp(UINT nFlags, CPoint point) 
{
	// TODO: Add your message handler code here and/or call default
	
	CScrollView::OnLButtonUp(nFlags, point);
}

void CTestView::OnLButtonDown(UINT nFlags, CPoint point) 
{
	// TODO: Add your message handler code here and/or call default
	
	int t;
    
	//CTestDoc* pDoc = GetDocument();
	if (pDoc->EditMode==0) return;

	CPoint Pos=GetScrollPosition(); 	
    int index_X = (point.x+Pos.x-5)/STEP;
	int index_Y = (point.y+Pos.y-5)/STEP;
     
    if (pDoc->EditMode==1) t=980;
	if (pDoc->EditMode==2) t=999;
	if (pDoc->EditMode==3) t=0;
    
	if (  index_X && index_Y && 
	     (index_X<pDoc->PltSizeX-1) && (index_Y<pDoc->PltSizeY-1) )
		 
	{ if (!(pDoc->Data[index_Y][index_X] && t))
		{ pDoc->Data[index_Y][index_X]=t;
		  CDC *pDC=GetDC();
	      DrawElement (pDC,t,index_X,index_Y,1);
		}
	}
	
	CScrollView::OnLButtonDown(nFlags, point);
}

void CTestView::OnNorm() 
{
	// TODO: Add your command handler code here
    CTestDoc* pDoc = GetDocument();
	pDoc->EditMode=0;

}

void CTestView::OnUpdateViewToolbar(CCmdUI* pCmdUI) 
{   
//	if (EditMode) {
//		pCmdUI->SetCheck(

	pCmdUI->SetCheck(0);
}

void CTestView::OnUpdateNorm(CCmdUI* pCmdUI) 
{
//	if (EditMode) pCmdUI->SetCheck(1);
//	else pCmdUI->SetCheck(0);
}

void CTestView::OnAdd() 
{
	// TODO: Add your command handler code here
    int fl=0;
	//CTestDoc* pDoc = GetDocument();
    CDC *pDC=GetDC();

	for(int i=0;i<pDoc->PltSizeY;i++)
	   for (int j=0;j<pDoc->PltSizeX;j++)
	   {  if(pDoc->Data[i][j]==980)
			{ pDoc->Data[i][j]=pDoc->LastTraceNo; 
		      DrawElement(pDC,0,j,i,1); 
			  DrawElement(pDC,pDoc->LastTraceNo,j,i,1); 
			  fl=1;
			}
	   }
     if (fl) pDoc->LastTraceNo++;

}

void CTestView::OnStart() 
{
	// TODO: Add your command handler code here
	//m_dialog1.DoModal();
    //CTestDoc *pDoc=GetDocument();
	
	BeforeWin.DoModal();
	if(BeforeWin.SortType>2) return;

	MakeList();
	if(BeforeWin.SortType==1) SortList1();
    if(BeforeWin.SortType==2) SortList2();
	
	AfterWin.Summa=TraceByList();
	AfterWin.Cant=Un;
	AfterWin.Num=ListSize;
	AfterWin.Time=TimeInSeconds;
	AfterWin.Method=BeforeWin.SortType+1;    ///   Sort desc
	AfterWin.DoModal();

}



int CTestView::MakeTrassa(int begin, int end)
{
/**************************************************************************/
/*******    ПОДПРОГРАММА ПРОВЕДЕНИЯ ТРАССЫ МЕЖДУ ДВУМЯ ТОЧКАМИ   **********/
/**************************************************************************/

{ int i,j,k,q,w,cd,code,first,second,tr_num=0,x=0,y=0;
  int directions[4][2],coord[4][2],allow[4],stack[4][500][2];
  //CTestDoc *pDoc=GetDocument();
  CDC *pDC=GetDC();

  //int BData = pDoc->Data[points[begin][1]] [points[begin][0]];
  //int EData = pDoc->Data[points[end][1]] [points[end][0]];

 //*****  Установка начальных координат лучей, инициализация массивов *******
  coord[0][0]=points[begin][0];   coord[0][1]=points[begin][1];
  coord[1][0]=points[begin][0];   coord[1][1]=points[begin][1];
  coord[2][0]=points[end][0];     coord[2][1]=points[end][1];
  coord[3][0]=points[end][0];     coord[3][1]=points[end][1];

  for(i=0;i<4;i++)
  { stack[i][0][0]=coord[i][0];  stack[i][1][0]=0;
    stack[i][0][1]=coord[i][1];  stack[i][1][1]=0;
    allow[i]=1;
    for(j=0;j<2;j++)
    directions[i][j]=0;
  } tr_num++;
   //setfillstyle(SOLID_FILL,2);
  // deletemouse();
  // bar(89+coord[0][0]*9,87+coord[0][1]*7,94+coord[0][0]*9,90+coord[0][1]*7);
  // bar(89+coord[2][0]*9,87+coord[2][1]*7,94+coord[2][0]*9,90+coord[2][1]*7);
   //showmouse();
//********************  Установка Направлений лучей  **********************
  if(coord[0][0]<coord[2][0])
   {  directions[0][0]=1;    directions[3][0]=-1;  }
  if(coord[0][0]>coord[2][0])
   {  directions[0][0]=-1;   directions[3][0]=1;   }
  if(coord[0][0]==coord[2][0])
   {  if(coord[0][0]>pDoc->PltSizeX/2)q=-1; else q=1;
      directions[0][0]=q;    directions[3][0]=q;
   }
  if(coord[0][1]>coord[2][1])
   { directions[1][1]=-1;    directions[2][1]=1;   }
  if(coord[0][1]<coord[2][1])
   { directions[1][1]=1;     directions[2][1]=-1;  }
  if(coord[0][1]==coord[2][1])
   { if(coord[0][1]>pDoc->PltSizeY/2)q=-1; else q=1;
     directions[1][1]=q;     directions[2][1]=q;
   }
//************************ Движение Лучей  **********************************
   for(k=0;k<100;k++)
   for(i=0;i<4;i++)
   { if(i<2)w=2; else w=0;
     if(allow[i])
     { for(j=0;j<2;j++)
       coord[i][j]+=directions[i][j];
       stack[i][k+1][0]=coord[i][0];  stack[i][k+2][0]=0;
       stack[i][k+1][1]=coord[i][1];  stack[i][k+2][1]=0;
       if((pDoc->Data[coord[i][1]][coord[i][0]]>=520+w)&&   // Found way
	      (pDoc->Data[coord[i][1]][coord[i][0]]<=521+w))
	   {  first=i; 
	      second=pDoc->Data [coord[i][1]] [coord[i][0]]-520;
		  stack[i][k+1][0]=0;  stack[i][k+1][1]=0;
		  x=coord[i][0]; y=coord[i][1];  i=4; k=100; break;
       } 
	   if(pDoc->Data[coord[i][1]][coord[i][0]])
       { for(j=0;j<2;j++)
			{ coord[i][j]-=directions[i][j];
			  if(i==0)q=1;
			  if(i==1)q=0;
			  if(i==2)q=3;
			  if(i==3)q=2;
			  coord[i][j]+=directions[q][j];
			} 
		  stack[i][k+1][0]=coord[i][0];  stack[i][k+2][0]=0;
	      stack[i][k+1][1]=coord[i][1];  stack[i][k+2][1]=0;
	      if((pDoc->Data[coord[i][1]][coord[i][0]]>=520+w)&&
	         (pDoc->Data[coord[i][1]][coord[i][0]]<=521+w))
		  {  first=i; 
		     second=pDoc->Data[coord[i][1]][coord[i][0]]-520;
	         stack[i][k+1][0]=0;  stack[i][k+1][1]=0;
	         x=coord[i][0]; y=coord[i][1]; i=4; k=100; break;
		  } 
		  if(pDoc->Data[coord[i][1]][coord[i][0]])
		  { allow[i]=0;
	        for(j=0;j<2;j++)
				coord[i][j]-=directions[q][j];
		  }
		}
    //   deletemouse();
    //   bar(89+coord[i][0]*9,87+coord[i][1]*7,94+coord[i][0]*9,90+coord[i][1]*7);
	//   showmouse();
       DrawElement(pDC,520+i,coord[i][0],coord[i][1],1); 
	   pDoc->Data[coord[i][1]][coord[i][0]]=520+i;
	   if(pDoc->Pause) Sleep(10);
   } }
//*****************  Стираем лучи    *************************************
//  deletemouse();
//  setfillstyle(SOLID_FILL,14);
//  bar(89+points[begin][0]*9,87+points[begin][1]*7,94+points[begin][0]*9,90+points[begin][1]*7);
//  bar(89+points[end][0]*9,87+points[end][1]*7,94+points[end][0]*9,90+points[end][1]*7);
  for(i=0;i<pDoc->PltSizeY;i++)
  for(j=0;j<pDoc->PltSizeX;j++)
  if((pDoc->Data[i][j]>=520)&&(pDoc->Data[i][j]<=523))
  { pDoc->Data[i][j]=0;
    DrawElement(pDC,0,j,i,1); 

    //bar(89+j*9,87+i*7,94+j*9,90+i*7);
  } //showmouse();
  
  pDoc->Data[points[begin][1]] [points[begin][0]]=885;//BData;
  pDoc->Data[points[end][1]] [points[end][0]]=885;//EData;
  DrawElement(pDC,885,points[begin][0],points[begin][1],1); 
  DrawElement(pDC,885,points[end][0],points[end][1],1); 

//******************** Определяем трассу и рисуем ее  ********************
if(x&&y)
{ for(j=0;j<100;j++)
  if((stack[second][j][0]==x)&&(stack[second][j][1]==y))break;
  for(i=0;i<100;i++)if(!stack[first][i][0])break;
  for(k=0;k<j+1;k++)
  { stack[first][i+k][0]=stack[second][j-k][0];
    stack[first][i+k][1]=stack[second][j-k][1];
  } j=i+k;
  for(i=0;i<j;i++)
  { q=stack[first][i+1][0]-stack[first][i][0];
    w=stack[first][i+1][1]-stack[first][i][1];
    k=pDoc->Data[stack[first][i][1]][stack[first][i][0]];
    if(q&&((cd==1)||(cd==3))) { code=879;       }
    if((q==1)&&(cd==2))       { code=874; cd=1; }
    if((q==-1)&&(cd==2))      { code=876; cd=3; }
    if((q==1)&&(cd==4))       { code=875; cd=1; }
    if((q==-1)&&(cd==4))      { code=877; cd=3; }
    if(w&&((cd==2)||(cd==4))) { code=878;       }
    if((w==1)&&(cd==1))       { code=876; cd=4; }
    if((w==-1)&&(cd==1))      { code=877; cd=2; }
    if((w==1)&&(cd==3))       { code=874; cd=4; }
    if((w==-1)&&(cd==3))      { code=875; cd=2; }
    if(!i)
    { if(q==1)        { code=885; cd=1; }
      if(q==-1)       { code=887; cd=3; }
	  if(w==1)        { code=888; cd=4; }
      if(w==-1)       { code=886; cd=2; }
	} if(i==j-1)
    { if(cd==1)  code=887;
      if(cd==2)  code=888;
      if(cd==3)  code=885;
      if(cd==4)  code=886;
    } 
	  
    pDoc->Data[stack[first][i][1]][stack[first][i][0]]=code;	  
    DrawElement(pDC,code,stack[first][i][0],stack[first][i][1],1); 
  }
  
} else j=0;
  return(j);
}


//return(1);

}




void CTestView::MakeList()
{
  //CTestDoc *pDoc=GetDocument();
 	
  int JC,i,j,k,q,w,min,min_num,LastPos;
  LastPos=0;
  ListSize=0;
  points_num=0;
  for (JC=1;JC<50;JC++)
  {
	 for(i=0;i<pDoc->PltSizeY;i++)
	 for(j=0;j<pDoc->PltSizeX;j++)
	 if(pDoc->Data[i][j]==JC)
	 {  points[points_num][0]=j;
		points[points_num][1]=i;
		points_num++;
	 } //   points (LastPos-points_num) - list of points for current job

	 min=1000;
	 for(k=LastPos;k<points_num-1;k++)
	 { for(i=k+1;i<points_num;i++)
	   {  j=abs(points[k][0]-points[i][0])+abs(points[k][1]-points[i][1]);
		  if(j<min){ min=j; min_num=i; }
	   }
	   q=points[k+1][0];
	   w=points[k+1][1];
	   points[k+1][0]=points[min_num][0];
	   points[k+1][1]=points[min_num][1];
	   points[min_num][0]=q;
	   points[min_num][1]=w;
	   min=1000;
	 } //  points (LastPos-points_num) - optimized list for current job

	 for (i=LastPos;i<points_num-1; i++)
	 { List[ListSize][0]=i;
	   List[ListSize][1]=i+1;
	   ListSize++;
	 } // Add points to list
	 LastPos=points_num;
  }
}


void CTestView::SortList1()
{
  int min=1000,min_num,D;
  for (int i=0;i<ListSize;i++)
  { for (int j=i;j<ListSize;j++)
	  if ( (D=Distance(List[j][0],List[j][1])) <min )
	  { min=D; min_num=j; }
	int a=List[i][0];
	int b=List[i][1];
	List[i][0]=List[min_num][0];
	List[i][1]=List[min_num][1];
	List[min_num][0]=a;
	List[min_num][1]=b;
	min=1000;
  }

}


void CTestView::SortList2()
{
int max=-1,max_num,D;
  for (int i=0;i<ListSize;i++)
  { for (int j=i;j<ListSize;j++)
	  if ( (D=Distance (List[j][0],List[j][1])) > max )
	  { max=D; max_num=j; }
	int a=List[i][0];
	int b=List[i][1];
	List[i][0]=List[max_num][0];
	List[i][1]=List[max_num][1];
	List[max_num][0]=a;
	List[max_num][1]=b;
	max=-1;
  }
}

int CTestView::TraceByList()
{ 
  Un=0; 
  int summa=0;
  CTime t1= CTime::GetCurrentTime();
  
  for (int i=0;i<ListSize;i++)
  { int res=MakeTrassa(List[i][0],List[i][1]);
	if(res<1)Un++;
	else summa+=res;
  }
  
  CTime t2= CTime::GetCurrentTime();
  int s1=t1.GetSecond()+60*t1.GetMinute();
  int s2=t2.GetSecond()+60*t2.GetMinute();
  TimeInSeconds=s2-s1;

  return (summa);

}


int CTestView::Distance(int point1, int point2)
{ return (abs(points[point1][0]-points[point2][0])+
		  abs(points[point1][1]-points[point2][1]));

}

void CTestView::Trace()
{  
   MakeList();
   SortList1();
   TraceByList(); 

}

void CTestView::OnStartFirstsortmethod() 
{
	// TODO: Add your command handler code here
	MakeList();
	AfterWin.Summa=TraceByList();
	AfterWin.Cant=Un;
	AfterWin.Num=ListSize;
	AfterWin.Time=TimeInSeconds;
	AfterWin.Method=1;    ///   Without Sort
	AfterWin.DoModal();
	
}

void CTestView::OnStartSecondsortmethod() 
{
	// TODO: Add your command handler code here
	MakeList();
	SortList1();
	AfterWin.Summa=TraceByList();
	AfterWin.Cant=Un;
	AfterWin.Num=ListSize;
	AfterWin.Time=TimeInSeconds;
	AfterWin.Method=2;    ///   Sort asc
	AfterWin.DoModal();
}

void CTestView::OnStartThirdsortmethod() 
{
	// TODO: Add your command handler code here
	MakeList();
	SortList2();
	AfterWin.Summa=TraceByList();
	AfterWin.Cant=Un;
	AfterWin.Num=ListSize;
	AfterWin.Time=TimeInSeconds;
	AfterWin.Method=3;    ///   Sort desc
	AfterWin.DoModal();
}

void CTestView::OnStatistic() 
{
	// TODO: Add your command handler code here
	ResultDialog.DoModal();
	if (ResultDialog.graf)
		GrafWin.DoModal();

}

void CTestView::OnOptionsFieldoptions() 
{
	// TODO: Add your command handler code here
    //CTestDoc *pDoc=GetDocument();
	int OXS=pDoc->PltSizeX;
    int OYS=pDoc->PltSizeY;
	int OS=pDoc->step;

	OptionWin.DoModal();

	if( (OXS!=pDoc->PltSizeX) || (OYS!=pDoc->PltSizeY) )
	{   pDoc->OnNewDocument();
	    Invalidate();
	}
    if (OS!=pDoc->step) Invalidate();

}


int CTestView::OnCreate(LPCREATESTRUCT lpCreateStruct) 
{
	if (CScrollView::OnCreate(lpCreateStruct) == -1)
		return -1;
	
	// TODO: Add your specialized creation code here
    pDoc=GetDocument();
	pDoc->EditMode=0;
	pDoc->PltSizeX=74;
	pDoc->PltSizeY=42;	

	pDoc->step=10;
	pDoc->Critery=1;
	pDoc->Pause=true;
	pDoc->StatNo=0;
	for(int i=0;i<3;i++)
	{ pDoc->UPercent[i]=100;
	  pDoc->USumma[i]=0;
	  pDoc->UTime[i]=0;
	}

	OptionWin.pDoc = pDoc;
	BeforeWin.pDoc = pDoc;
	AfterWin.pDoc  = pDoc;
    ResultDialog.pDoc = pDoc;
	
	return 0;
}
