📄 多边形填充view.cpp
字号:
// 多边形填充View.cpp : implementation of the CMyView class
//
#include "stdafx.h"
#include "多边形填充.h"
#include "多边形填充Doc.h"
#include "多边形填充View.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
/////////////////////////////////////////////////////////////////////////////
// CMyView
IMPLEMENT_DYNCREATE(CMyView, CView)
BEGIN_MESSAGE_MAP(CMyView, CView)
//{{AFX_MSG_MAP(CMyView)
ON_WM_LBUTTONDOWN()
ON_WM_RBUTTONDOWN()
ON_WM_RBUTTONDBLCLK()
//}}AFX_MSG_MAP
// Standard printing commands
ON_COMMAND(ID_FILE_PRINT, CView::OnFilePrint)
ON_COMMAND(ID_FILE_PRINT_DIRECT, CView::OnFilePrint)
ON_COMMAND(ID_FILE_PRINT_PREVIEW, CView::OnFilePrintPreview)
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CMyView construction/destruction
CMyView::CMyView()
{
// TODO: add construction code here
i=0;
}
CMyView::~CMyView()
{
}
BOOL CMyView::PreCreateWindow(CREATESTRUCT& cs)
{
// TODO: Modify the Window class or styles here by modifying
// the CREATESTRUCT cs
return CView::PreCreateWindow(cs);
}
/////////////////////////////////////////////////////////////////////////////
// CMyView drawing
void CMyView::OnDraw(CDC* pDC)
{
CMyDoc* pDoc = GetDocument();
ASSERT_VALID(pDoc);
// TODO: add draw code for native data here
}
/////////////////////////////////////////////////////////////////////////////
// CMyView printing
BOOL CMyView::OnPreparePrinting(CPrintInfo* pInfo)
{
// default preparation
return DoPreparePrinting(pInfo);
}
void CMyView::OnBeginPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)
{
// TODO: add extra initialization before printing
}
void CMyView::OnEndPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)
{
// TODO: add cleanup after printing
}
/////////////////////////////////////////////////////////////////////////////
// CMyView diagnostics
#ifdef _DEBUG
void CMyView::AssertValid() const
{
CView::AssertValid();
}
void CMyView::Dump(CDumpContext& dc) const
{
CView::Dump(dc);
}
CMyDoc* CMyView::GetDocument() // non-debug version is inline
{
ASSERT(m_pDocument->IsKindOf(RUNTIME_CLASS(CMyDoc)));
return (CMyDoc*)m_pDocument;
}
#endif //_DEBUG
/////////////////////////////////////////////////////////////////////////////
// CMyView message handlers
void CMyView::OnLButtonDown(UINT nFlags, CPoint point)
{
// TODO: Add your message handler code here and/or call default
polyPoint=point;
Addspt(polyPoint);
CView::OnLButtonDown(nFlags, point);
}
void CMyView::OnRButtonDown(UINT nFlags, CPoint point)
{
// TODO: Add your message handler code here and/or call default
DrawPolygan(spt,i);
CView::OnRButtonDown(nFlags, point);
}
void CMyView::Addspt(CPoint point)
{
spt[i++]=point;
}
void CMyView::DrawPolygan(CPoint spt[], int i)
{
spt[i]=spt[0];
RedrawWindow();
CDC* pDC=GetDC();
CPen newpen(PS_SOLID,1,RGB(255,0,0));
CPen *old=pDC->SelectObject(&newpen);
pDC->Polyline(spt,i+1);
pDC->SelectObject(old);
ReleaseDC(pDC);
}
void CMyView::OnRButtonDblClk(UINT nFlags, CPoint point)
{
// TODO: Add your message handler code here and/or call default
seed=point;
ScanlineSeedfill(seed);
// Floodfill(seed );
CView::OnRButtonDblClk(nFlags, point);
}
/*void CMyView::Floodfill(CPoint point)
{
CWindowDC dc(this);
int fill=RGB(0,255,0);
int boundary=RGB(255,0,0);
int x, y, p0,pmin,pmax;
for(int m=0;m<=i-m;m++)
{
for(int n=0;n<=i-m;n++)
if(spt[n].y<spt[n+1].y)
{
p0=spt[n].y;
spt[n].y=spt[n+1].y;
spt[n+1].y=p0;
}
}
pmax=spt[0].y;
pmin=spt[i-1].y;
x=point.x;
y=point.y;
for(; y<pmax+1;y++)
{
int current = dc.GetPixel (x,y);
while ((current != boundary)&&(current!=fill))
{
dc.SetPixel(x,y,fill);
x++;
current=dc.GetPixel(x,y);
}
x=point.x;
x--;
current=dc.GetPixel(x,y);
while((current != boundary)&& (current!=fill))
{
dc.SetPixel(x,y,fill);
x--;
current=dc.GetPixel(x,y);
}
x=point.x;
}
x=point.x;
y=point.y-1;
for(;y>pmin-2;y--)
{
int current = dc.GetPixel (x,y);
while ((current != boundary)&&(current!=fill))
{
dc.SetPixel(x,y,fill);
x++;
current=dc.GetPixel(x,y);
}
x=point.x;
x--;
current=dc.GetPixel(x,y);
while((current != boundary)&& (current!=fill))
{
dc.SetPixel(x,y,fill);
x--;
current=dc.GetPixel(x,y);
}
x=point.x;
}
}*/
void CMyView::ScanlineSeedfill(CPoint point)
{
CDC* pDC = GetDC();
int x0,xl,xr,y0,x,y,xid;
int flag,xnextspan;
CStack s;
CPoint p;
unsigned long oldvalue;
int newvalue;
oldvalue=RGB(255,0,0);
newvalue=RGB(0,255,0);
s.Init ();
s.Push(point);
while (s.IsEmpty()==0)
{
s.Pop();
p=s.e;
pDC->SetPixel(p.x,p.y,newvalue);
x=p.x;
y=p.y;
x0=x+1;
while(pDC->GetPixel(x0,y)!=oldvalue)
{
pDC->SetPixel(x0,y,newvalue);
x0++;
}
xr=x0-1;
x0=x-1;
while(pDC->GetPixel(x0,y)!=oldvalue)
{
pDC->SetPixel(x0,y,newvalue);
x0--;
}
xl=x0+1;
y0=y;
for(int i=1;i>=-1;i-=2)
{
x0=xr;
y=y0+i;
while(x0>=xl)
{
flag=0;
while( (pDC->GetPixel(x0,y)!=oldvalue) && (pDC->GetPixel(x0,y)!=newvalue)&&(x0>xl))
{
if(flag==0)
{
flag=1;
xid=x0;
}
x0--;
}
if(flag==1)
{
s.Push (CPoint(xid,y));
flag=0;
}
xnextspan=x0;
while((pDC->GetPixel(x0,y)==oldvalue)||(pDC->GetPixel(x0,y)==newvalue)&&(x0>=xl))
x0--;
if(xnextspan==x0)
x0--;
}
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -