www.pudn.com > MapDB.rar > MapObject.cpp


#include "stdafx.h" 
#include "mapobject.h" 
#include "math.h" 
 
#ifdef _DEBUG 
#define new DEBUG_NEW 
#undef THIS_FILE 
static char THIS_FILE[] = __FILE__; 
#endif 
 
#define CPTSIZE		6 
 
IMPLEMENT_SERIAL(CMPoint,CObject,1) 
 
CMPoint::CMPoint() 
{ 
	r = 0; 
	c = 0; 
	x = 0; 
	y = 0; 
	xt = 0.0; 
	yt = 0.0; 
	xi = 0.0; 
	yi = 0.0; 
} 
 
void CMPoint::Serialize( CArchive& ar ) 
{ 
   CObject::Serialize(ar); 
 
   if ( ar.IsStoring() ) 
   { 
		ar << r << c << x << y << xi << yi; 
   } 
   else 
   { 
		ar >> r >> c >> x >> y >> xi >> yi; 
   } 
} 
 
long RD2L(double d) 
{ 
	CString s,o; 
	s.Format("%lf",d); 
	if ( s.Find('.') < 0 ) 
		return (long)d; 
	if ( s.Find('.') == 0 ) 
		return (long)(d + 0.4); 
	o = s.Mid(s.Find('.') - 1,1); 
	if ( o.FindOneOf("02468") ) 
		return (long)(d + 0.4); 
	else 
		return (long)(d + 0.5); 
} 
 
IMPLEMENT_SERIAL(CMapObject,CObject,1) 
 
CMapObject::CMapObject() 
{ 
} 
 
CMapObject::~CMapObject() 
{ 
	for ( int count = 0;count < m_mpts.GetSize();count ++) 
		delete m_mpts[count]; 
} 
 
void CMapObject::Serialize( CArchive& ar ) 
{ 
   CObject::Serialize(ar); 
 
   if ( ar.IsStoring() ) 
   { 
	   ar << ID; 
	   ar << m_titles.GetSize(); 
	   for (int count = 0;count < m_titles.GetSize();count ++) 
		   ar << m_titles[count]; 
	   ar << m_mpts.GetSize(); 
	   for (count = 0;count < m_mpts.GetSize();count ++) 
		   ar << m_mpts[count]; 
  } 
   else 
   { 
	   long titlesum,mptsum; 
	   ar >> ID; 
	   ar >> titlesum; 
	   for (int count = 0;count < titlesum;count ++) 
	   { 
		   DWORD title; 
		   ar >> title; 
		   m_titles.Add(title);; 
	   } 
	   ar >> mptsum; 
	   for (count = 0;count < mptsum;count ++) 
	   { 
		   CMPoint * mpt; 
		   ar >> mpt; 
		   m_mpts.Add(mpt); 
	   } 
   } 
} 
 
void CMapObject::DrawVector(CDC *pDC, double sx, double sy, double bx, double by) 
{ 
	if ( ! m_mpts.GetSize() ) 
		return; 
 
	if ( m_mpts.GetSize() < 2 ) 
	{ 
		CMPoint * mpt = m_mpts[0]; 
		pDC->SetPixel(RD2L(mpt->c * sx + bx),RD2L(mpt->r * sy + by),RGB(0,0,0)); 
		pDC->MoveTo(RD2L(mpt->c * sx + bx),RD2L((mpt->r) * sy + by - CPTSIZE));	 
		pDC->LineTo(RD2L((mpt->c) * sx + bx - CPTSIZE * sqrt(3) / 2.0),RD2L((mpt->r) * sy + by + CPTSIZE / 2.0)); 
		pDC->LineTo(RD2L((mpt->c) * sx + bx + CPTSIZE * sqrt(3) / 2.0),RD2L((mpt->r) * sy + by + CPTSIZE / 2.0)); 
		pDC->LineTo(RD2L(mpt->c * sx + bx),RD2L((mpt->r) * sy + by - CPTSIZE));	 
		return; 
	} 
 
	pDC->MoveTo((int)(m_mpts[0]->c * sx + bx + 0.5),(int)(m_mpts[0]->r * sy + by + 0.5));	 
	for ( int count = 0;count < m_mpts.GetSize();count ++) 
	{ 
		CMPoint * mpt = m_mpts[count]; 
		pDC->LineTo((int)(mpt->c * sx + bx + 0.5),(int)(mpt->r * sy + by + 0.5)); 
	} 
 
} 
 
BOOL CMapObject::IsPlane() 
{ 
	for (int count = 0;count < m_titles.GetSize();count += 2) 
	{ 
		DWORD title = m_titles[count]; 
		if ( title >= OBJTITLE_PLANEOBJECT && title < OBJTITLE_POINTOBJECT ) 
			return TRUE; 
	} 
	return FALSE; 
} 
 
BOOL CMapObject::IsLine() 
{ 
	for (int count = 0;count < m_titles.GetSize();count += 2) 
	{ 
		DWORD title = m_titles[count]; 
		if ( title >= OBJTITLE_LINEOBJECT && title < OBJTITLE_PLANEOBJECT ) 
			return TRUE; 
	} 
	return FALSE; 
} 
 
BOOL CMapObject::GetPolyRgn(CRgn * rgn) 
{ 
	CPoint * pts = new CPoint[m_mpts.GetSize()]; 
	for (int count = 0;count < m_mpts.GetSize();count ++) 
	{ 
		CMPoint * mpt = m_mpts[count]; 
		pts[count].x = mpt->x; 
		pts[count].y = mpt->y; 
	} 
	BOOL bOK = rgn->CreatePolygonRgn(pts,m_mpts.GetSize(),ALTERNATE); 
	delete [] pts; 
	return bOK; 
} 
 
BOOL CMapObject::GetRect(CRect & rect) 
{ 
	if ( ! m_mpts.GetSize() ) 
		return FALSE; 
 
	if ( ! m_bound.IsRectNull() ) 
	{ 
		rect = m_bound; 
		return TRUE; 
	} 
 
	double xmin = 1E12,ymin = 1E12,xmax = 1E-12,ymax = 1E-12; 
	for (int count = 0;count < m_mpts.GetSize();count ++) 
	{ 
		CMPoint * mpt = m_mpts[count]; 
 
		if ( mpt->x < xmin ) 
			xmin = mpt->x; 
		if ( mpt->x > xmax ) 
			xmax = mpt->x; 
 
		if ( mpt->y < ymin ) 
			ymin = mpt->y; 
		if ( mpt->y > ymax ) 
			ymax = mpt->y; 
	} 
	 
	rect.SetRect((int)xmin,(int)ymin,(int)xmax,(int)ymax); 
 
	m_bound = rect; 
 
	return TRUE; 
} 
 
BOOL CMapObject::GetCenter(CPoint & pt) 
{ 
	if ( ! m_mpts.GetSize() ) 
		return FALSE; 
 
	if ( m_center.x || m_center.y ) 
	{ 
		pt.x = m_center.x; 
		pt.y = m_center.y; 
		return TRUE; 
	} 
 
	double x = 0; 
	double y = 0; 
	for (int count = 0;count < m_mpts.GetSize();count ++) 
	{ 
		CMPoint * mpt = m_mpts[count]; 
		x += mpt->x; 
		y += mpt->y; 
	} 
	 
	pt.x = (int)(x / m_mpts.GetSize()); 
	pt.y = (int)(y / m_mpts.GetSize()); 
 
	m_center.x = pt.x; 
	m_center.y = pt.y; 
 
	return TRUE; 
}