Click here to Skip to main content
15,888,461 members
Articles / Desktop Programming / MFC

A Matrix-Based 2-D Polygon Clipping Class

Rate me:
Please Sign up or sign in to vote.
3.50/5 (40 votes)
28 Mar 2003CDDL8 min read 200K   4.7K   58  
An article on 2-D Polygon Clipping
// PolygonClip.h: interface for the CPolygonClip class.
//
//////////////////////////////////////////////////////////////////////

#if !defined(AFX_POLYGONCLIP_H__3410F6D8_11EC_11D7_8CED_00C0F0172654__INCLUDED_)
#define AFX_POLYGONCLIP_H__3410F6D8_11EC_11D7_8CED_00C0F0172654__INCLUDED_

#if _MSC_VER > 1000
#pragma once
#endif // _MSC_VER > 1000

#define MAX_NUM_VERTICIES 100	// Maximum number of verticies allowed
#define MaxDetChanges 50        // Max number of determinant changes per line
#define DotSize 3				// Size of the intersection dot
#define PenSize 1				// Size of the pen
		
class CPolygonClip : public CObject  
{
public:
	void Draw(CDC* pDC);
	unsigned int FindArrayMax(unsigned int* pNumChanges);
	bool CalculateIntersections(double* LineX1, double* LineX2, 
								double* LineY1, double* LineY2, 
								double* PolyVertexX, double* PolyVertexY, 
								unsigned int nMaxNumSignChanges);
	unsigned int* CalcSiDeterm(double* LineX1, double* LineY1, 
						   	   double* LineX2, double* LineY2, 
							   double* PolyVertexX, double* PolyVertexY);
	CPolygonClip(unsigned int nNumVerticies, unsigned int nNumLines);
	virtual ~CPolygonClip();

protected:
	double** m_ppdTempIntrY;						// Temp arrays for sorting
	double** m_ppdTempIntrX;
	bool** m_ppbSignChangeTable;					// The sign change table
	unsigned int* m_pnNumSignChanges;				// Number of determinant sign changes per line
	unsigned int* m_pnIntersectsPerLine;			// The number of intersections for a given line
	unsigned int* m_pnUniquesPerLine;				// Holds the number of unique intersections per line
	int** m_ppnDetSignTable;						// Determinant sign table
	double** m_ppdIntersectionY;					// Polygon and line intersection point y coordinates
	double** m_ppdIntersectionX;					// Polygon and line intersection point x coordinates
	
	CPolygonClip(){};								// Default constructor - do not use

private:
	bool CheckValidPoint(CPoint* lpVertexPoints, int nCount, CPoint aPoint);
	bool m_bIntersectionFlag;
	bool m_bDetermCalcFlag;
	double TurboDeterm(double Elem11, double Elem12, double Elem13,
					   double Elem21, double Elem22, double Elem23);
	unsigned int m_nNumVerticies;			// Number of polygon verticies
	unsigned int m_nNumLines;				// Number of lines

	// Temp arrays to hold spanning verticies 
	double dTempVertex[MAX_NUM_VERTICIES][MAX_NUM_VERTICIES];
	double dTempVertexPrior[MAX_NUM_VERTICIES][MAX_NUM_VERTICIES];
	double dVertex[MAX_NUM_VERTICIES][MAX_NUM_VERTICIES];
	double dVertexPrior[MAX_NUM_VERTICIES][MAX_NUM_VERTICIES];
};

#endif // !defined(AFX_POLYGONCLIP_H__3410F6D8_11EC_11D7_8CED_00C0F0172654__INCLUDED_)

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Common Development and Distribution License (CDDL)


Written By
Other
Anonymous Proxy Anonymous Proxy
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions