⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 polygon.cpp

📁 一个复杂的画图系统
💻 CPP
字号:
// Polygon.cpp: implementation of the CPolygon class.
//
//////////////////////////////////////////////////////////////////////

#include "stdafx.h"
#include "geditor.h"
#include "Polygon.h"
#include "prop.h"
#include "geditorView.h"
#include <cmath>
#ifdef _DEBUG
#undef THIS_FILE
static char THIS_FILE[]=__FILE__;
#define new DEBUG_NEW
#endif

//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////
IMPLEMENT_SERIAL(CPolygon, CShape,1)

CPolygon::CPolygon()
{
	edgeclr=0x00C0C0C0;
}

CPolygon::~CPolygon()
{
	m_tVtxArray.RemoveAll( );
	//m_RAry.RemoveAll();
	m_ptAry.RemoveAll();
}

void CPolygon::Draw(CDC* pDC)
{
	CBrush brush((COLORREF)m_color);
	CBrush* oldBrush = pDC->SelectObject(&brush);
	CPen pen(PS_SOLID,1,(COLORREF)m_color);
	CPen *oldPen = pDC->SelectObject(&pen);

	pDC->Polygon(&(m_tVtxArray[0]),m_tVtxArray.GetSize());
	if ((dragmode>0)&&(dragmode<11))
	{
		CPen pens(PS_SOLID,1,RGB(0,0,0));
		pDC->SelectObject(&pens);
		CBrush brus(RGB(255,255,255));
		pDC->SelectObject(&brus);
		int RecWd=4;
		int count=m_tVtxArray.GetSize();
		for (int i=0;i<count;i++)
			pDC->Rectangle(m_tVtxArray[i].x-RecWd,m_tVtxArray[i].y-RecWd,m_tVtxArray[i].x+RecWd,m_tVtxArray[i].y+RecWd);
		if (dragmode==1)
			UpdateProp(-1);
		else if (dragmode==2)
			UpdateProp(Curpt);
		DeleteObject(pens);
	};
	pDC->SelectObject(oldBrush);
	pDC->SelectObject(oldPen);
	DeleteObject(pen);

}

#ifdef _DEBUG
void CPolygon::AssertValid() const
{
	CShape::AssertValid();
	
}

void CPolygon::Dump(CDumpContext& dc) const
{
	CShape::Dump(dc);
}

#endif //_DEBUG

void CPolygon::Serialize(CArchive &ar)
{
	int nVtxCount;
	CPoint point;
	//Call base class Serialize()
	CObject::Serialize(ar);
	int i;
	
	if(ar.IsStoring())
	{ 
		nVtxCount = m_tVtxArray.GetSize();
		ar<<nVtxCount;
		for(i=0;i<nVtxCount;i++)
			ar<<m_tVtxArray[i].x<<m_tVtxArray[i].y;
		ar<<m_color<<dragmode<<name<<linesty;
	}
	else
	{
		ar>>nVtxCount;
		for(i=0;i<nVtxCount;i++)
		{
			ar>>point.x>>point.y;
			AddPoint(point);
		}
		ar>>m_color>>dragmode>>name>>linesty;		
	}

}

void CPolygon::dragobj(int leash,CDC *pDC, CPoint pt, CGeditorView *pView)
{//        刷新需要改进......
	if (leash==1)//约束轴
		pt.y=movept.y;
	else if (leash==2)
		pt.x=movept.x;
    invalid(pView);
	int m=m_tVtxArray.GetSize();
	if(dragmode==1) 
	{
		for(int i=0;i<m;i++)
		{
			m_tVtxArray[i].x+=pt.x-movept.x;
			m_tVtxArray[i].y+=pt.y-movept.y;
		};
		movept.x=pt.x;
		movept.y=pt.y;
	} else if (dragmode==2) 
		m_tVtxArray[Curpt]=pt;
	Draw(pDC);
}

int CPolygon::InTest(CPoint pt)
{
	int Rng=4;
	int m=m_tVtxArray.GetSize();
	int i=0;
	BOOL None=TRUE;
	while((i<m)&&(None))
	{
		if ((abs(pt.x-m_tVtxArray[i].x)<Rng)&&(abs(pt.y-m_tVtxArray[i].y)<Rng))
			None=FALSE;
		i++;
	};
	if (!None) 
	{
		::SetCursor(AfxGetApp()->LoadStandardCursor(IDC_SIZEALL));
		Curpt=i-1;
		return 2;
	};
	return PtInPoly(pt);
	return 0;
}

