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

📄 filldlg.cpp

📁 用于内存中任意四边形的填充,无论凹凸四边形都适用
💻 CPP
📖 第 1 页 / 共 2 页
字号:
// FillDlg.cpp : implementation file
//

#include "stdafx.h"
#include "Fill.h"
#include "FillDlg.h"

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

CPoint point[4];
CDC* pDC;
int **region;
UINT height,width;
int color;
/////////////////////////////////////////////////////////////////////////////
// CAboutDlg dialog used for App About

class CAboutDlg : public CDialog
{
public:
	CAboutDlg();

// Dialog Data
	//{{AFX_DATA(CAboutDlg)
	enum { IDD = IDD_ABOUTBOX };
	//}}AFX_DATA

	// ClassWizard generated virtual function overrides
	//{{AFX_VIRTUAL(CAboutDlg)
	protected:
	virtual void DoDataExchange(CDataExchange* pDX);    // DDX/DDV support
	//}}AFX_VIRTUAL

// Implementation
protected:
	//{{AFX_MSG(CAboutDlg)
	//}}AFX_MSG
	DECLARE_MESSAGE_MAP()
};

CAboutDlg::CAboutDlg() : CDialog(CAboutDlg::IDD)
{
	//{{AFX_DATA_INIT(CAboutDlg)
	//}}AFX_DATA_INIT
}

void CAboutDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CAboutDlg)
	//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CAboutDlg, CDialog)
	//{{AFX_MSG_MAP(CAboutDlg)
		// No message handlers
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CFillDlg dialog

CFillDlg::CFillDlg(CWnd* pParent /*=NULL*/)
	: CDialog(CFillDlg::IDD, pParent)
{
	//{{AFX_DATA_INIT(CFillDlg)
	m_height = 0;
	m_width = 0;
	m_color = 0;
	//}}AFX_DATA_INIT
	// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
	m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}

void CFillDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CFillDlg)
	DDX_Text(pDX, IDC_EDIT1, m_height);
	DDX_Text(pDX, IDC_EDIT2, m_width);
	DDX_Text(pDX, IDC_EDIT3, m_color);
	//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CFillDlg, CDialog)
	//{{AFX_MSG_MAP(CFillDlg)
	ON_WM_SYSCOMMAND()
	ON_WM_PAINT()
	ON_WM_QUERYDRAGICON()
	ON_BN_CLICKED(ID_FILL, OnFill)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CFillDlg message handlers

BOOL CFillDlg::OnInitDialog()
{
	CDialog::OnInitDialog();

	// Add "About..." menu item to system menu.

	// IDM_ABOUTBOX must be in the system command range.
	ASSERT((IDM_ABOUTBOX & 0xFFF0) == IDM_ABOUTBOX);
	ASSERT(IDM_ABOUTBOX < 0xF000);

	CMenu* pSysMenu = GetSystemMenu(FALSE);
	if (pSysMenu != NULL)
	{
		CString strAboutMenu;
		strAboutMenu.LoadString(IDS_ABOUTBOX);
		if (!strAboutMenu.IsEmpty())
		{
			pSysMenu->AppendMenu(MF_SEPARATOR);
			pSysMenu->AppendMenu(MF_STRING, IDM_ABOUTBOX, strAboutMenu);
		}
	}

	// Set the icon for this dialog.  The framework does this automatically
	//  when the application's main window is not a dialog
	SetIcon(m_hIcon, TRUE);			// Set big icon
	SetIcon(m_hIcon, FALSE);		// Set small icon
	
	// TODO: Add extra initialization here
	pDC=GetDC();
	point[0].x=30;
	point[0].y=30;
	point[1].x=80;
	point[1].y=60;
	point[2].x=100;
	point[2].y=90;
	point[3].x=50;
	point[3].y=80;
    UpdateData(true);		/*CString str;
	int y;
	region[0][0]=8;
	y=region[0][0];
	str.Format("%d",y);
	AfxMessageBox(str);*/
	return TRUE;  // return TRUE  unless you set the focus to a control
}

void CFillDlg::OnSysCommand(UINT nID, LPARAM lParam)
{
	if ((nID & 0xFFF0) == IDM_ABOUTBOX)
	{
		CAboutDlg dlgAbout;
		dlgAbout.DoModal();
	}
	else
	{
		CDialog::OnSysCommand(nID, lParam);
	}
}

// If you add a minimize button to your dialog, you will need the code below
//  to draw the icon.  For MFC applications using the document/view model,
//  this is automatically done for you by the framework.

void CFillDlg::OnPaint() 
{
	if (IsIconic())
	{
		CPaintDC dc(this); // device context for painting

		SendMessage(WM_ICONERASEBKGND, (WPARAM) dc.GetSafeHdc(), 0);

		// Center icon in client rectangle
		int cxIcon = GetSystemMetrics(SM_CXICON);
		int cyIcon = GetSystemMetrics(SM_CYICON);
		CRect rect;
		GetClientRect(&rect);
		int x = (rect.Width() - cxIcon + 1) / 2;
		int y = (rect.Height() - cyIcon + 1) / 2;

		// Draw the icon
		dc.DrawIcon(x, y, m_hIcon);
	}
	else
	{
		CDialog::OnPaint();
	}
}

