Download Source Code

CWinPolygon Class Reference

Encapsulates a polygon. More...

#include <WinPolygon.h>

Collaboration diagram for CWinPolygon:

Collaboration graph
[legend]
List of all members.

Public Types

typedef CWinPolygon::tagRANGE RANGE
 2D range
typedef CWinPolygon::tagRANGELPRANGE
 2D range

Public Member Functions

long Distance (CWinPolygon *pWp)
 Calculates the minimum distance between polygon points.
BOOL GetIBeam (LPPOINT ptTop, LPPOINT ptBottom, BOOL bInvY=FALSE)
 Returns points representing the vertical height of the polygon.
BOOL Scale (LPRECT pSrc, LPRECT pDst)
 Scales the polygon points according to the ratio of specified rectangles.
BOOL Intersect (LPPOINT pPts, DWORD dwPts)
 Collision tests with another polygon.
BOOL Intersect (CWinPolygon *pWp)
 Collision tests with another polygon.
void rScanSort (LPPOINT pPts, DWORD left, DWORD right)
 Performs a recursive quick sort on the polygons.
BOOL ScanSort ()
 Performs a recursive quick sort on the polygons.
BOOL Inflate (long x, long y)
 Inflates the polygon by the specified amount.
float SimpleArea ()
 Calculates a rough estimate of the polygon area.
BOOL ConvexHull ()
 Calculates a convex hull of the polygon.
BOOL ApxConvexHull ()
 Calculates an approximate convex hull.
BOOL FromMap (LPDWORD map, long x, long y, long w, long h, UINT th, LPBYTE buf=NULL)
 Creates a polygon from a map.
 CWinPolygon ()
 Default constructor.
 CWinPolygon (LPPOINT pPts, DWORD dwPts)
 Constructs a polygon object from a list of points.
 CWinPolygon (CWinPolygon &wp)
 Constructs a polygon object by copying another CWinPolygon object.
virtual ~CWinPolygon ()
 Destructor.
void Destroy ()
 Releases the polygon resources.
BOOL Allocate (DWORD sz)
 Allocates the specified number of points in the polygon.
LPPOINT GetPoints ()
 Returns a pointer to the polygon POINT structure array.
DWORD GetNumPoints ()
 Returns the total number of points in the polygon.
DWORD GetNumEdges ()
 Returns the number of edges the polygon has.
LPLINE GetEdge (DWORD i)
 Returns a line struture pointer for the specified edge.
BOOL PtInPolygon (LPPOINT ppt)
 Tests to see if the point lies within the polygon.
BOOL CombinePoint (LPPOINT ppt)
 Adds a point into the current polygon by calculating the closest edge.
CWinPolygonoperator+= (LPPOINT ppt)
 Appends point to the end of the polygon.
BOOL AddPoint (LPPOINT ppt)
 Appends point to the end of the polygon.
BOOL AddPoints (LPPOINT ppt, DWORD dwPts)
 Appends points to the end of the polygon point list.
BOOL AddPoint (long x, long y)
 Appends point to the end of the polygon.
BOOL AddPolygon (CWinPolygon *pWp)
 Add The points from the specified polygon.
BOOL RemovePoint (DWORD dwPt)
 Removes the specified point from the polygon.
BOOL RemovePoint (long x, long y)
 Removes the specified point from the polygon.
 operator DWORD ()
 Returns the number of points in the polygon.
 operator LPPOINT ()
 Returns a pointer to the polygon POINT structure array.
POINT & operator[] (DWORD i)
 Returns a reference to the specified point.
long isLeft (POINT P0, POINT P1, POINT P2)
 Returns greater than zero if the specified triangle is left-handed.
long ComparePoint (POINT p1, POINT p2)
 For sorting points.
POINT CalculateCenter ()
 Calculates the center point of the polygon.
CWinPolygonoperator= (CWinPolygon &wp)
 Copies another CWinPolygon object.
BOOL SetPoints (LPPOINT pPts, DWORD dwPts)
 Sets the value of the polygon points.
BOOL SetPoints (CWinPolygon *pWp)
 Sets the value of the polygon points.
void Offset (LPPOINT ppt)
 Offsets the polygon by the specified distance.
void Offset (long x, long y)
 Offsets the polygon by the specified distance.
float ConvexArea ()
 Calculates the area of a convex polygon.

Static Public Member Functions

static BOOL PtInPolygon (LPPOINT pptList, DWORD dwListSize, LPPOINT ppt)
 Tests to see if the point lies within the polygon.
static BOOL CombinePoint (LPPOINT pptList, DWORD dwListSize, LPPOINT ppt)
 Adds a point into the current polygon by calculating the closest edge.
static float TriangleArea (float a, float b, float c)
 Calculates the area of a triangle given the lengths of it's sides.
static float TriangleArea (float x1, float y1, float x2, float y2, float x3, float y3)
 Calculates the area of the triangle defined by three points.

Classes

struct  tagRANGE
 2D range More...

Detailed Description

Encapsulates a polygon.

This structure encapsulates a polylgon and provides a few nifty functions for manipulating them. The polygon is stored simply as a list of points.


Constructor & Destructor Documentation

CWinPolygon::CWinPolygon LPPOINT  pPts,
DWORD  dwPts
[inline]
 

Constructs a polygon object from a list of points.

Parameters:
[in] pPts - Pointer to POINT structure array
[in] dwPts - Number of points in pPts

CWinPolygon::CWinPolygon CWinPolygon wp  )  [inline]
 

Constructs a polygon object by copying another CWinPolygon object.

Parameters:
[in] wp - Pointer to CWinPolygon object


Member Function Documentation

BOOL CWinPolygon::AddPoint long  x,
long  y
[inline]
 

Appends point to the end of the polygon.

Parameters:
[in] x - Horizontal offset of point
[in] y - Vertical offset of point
Returns:
Non-zero if success
See also:

BOOL CWinPolygon::AddPoint LPPOINT  ppt  )  [inline]
 

Appends point to the end of the polygon.

Parameters:
[in] ppt - Point to add
Returns:
Non-zero if added
See also:

BOOL CWinPolygon::AddPoints LPPOINT  ppt,
DWORD  dwPts
[inline]
 

Appends points to the end of the polygon point list.

Parameters:
[in] ppt - List of points
[in] dwPts - Number of points in ppt
Returns:
Non-zero if success
See also:

BOOL CWinPolygon::AddPolygon CWinPolygon pWp  )  [inline]
 

Add The points from the specified polygon.

Parameters:
[in] pWp - Polygon containing points to add
Returns:
Non-zero if success
See also:

BOOL CWinPolygon::Allocate DWORD  sz  )  [inline]
 

Allocates the specified number of points in the polygon.

Parameters:
[in] sz - Number of points to allocate
Returns:
Non-zero if success
See also:

BOOL CWinPolygon::ApxConvexHull  ) 
 

Calculates an approximate convex hull.

After this call, this class will contain the approximate convex hull of the original points.

Similar to the BFP Approximate Hull Algorithm ( Bentley-Faust-Preparata, 1982 ) but slightly faster

Warning:
This does not calculate an exact convex hull. For a precise convex hull, call ConvexHull()
Returns:
Non-zero if success
See also:

POINT CWinPolygon::CalculateCenter  ) 
 

Calculates the center point of the polygon.

Returns:
Center point of polygon
See also:

BOOL CWinPolygon::CombinePoint LPPOINT  pptList,
DWORD  dwListSize,
LPPOINT  ppt
[static]
 

Adds a point into the current polygon by calculating the closest edge.

Parameters:
[in] pptList - Pointer to POINT structure array
[in] dwListSize - Number of points in pptList
[in] ppt - Point to add
Magic function

Warning:
I haven't implemented this function yet!
The idea is just to calculate the closest edge to the polygon and conjoin the point into the polygon at that edge.

Returns:
Non-zero if success
See also:

BOOL CWinPolygon::CombinePoint LPPOINT  ppt  )  [inline]
 

Adds a point into the current polygon by calculating the closest edge.

Parameters:
[in] ppt - Point to add
Magic function

Warning:
I haven't implemented this function yet!
The idea is just to calculate the closest edge to the polygon and conjoin the point into the polygon at that edge.

Returns:
Non-zero if success
See also:

long CWinPolygon::ComparePoint POINT  p1,
POINT  p2
[inline]
 

For sorting points.

