// kl2View.cpp : implementation of the CKl2View class
//

#include "stdafx.h"
#include "kl2.h"

#include "kl2Doc.h"
#include "kl2View.h"

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

/////////////////////////////////////////////////////////////////////////////
// CKl2View

IMPLEMENT_DYNCREATE(CKl2View, CView)

BEGIN_MESSAGE_MAP(CKl2View, CView)
	//{{AFX_MSG_MAP(CKl2View)
		// NOTE - the ClassWizard will add and remove mapping macros here.
		//    DO NOT EDIT what you see in these blocks of generated code!
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CKl2View construction/destruction

CKl2View::CKl2View()
{
	// TODO: add construction code here

}

CKl2View::~CKl2View()
{
}

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

	return CView::PreCreateWindow(cs);
}

/////////////////////////////////////////////////////////////////////////////
// CKl2View drawing

void CKl2View::OnDraw(CDC* pDC)
{
	int iW,iH,ix1,iy1;

	int Gd, Gm ;
    int J1[200][200];
    int J2[200][200];
    int Kode[7];
    int i,j,n,m,Mn,L,Sum,Lmax ;



	CKl2Doc* pDoc = GetDocument();
	ASSERT_VALID(pDoc);

	static int Sum_0_Color = { RGB(0,250,0) };
	static int Sum_1_Color = { RGB(233,13,1) };
	static int Sum_2_Color = { RGB(19,3,240) };

	CBrush  Sum_0_brush;
	CBrush  Sum_1_brush;
	CBrush  Sum_2_brush;
	CBrush* oldbrush;

	POINT pnt;

	CPen    Sum_0_pen;
	CPen    Sum_1_pen;
	CPen    Sum_2_pen;
	CPen*   oldpen;

	Sum_0_brush.CreateHatchBrush(HS_CROSS, Sum_0_Color);
	Sum_0_pen.CreatePen(PS_SOLID,1,Sum_0_Color);

	Sum_1_brush.CreateHatchBrush(HS_CROSS, Sum_1_Color);
	Sum_1_pen.CreatePen(PS_SOLID,1,Sum_1_Color);

	Sum_2_brush.CreateHatchBrush(HS_CROSS, Sum_2_Color);
	Sum_2_pen.CreatePen(PS_SOLID,1,Sum_2_Color);

	iW = pDC->GetDeviceCaps(HORZRES);
	iH = pDC->GetDeviceCaps(VERTRES);

	Lmax=200;

	  Kode[0]=1;
	  Kode[1]=1;
	  Kode[2]=0;
	  Kode[3]=2;
	  Kode[4]=1;
	  Kode[5]=2;
	  Kode[6]=1;

		for (i=0; i<Lmax; i++)
		{
			for (j=0; j<Lmax; j++)
			{
				J1[i][j]=0;
				J2[i][j]=0;
			}
		}

	  J1[Lmax / 2][ Lmax / 2] = 1;

	for (n=1; n<39; n++)
	{

		for (i=1; i<Lmax-1; i++)
		{
			for (j=1; j<Lmax-1; j++)
			{
				Sum = J1[i][j] + J1[i][j-1] + J1[i][j+1] + J1[i+1][j] + J1[i-1][j];

				if (Sum==6) { 
							J2[i][j]=Kode[0]; 
							}
				if (Sum==5) { 
							J2[i][j]=Kode[1]; 
							}
				if (Sum==4) { 
							J2[i][j]=Kode[2]; 
							}
				if (Sum==3) { 
							J2[i][j]=Kode[3]; 
							}
				if (Sum==2) { 
							J2[i][j]=Kode[4]; 
							}
				if (Sum==1) { 
							J2[i][j]=Kode[5]; 
							}
				if (Sum==0) { 
							J2[i][j]=Kode[6]; 
							}

				if (J2[i][j]==0) { 
									oldpen = pDC->SelectObject(&Sum_0_pen);
									oldbrush =  pDC->SelectObject(&Sum_0_brush);
									pnt.x = Lmax/2+j;
									pnt.y = Lmax/2+i;
									pDC->SetPixel(pnt,Sum_0_Color);
	//								pDC->Ellipse(pnt.x,pnt.y,pnt.x+2,pnt.y+2); 

								}
				if (J2[i][j]==1) { 
									oldpen = pDC->SelectObject(&Sum_1_pen);
									oldbrush =  pDC->SelectObject(&Sum_1_brush);
									pnt.x = Lmax/2+j;
									pnt.y = Lmax/2+i;
		//							pDC->Ellipse(pnt.x,pnt.y,pnt.x+2,pnt.y+2); 
									pDC->SetPixel(pnt,Sum_1_Color);
								}
				if (J2[i][j]==2) { 
									oldpen = pDC->SelectObject(&Sum_2_pen);
									oldbrush =  pDC->SelectObject(&Sum_2_brush);
	//								pDC->Ellipse(j,i,j+1,i+1); 
									pnt.x = Lmax/2+j;
									pnt.y = Lmax/2+i;
		//							pDC->Ellipse(pnt.x,pnt.y,pnt.x+2,pnt.y+2); 
									pDC->SetPixel(pnt,Sum_2_Color);
							}

					}
			}

			for (i=0; i<Lmax; i++)
			{
				for (j=0; j<Lmax; j++)
				{
					J1[i][j]=J2[i][j];
					J2[i][j]=0;
				}
			}
	}
}

/////////////////////////////////////////////////////////////////////////////
// CKl2View diagnostics

#ifdef _DEBUG
void CKl2View::AssertValid() const
{
	CView::AssertValid();
}

void CKl2View::Dump(CDumpContext& dc) const
{
	CView::Dump(dc);
}

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

/////////////////////////////////////////////////////////////////////////////
// CKl2View message handlers