// The system calls this to obtain the cursor to display while the user drags
//  the minimized window.
HCURSOR CFillDlg::OnQueryDragIcon()
{
	return (HCURSOR) m_hIcon;
}

void CFillDlg::OnFill() 
{

	//TRACE("DDDDhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhh%d",2);
	int i,j;
	int i1,j1;
	float k[6];
	float b[6];
	int line=4;
	CPoint pt1[3],pt2[3];
	float k1[3],b1[3],k2[3],b2[3];
	CPoint tripts[3];
	bool istriangle=false;
	float k0[3],b0[3];
	int max,min;
	float a[3];
	bool outer=true;
    float a1,a2;
		
	if (point[0].x==point[1].x)
	{
		point[0].x+=1;
	}
	if (point[0].x==point[2].x)
	{
		point[0].x-=2;
	}
	if (point[0].x==point[3].x)
	{
        point[3].x+=1;
	}
	if (point[1].x==point[2].x)
	{		
        point[1].x+=1;
	}
	if (point[2].x==point[3].x)
	{
		point[2].x+=1;
       
	}
	if (point[3].x==point[1].x)
	{	
        point[1].x-=2;
	}
	UpdateData(true);
	height=m_height;
	width=m_width;
	color=m_color;
	UpdateData(false);
	
	region=new int*[height];
	for (i=0;i<height;i++)
	{
		*(region+i)=new int[width];
	}
	
	for (i=0;i<height;i++)
		for (j=0;j<width;j++)
		{
			region[i][j]=0;
		}
	
//////////////////////////////////////	
    for (i1=0;i1<2;i1++)
	{
		if (i1==0)
		{
			j1=1;
		} 
		else
		{
			j1=0;
		}
		a[0]=float(point[i1].y-point[j1].y)/float(point[i1].x-point[j1].x+0.0000001);
		a[1]=float(point[i1].y-point[2].y)/float(point[i1].x-point[2].x+0.0000001);
		a[2]=float(point[i1].y-point[3].y)/float(point[i1].x-point[3].x+0.0000001);
        if (a[0]==a[1]||a[1]==a[2]||a[2]==a[0])
        {
			if (a[0]==a[1])
			{				
				if (point[i1].x<point[j1].x)
				{
					min=i1;
				} 
				else
				{
					min=j1;
				}
				if (point[min].x>point[2].x)
				{
					min=2;
				} 
				tripts[0]=point[min];
				if (point[i1].x>point[j1].x)
				{
					max=i1;
				} 
				else
				{
					max=j1;
				}
				if (point[max].x<point[2].x)
				{
					max=2;
				} 
				tripts[1]=point[max];
				tripts[2]=point[3];
			}
			
			if (a[1]==a[2])
			{
				if (point[i1].x<point[2].x)
				{
					min=i1;
				} 
				else
				{
					min=2;
				}
				if (point[min].x>point[3].x)
				{
					min=3;
				} 
				tripts[0]=point[min];
				if (point[i1].x>point[2].x)
				{
					max=i1;
				} 
				else
				{
					max=2;
				}
				if (point[max].x<point[3].x)
				{
					max=3;
				} 
				tripts[1]=point[max];				
				tripts[2]=point[j1];
			}
			
			if (a[0]==a[2])
			{
				if (point[i1].x<point[j1].x)
				{
					min=i1;
				} 
				else
				{
					min=j1;
				}
				if (point[min].x>point[3].x)
				{
					min=3;
				} 
				tripts[0]=point[min];
				if (point[i1].x>point[j1].x)
				{
					max=i1;
				} 
				else
				{
					max=j1;
				}
				if (point[max].x<point[3].x)
				{
					max=3;
				} 
				tripts[1]=point[max];				
				tripts[2]=point[2];
			}
			
			k0[0]=float(tripts[1].y-tripts[0].y)/float(tripts[1].x-tripts[0].x+0.0000001);
			k0[1]=float(tripts[2].y-tripts[1].y)/float(tripts[2].x-tripts[1].x+0.0000001);
			k0[2]=float(tripts[2].y-tripts[0].y)/float(tripts[2].x-tripts[0].x+0.0000001);
			b0[0]=tripts[0].y-k0[0]*tripts[0].x;
			b0[1]=tripts[1].y-k0[1]*tripts[1].x;
            b0[2]=tripts[2].y-k0[2]*tripts[2].x;
			istriangle=true;
			break;
        } 
	}
	////////////////	
    if (istriangle)
    {
		triangleFill(tripts,k0,b0);
		for (i=0;i<height;i++)
		{
			for (j=0;j<width;j++)
			{
				if (region[i][j]==m_color)
				{
					pDC->SetPixel(j,i,RGB(255,0,0));
				} 
				else
				{
					pDC->SetPixel(j,i,RGB(0,0,255));
				}
			}
		}
    } 
    else
    {		
		k[0]=float(point[1].y-point[0].y)/float(point[1].x-point[0].x+0.0000001);
		k[1]=float(point[2].y-point[1].y)/float(point[2].x-point[1].x+0.0000001);
		k[2]=float(point[3].y-point[2].y)/float(point[3].x-point[2].x+0.0000001);
		k[3]=float(point[0].y-point[3].y)/float(point[0].x-point[3].x+0.0000001);
		k[4]=float(point[0].y-point[2].y)/float(point[0].x-point[2].x+0.0000001);
		k[5]=float(point[1].y-point[3].y)/float(point[1].x-point[3].x+0.0000001);
		b[0]=point[1].y-k[0]*point[1].x;
		b[1]=point[2].y-k[1]*point[2].x;
		b[2]=point[3].y-k[2]*point[3].x;
		b[3]=point[0].y-k[3]*point[0].x;
		b[4]=point[2].y-k[4]*point[2].x;
		b[5]=point[3].y-k[5]*point[3].x;
		///////////////////
		a1=point[1].y-k[4]*point[1].x-b[4];
		a2=point[3].y-k[4]*point[3].x-b[4];
		if (a1*a2>0)
		{
			outer=false;
			line=5;
		} 
		a1=point[0].y-k[5]*point[0].x-b[5];
		a2=point[2].y-k[5]*point[2].x-b[5];
		if (a1*a2>0)
		{
			outer=false;
			line=4;
		} 
		///////////////
		if (line==5)
		{
			pt1[0]=point[0];
			pt1[1]=point[1];
			pt1[2]=point[3];
			k1[0]=k[0];
			k1[1]=k[5];
			k1[2]=k[3];
			b1[0]=b[0];
			b1[1]=b[5];
			b1[2]=b[3];
			pt2[0]=point[1];
			pt2[1]=point[2];
			pt2[2]=point[3];
			k2[0]=k[1];
			k2[1]=k[2];
			k2[2]=k[5];
			b2[0]=b[1];
			b2[1]=b[2];
			b2[2]=b[5];
		} 
		else
		{
			pt1[0]=point[0];
			pt1[1]=point[1];
			pt1[2]=point[2];
			k1[0]=k[0];
			k1[1]=k[1];
			k1[2]=k[4];
			b1[0]=b[0];
			b1[1]=b[1];
			b1[2]=b[4];
			pt2[0]=point[0];
			pt2[1]=point[2];
			pt2[2]=point[3];
			k2[0]=k[4];
			k2[1]=k[2];
			k2[2]=k[3];
			b2[0]=b[4];
			b2[1]=b[2];
			b2[2]=b[3];	
		}
		triangleFill(pt1,k1,b1);
		triangleFill(pt2,k2,b2);
		
		for (i=0;i<height;i++)
		{
			for (j=0;j<width;j++)
			{
				if (region[i][j]==m_color)
				{
					pDC->SetPixel(j,i,RGB(255,0,0));
				} 
				else
				{
					pDC->SetPixel(j,i,RGB(0,0,255));
				}
			}
		}
    }
	//TRACE("\nDDDDhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhh%d",2);
	///m_Fill.SetCheck(1);
}