int CPolygon::PtInPoly(CPoint &pt)
{
	int x=0;
	int num=0,j=0;
	float deltax=0,deltay=0,yt=0;
	int m=m_tVtxArray.GetSize();
	for (int i=0;i<m;i++)
	{
		if (i==(m-1)) j=0;
		else j=i+1;
		if (((pt.y<=m_tVtxArray[j].y)&&(pt.y>=m_tVtxArray[i].y))
			|((pt.y>=m_tVtxArray[j].y)&&(pt.y<=m_tVtxArray[i].y)))
		{
			deltax=(float)(m_tVtxArray[j].x-m_tVtxArray[i].x);
			deltay=(float)(m_tVtxArray[j].y-m_tVtxArray[i].y);
			yt=(float)(pt.y-m_tVtxArray[i].y);
			x=(int)((deltax/deltay)*yt)+m_tVtxArray[i].x;
			if(pt.x<=x)
				num++; 
		};
	};
	if ((num%2)==1)//取模
	{
		::SetCursor(AfxGetApp()->LoadCursor(IDC_CURSEL));
		return 1;
	};
	return 0;
}

void CPolygon::rotate(int rsel, CDC *pDC, CPoint pt, CGeditorView *pView)
{
	if(dragmode==2)
	{
		invalid(pView);
		double r=m_ptAry[Curpt].r;
		double R=sqrt((pt.x-basept.x)*(pt.x-basept.x)+(pt.y-basept.y)*(pt.y-basept.y));
		double cos01=m_ptAry[Curpt].pcos;//原角度
		double sin01=m_ptAry[Curpt].psin;
		double cos02=(double)(pt.x-basept.x)/R;//转动后角度
		double sin02=(double)(pt.y-basept.y)/R;
		double cosdelt0=cos02*cos01+sin02*sin01;
		double sindelt0=sin02*cos01-cos02*sin01;
		int m=m_tVtxArray.GetSize();
		for (int i=0;i<m;i++)
		{
			double cos0=m_ptAry[i].pcos;
			double sin0=m_ptAry[i].psin;
			cos0=cos0*cosdelt0-sin0*sindelt0;
			sin0=sin0*cosdelt0+cos0*sindelt0;
			double jd=0.5;
			double x=m_ptAry[i].r*cos0+basept.x;
			double y=m_ptAry[i].r*sin0+basept.y;
			m_tVtxArray[i].x=(int)(x+jd);
			m_tVtxArray[i].y=(int)(y+jd);
		};
		Draw(pDC);
	};

}

void CPolygon::CalForRot(CPoint &pt)
{
	movept=m_tVtxArray[Curpt];
	basept.x=290;
	basept.y=200;
//	m_RAry.RemoveAll();
	m_ptAry.RemoveAll();
	struct PTS ps;
	for (int i=0;i<m_tVtxArray.GetSize();i++)
	{
		ps.r=sqrt((m_tVtxArray[i].x-basept.x)*(m_tVtxArray[i].x-basept.x)+(m_tVtxArray[i].y-basept.y)*(m_tVtxArray[i].y-basept.y));
		ps.pcos=(double)(m_tVtxArray[i].x-basept.x)/ps.r;
		ps.psin=(double)(m_tVtxArray[i].y-basept.y)/ps.r;
		m_ptAry.Add(ps);//在生成时单独计算可减少计算量
	};
}


CShape * CPolygon::copys(TCHAR &ch)
{
	CPolygon *p;
    p=new CPolygon;
	int m=m_tVtxArray.GetSize();
	CPoint pt;
	for(int i=0;i<m;i++)
	{
		pt=m_tVtxArray[i];
		pt.x+=10;
		pt.y+=10;
		p->m_tVtxArray.Add(pt);
	};
	ch='P';
	return p;

}

void CPolygon::Mirror(int syd, CDC *pDC, CPoint &basept, CGeditorView *pView)
{
	invalid(pView);
	int m=m_tVtxArray.GetSize();
	int i=0;
	int txy=0;
	switch(syd)
	{
	case 0://x轴为对称轴
		for(i=0;i<m;i++)
			m_tVtxArray[i].y=2*basept.y-m_tVtxArray[i].y;
		break;
	case 1://y轴为对称轴
		for(i=0;i<m;i++)
			m_tVtxArray[i].x=2*basept.x-m_tVtxArray[i].x;
		break;
	case 2://原点对称
		for(i=0;i<m;i++)
		{
			m_tVtxArray[i].y=2*basept.y-m_tVtxArray[i].y;
			m_tVtxArray[i].x=2*basept.x-m_tVtxArray[i].x;
		};
		break;
	case 3://自对称
		break;
	default:
		break;
	};
	Draw(pDC);
}