Parameters:
[in] p1 - First point
[in] p2 - Second point
-1 if p1 is before p2 0 if points are the same 1 if p2 is before p1

Returns:
See also:

float CWinPolygon::ConvexArea  ) 
 

Calculates the area of a convex polygon.

The polygon in the object must be convex or this function returns an undefined value!

Returns:
Area
See also:

BOOL CWinPolygon::ConvexHull  ) 
 

Calculates a convex hull of the polygon.

After calling this function, the polygon will contain a convex hull of the original polygon. This calculates an exact convex hull.

Warning:
Requires sorting, call ScanSort() before this function unless you know the points are sorted.
Copyright notice for this function:

Based on "Andrew's Monotone Chain algorithm" Copyright 2001, softSurfer (www.softsurfer.com) This code may be freely used and modified for any purpose providing that this copyright notice is included with it. SoftSurfer makes no warranty for this code, and cannot be held liable for any real or imagined damage resulting from its use. Users of this code must verify correctness for their application.

Returns:
Non-zero if success
See also:

long CWinPolygon::Distance CWinPolygon pWp  ) 
 

Calculates the minimum distance between polygon points.

Parameters:
[in] pWp - CWinPolygon object
This function calculates the minimum distance between to points in the polygons.

Warning:
This function currently makes only a rough estimation of distance.
Returns:
The distance between the closest points.
See also:

BOOL CWinPolygon::FromMap LPDWORD  map,
long  x,
long  y,
long  w,
long  h,
UINT  th,
LPBYTE  buf = NULL
 

Creates a polygon from a map.

Parameters:
[in] map - pointer to map values
[in] x - Horizontal offset for the extraction
[in] y - Vertical offset for the extraction
[in] w - Width of the image area for the extraction
[in] h - Height of the image area for the extraction
[in] th - Value threshold
[in] buf - Pointer to buffer that holds map walk data. You can speed up multiple calls to this function by allocating this ahead of time, then passing it into this function.
This function can create a polygon by 'filling' around a bitmap image. Great for selcting parts of an image.

Returns:
Non-zero if success
See also:

LPLINE CWinPolygon::GetEdge DWORD  i  )  [inline]
 

Returns a line struture pointer for the specified edge.

Parameters:
[in] i - Edge index
This function returns an offset into the point array of the specified edge. It does not allocate any new memory

Returns:
Pointer to the specified edge
See also:

BOOL CWinPolygon::GetIBeam LPPOINT  ptTop,
LPPOINT  ptBottom,
BOOL  bInvY = FALSE
 

Returns points representing the vertical height of the polygon.

Parameters:
[out] ptTop - The top point of the line
[out] ptBottom - Bottom point of the line
[in] bInvY - Non-zero to flip the line upside down.
This function basically creates a line that cuts the polygon in half vertcally. The line is as long as the polygon is high, and is located roughly at the horizontal center.

I know this is a strange function, but I needed it OK?

Returns:
Non-zero if success
See also:

BOOL CWinPolygon::Inflate long  x,
long  y
 

Inflates the polygon by the specified amount.

Parameters:
[in] x - Horizontal inflate value
[in] y - Vertical inflate value
Returns:
Non-zero if success
See also:

BOOL CWinPolygon::Intersect CWinPolygon pWp  )  [inline]
 

Collision tests with another polygon.

Parameters:
[in] pWp - CWinPolygon object to collision test
Returns:
Non-zero if polygons intersect
See also:

BOOL CWinPolygon::Intersect LPPOINT  pPts,
DWORD  dwPts
 

Collision tests with another polygon.

Parameters:
[in] pPts - Points in polygon for collision test
[in] dwPts - Number of points in pPts
Unfortunately, this is not a very optimized function. But it works well.

Returns:
Non-zero if polygons intersect
See also:

long CWinPolygon::isLeft POINT  P0,
POINT  P1,
POINT  P2
[inline]
 

Returns greater than zero if the specified triangle is left-handed.

Parameters:
[in] P0 - First point in triangle
[in] P1 - Second point in triangle
[in] P2 - Third point in triangle
Returns:
greater than zero if left handed triangle is specified
See also:

void CWinPolygon::Offset long  x,
long  y
[inline]
 

Offsets the polygon by the specified distance.

Parameters:
[in] x - Horizontal offset distance
[in] y - Vertical offset distance

void CWinPolygon::Offset LPPOINT  ppt  )  [inline]
 

Offsets the polygon by the specified distance.

Parameters:
[in] ppt - Point specifying offset distance

CWinPolygon& CWinPolygon::operator+= LPPOINT  ppt  )  [inline]
 

Appends point to the end of the polygon.

Parameters:
[in] ppt - Point to add
Returns:
Reference to this object
See also:

BOOL CWinPolygon::PtInPolygon LPPOINT  pptList,
DWORD  dwListSize,
LPPOINT  ppt
[static]
 

Tests to see if the point lies within the polygon.

Parameters:
[in] pptList - Polygon point list
[in] dwListSize - Number of points in pptList
[in] ppt - Point to test
This function works well and is reasonably fast.

Returns:
Non-zero if point is within the polygon
See also:

BOOL CWinPolygon::PtInPolygon LPPOINT  ppt  )  [inline]
 

Tests to see if the point lies within the polygon.

Parameters:
[in] ppt - Point to test
This function works well and is reasonably fast.

Returns:
Non-zero if point is within the polygon
See also:

BOOL CWinPolygon::RemovePoint long  x,
long  y
[inline]
 

Removes the specified point from the polygon.

Parameters:
[in] x - Horizontal offset of point
[in] y - Vertical offset of point
Searches the polygon for a point matching the specified x, y position and removes the first occurence.

Returns:
Non-zero if success
See also:

BOOL CWinPolygon::RemovePoint DWORD  dwPt  )  [inline]
 

Removes the specified point from the polygon.

Parameters:
[in] dwPt - Index of point to remove
Returns:
Non-zero if success
See also:

void CWinPolygon::rScanSort LPPOINT  pPts,
DWORD  left,
DWORD  right
 

Performs a recursive quick sort on the polygons.

Parameters:
[in] pPts - Points to sort
[in] left - Left index
[in] right - Right index
Call ScanSort(). Don't call this function directly

BOOL CWinPolygon::Scale LPRECT  pSrc,
LPRECT  pDst
 

Scales the polygon points according to the ratio of specified rectangles.

Parameters:
[in] pSrc - Source rect
[in] pDst - Destination rect
Calculates the vertical and horizontal difference and scales the polygon accordingly. Great for scaling a polygon for overlaying on one image to the same image of a different size.

Returns:
Non-zero if success
See also:

BOOL CWinPolygon::ScanSort  ) 
 

Performs a recursive quick sort on the polygons.

Reasonably fast sorting algorithm.

Returns:
Non-zero if success
See also:

BOOL CWinPolygon::SetPoints CWinPolygon pWp  )  [inline]
 

Sets the value of the polygon points.

Parameters:
[in] pWp - Polygon containing point values
Returns:
Non-zero if success
See also:

BOOL CWinPolygon::SetPoints LPPOINT  pPts,
DWORD  dwPts
[inline]
 

Sets the value of the polygon points.

Parameters:
[in] pPts - Pointer to POINT array
[in] dwPts - Number of points in pPts
Returns:
Non-zero if success
See also:

float CWinPolygon::SimpleArea  ) 
 

Calculates a rough estimate of the polygon area.

Returns:
value representing the rough polygon area.
See also:

static float CWinPolygon::TriangleArea float  x1,
float  y1,
float  x2,
float  y2,
float  x3,
float  y3
[inline, static]
 

Calculates the area of the triangle defined by three points.

Parameters:
[in] x1 - Horizontal offset first point
[in] y1 - Vertical offset first point
[in] x2 - Horizontal offset second point
[in] y2 - Vertical offset second point
[in] x3 - Horizontal offset third point
[in] y3 - Vertical offset third point
Returns:
Area of the specified triangle
See also:

static float CWinPolygon::TriangleArea float  a,
float  b,
float  c
[inline, static]
 

Calculates the area of a triangle given the lengths of it's sides.

Parameters:
[in] a - First side length
[in] b - Second side length
[in] c - Third side length
Returns:
Area of the triangle
See also:


The documentation for this class was generated from the following files:
  • inc/WinPolygon.h
  • WinPolygon.cpp
Copyright Robert Umbehant
This documentation is covered by the LGPL