bool CFillDlg::IsOutside(CPoint *pts,float *k,float *b,int& line)
{
   /*bool outer=true;
   float a1,a2;
   a1=pts[1].y-k[4]*pts[1].x-b[4];
   a2=pts[3].y-k[4]*pts[3].x-b[4];
   if (a1*a2>0)
   {
	   outer=false;
	   line=5;
   } 
   a1=pts[0].y-k[5]*pts[0].x-b[5];
   a2=pts[2].y-k[5]*pts[2].x-b[5];
   if (a1*a2>0)
   {
	   outer=false;
	   line=4;
   } 
   return outer; */
	return true;
}

void CFillDlg::sort(CPoint *points,CPoint *pts,UINT *num)
{
	/*UINT i,j;
	int c,d;
	UINT first,second;
	float k,b;
	float k1,b1;
	float a1,a2;
	num[0]=0;

	for (i=1;i<=3;i++)
	{
		switch(i)
		{
		case 1:
		    first=2;
			second=3;
			break;	
		case 2:
			first=1;
			second=3;
			break;
		case 3:
			first=1;
			second=2;
			break;
		}
		k=float(points[i].y-points[0].y)/float(points[i].x-points[0].x+0.0000001);
		b=points[0].y-k*points[0].x;
		a1=points[first].y-k*points[first].x-b;
		a2=points[second].y-k*points[second].x-b;

⌨️ 快捷键说明

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