#include "stdafx.h"
#include "2DObject.h"

/************************************************************************
¶þÎ¬Èý½ÇÐÎÀà                                                                      
************************************************************************/
C2DTriangle::C2DTriangle()
{
	m_arPt.RemoveAll();
	m_byR = g_2DObj.m_byR[3];
	m_byG = g_2DObj.m_byG[3];
	m_byB = g_2DObj.m_byB[3];
	m_byLineStyle = g_2DObj.m_byLineStyle[3];
	m_wLineWidth  = g_2DObj.m_wLineWidth[3];
}

C2DTriangle::C2DTriangle(CArray<CPoint, CPoint&>& arPt)
{
	m_arPt.Copy(arPt);
	m_byR = g_2DObj.m_byR[3];
	m_byG = g_2DObj.m_byG[3];
	m_byB = g_2DObj.m_byB[3];
	m_byLineStyle = g_2DObj.m_byLineStyle[3];
	m_wLineWidth  = g_2DObj.m_wLineWidth[3];
}

C2DTriangle::~C2DTriangle()
{

}

void C2DTriangle::Serialize( CFile* pFile, BOOL bSave /*= TRUE*/ )
{
	if (bSave)
	{
		C2DLine::Serialize(pFile,bSave);
		int nCount = (int)m_arPt.GetCount();
		pFile->Write(&nCount, sizeof(int));
		for (int i=0; i!=nCount; ++i)
		{
			CPoint pt;
			pt = m_arPt.GetAt(i);
			pFile->Write(&pt, sizeof(CPoint));
		}
	} 
	else
	{
		C2DLine::Serialize(pFile,bSave);
		int nCount = 0;
		pFile->Read(&nCount, sizeof(int));
		for (int i=0; i != nCount; ++i)
		{
			CPoint pt;
			pFile->Read(&pt, sizeof(CPoint));
			m_arPt.Add(pt);
		}
	}
}

void C2DTriangle::Draw( CDC* pDC )
{
	CPen pen(m_byLineStyle,m_wLineWidth,GetClrRefFromByte(m_byR,m_byG,m_byB));
	CPen* pOldPen = pDC->SelectObject(&pen);
	CPoint pt0,pt1,pt2;
	int nCount = (int)m_arPt.GetCount();
	if (nCount == 3)
	{
		pDC->MoveTo(m_arPt.GetAt(0));
		for (int i=1; i!=nCount; ++i)
		{
			pDC->LineTo(m_arPt.GetAt(i));
		}
		pDC->LineTo(m_arPt.GetAt(0));
	}
	pDC->SelectObject(pOldPen);
}

UINT C2DTriangle::GetClass()
{
	return CLASS_DRAW_TRIANGLE;
}


void C2DTriangle::GetPoint( int nIndex,int& x,int& y )
{
	if (nIndex>=m_arPt.GetCount() || nIndex<0)
	{
		return;
	}
	CPoint pt = m_arPt.GetAt(nIndex);
	x = pt.x;
	y = pt.y;
}