void CPolygon::ChangeBase(int x,int y)
{
	movept=m_tVtxArray[Curpt];
	basept.x=x;
	basept.y=y;
	m_ptAry.RemoveAll();
	struct PTS ps;
	for (int i=0;i<4;i++)
	{
		ps.r=sqrt((m_tVtxArray[i].x-basept.x)*(m_tVtxArray[i].x-basept.x)+(m_tVtxArray[i].y-basept.y)*(m_tVtxArray[i].y-basept.y));
		ps.pcos=(double)(m_tVtxArray[i].x-basept.x)/ps.r;
		ps.psin=(double)(m_tVtxArray[i].y-basept.y)/ps.r;
		m_ptAry.Add(ps);//在生成时单独计算可减少计算量
	};
}

void CPolygon::invalid(CGeditorView *pView)
{
	CPoint apoly[4];
	CRgn* pRgne=new CRgn();
	int m=m_tVtxArray.GetSize();
	pRgne->CreatePolygonRgn(&(m_tVtxArray[0]),m,ALTERNATE);
	pView->InvalidateRgn(pRgne);
	delete pRgne;
	CRgn* pRgns;
	int j=0;
	CPoint spt,ept;
	for (int i=0;i<m;i++)
	{
		apoly[0].x=m_tVtxArray[i].x-10;
		apoly[1].x=m_tVtxArray[i].x-10;
		apoly[2].x=m_tVtxArray[i].x+10;
		apoly[3].x=m_tVtxArray[i].x+10;
		apoly[0].y=m_tVtxArray[i].y-10;
		apoly[1].y=m_tVtxArray[i].y+10;
		apoly[2].y=m_tVtxArray[i].y+10;
		apoly[3].y=m_tVtxArray[i].y-10;
		pRgns=new CRgn();
		pRgns->CreatePolygonRgn(apoly,4,ALTERNATE);
		pView->InvalidateRgn(pRgns);
		if (i==(m-1))
			j=0;
		else j=i+1;
		spt=m_tVtxArray[i];
		ept=m_tVtxArray[j];
		int itwd=width/2+1;
		if ((spt.x>ept.x)&&(spt.y>ept.y)||
		(spt.x<ept.x)&&(spt.y<ept.y)) {
		apoly[0].x=spt.x-itwd;
		apoly[1].x=spt.x+itwd;
		apoly[2].x=ept.x+itwd;
		apoly[3].x=ept.x-itwd;
		apoly[0].y=spt.y+itwd;
		apoly[1].y=spt.y-itwd;
		apoly[2].y=ept.y-itwd;
		apoly[3].y=ept.y+itwd;
	} else {
		apoly[0].x=spt.x-itwd;
		apoly[1].x=spt.x+itwd;
		apoly[2].x=ept.x+itwd;
		apoly[3].x=ept.x-itwd;
		apoly[0].y=spt.y-itwd;
		apoly[1].y=spt.y+itwd;
		apoly[2].y=ept.y+itwd;
		apoly[3].y=ept.y-itwd;
	};
		pRgns=new CRgn();
		pRgns->CreatePolygonRgn(apoly,4,ALTERNATE);
		pView->InvalidateRgn(pRgns);
	};
	delete pRgns;
}

void CPolygon::InitProp()
{
	item.mask=LVIF_TEXT|LVIF_STATE;
	item.state=0;
	item.stateMask=0;

	item.iItem=0;
	item.iSubItem=0;
	item.pszText="Ver[0].X";
	sprintf(svalue,"%3d",m_tVtxArray[0].x);

	item.iItem=1;
	item.iSubItem=0;
	item.pszText="Ver[0].Y";
	sprintf(svalue,"%3d",m_tVtxArray[0].y);
}

void CPolygon::UpdateProp(int sel)
{
}

void CPolygon::AddVert()
{
	int Iitem=m_tVtxArray.GetSize()-1;
	item.iItem=Iitem*2;
	item.iSubItem=0;
	sprintf(svalue,"%1d",Iitem);
	char sring[7];
	sring[0]='V';
	sring[1]='e';
	sring[2]='r';
	sring[3]='[';
	sring[4]=svalue[0];
	sring[5]=']';
	sring[6]='.';
	sring[7]='X';
	item.pszText=sring;
	sprintf(svalue,"%3d",m_tVtxArray[Iitem].x);
	item.iItem++;
	item.iSubItem=0;
	sring[7]='Y';
	item.pszText=sring;
	sprintf(svalue,"%3d",m_tVtxArray[Iitem].y);

}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -