Old fashion design for PC-104
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 

849 lines
20 KiB

//----------------------------------------------------------------------------
// Laser Guard Ltd.
// Copyright © 2000-2002. All Rights Reserved.
// Version 1.0 11/05/2000
// Last modification: 08/10/2003
// Project Intruder Position Indicator
// SUBSYSTEM: Polygon
// FILE: Polygon.cpp
// AUTHOR: Chizhevskiy Sergey
// OVERVIEW
// ~~~~~~~~
// Source file of class CPolygon
//----------------------------------------------------------------------------
#include "stdafx.h"
#include "..\LgLib\Version.h"
#include "..\LgLib\Comm\LGSm.h"
#include "Polygon.h"
struct VERTEX
{
double x, y, r;
BOOL f;
int n;
};
typedef VERTEX* LPVERTEX;
struct SEGMENT
{
INT pPoints[2];
INT nIndex;
};
typedef SEGMENT* LPSEGMENT;
struct SEGMENTS
{
LPVERTEX pPoints;
INT nCount;
};
typedef SEGMENTS* LPSEGMENTS;
CPolygon::CPolygon(INT type)
{
m_hRgn = NULL;
m_pPoints = NULL;
m_nCount = 0;
m_pPointsDraw = NULL;
m_Type = type;
m_fVisible = TRUE;
LGLOG( LOG_LEVEL_MEDIUM, LOG_TYPE_INFO, this, "CPolygon::CPolygon(INT)" );
}
CPolygon::CPolygon(HANDLE hFile)
{
m_hRgn = NULL;
m_pPoints = NULL;
m_nCount = 0;
m_Type = 0;
m_fVisible = TRUE;
Load(hFile);
LGLOG( LOG_LEVEL_MEDIUM, LOG_TYPE_INFO, this, "CPolygon::CPolygon(HANDLE)" );
}
CPolygon::~CPolygon()
{
Empty();
LGLOG( LOG_LEVEL_MEDIUM, LOG_TYPE_INFO, this, "CPolygon::~CPolygon" );
}
void CPolygon::Empty()
{
if(m_pPoints)
{
delete[] m_pPoints;
delete[] m_pPointsDraw;
}
if(m_hRgn)
::DeleteObject(m_hRgn);
m_hRgn = NULL;
m_pPoints = NULL;
m_nCount = 0;
}
BOOL CPolygon::Save(HANDLE hFile)
{
DWORD dwWritten = 0UL;
::WriteFile(hFile, &m_Type, sizeof(m_Type), &dwWritten, NULL);
::WriteFile(hFile, &m_nCount, sizeof(m_nCount), &dwWritten, NULL);
::WriteFile(hFile, m_pPoints, m_nCount * sizeof(POINT), &dwWritten, NULL);
return 0;
}
BOOL CPolygon::Load(HANDLE hFile)
{
DWORD dwReaded = 0UL;
Empty();
::ReadFile(hFile, &m_Type, sizeof(m_Type), &dwReaded, NULL);
::ReadFile(hFile, &m_nCount, sizeof(m_nCount), &dwReaded, NULL);
m_pPoints = new POINT[m_nCount];
m_pPointsDraw = new POINT[m_nCount];
::ReadFile(hFile, m_pPoints, m_nCount * sizeof(POINT), &dwReaded, NULL);
m_hRgn = ::CreatePolygonRgn(m_pPoints, m_nCount, WINDING);
return 0;
}
BOOL CPolygon::AddPoint(LPPOINT pPoint, const double& diff)
{
BOOL bEndPoint = FALSE;
if(!m_hRgn)
{
if(m_nCount)
{
if((m_pPoints->x + diff >= pPoint->x) && (m_pPoints->x - diff <= pPoint->x) &&
(m_pPoints->y + diff >= pPoint->y) && (m_pPoints->y - diff <= pPoint->y))
{
m_hRgn = ::CreatePolygonRgn( m_pPoints, m_nCount, ALTERNATE );
return TRUE;
}
}
LPPOINT pPoints = new POINT[m_nCount + 1];
LPPOINT pPointsDraw = new POINT[m_nCount + 1];
if(m_pPoints)
{
memcpy(pPoints, m_pPoints, sizeof(POINT) * m_nCount);
delete[] m_pPoints;
delete[] m_pPointsDraw;
}
m_pPoints = pPoints;
m_pPointsDraw = pPointsDraw;
m_pPoints[m_nCount++] = *pPoint;
}
return FALSE;
}
int DBL_CMP(const double& a, const double& b, const double quality = 1e-10)
{
if(fabs(a - b) <= fabs(quality))
return 0;
if(a > b)
return 1;
return -1;
}
BOOL CrossProduct( LPVERTEX ln0, LPVERTEX ln1, LPVERTEX pCross, BOOL fCheck0 = TRUE, BOOL fCheck1 = TRUE )
{
double A0, A1, B0, B1, X, Y;
if(DBL_CMP(ln0[0].x, ln0[1].x) == 0)
{
A0 = DBL_MAX;
B0 = ln0[0].x;
}
else
{
A0 = (ln0[0].y - ln0[1].y) / (ln0[0].x - ln0[1].x);
B0 = ln0[0].y - A0 * ln0[0].x;
}
if(DBL_CMP(ln1[0].x , ln1[1].x) == 0)
{
A1 = DBL_MAX;
B1 = ln1[0].x;
}
else
{
A1 = (ln1[0].y - ln1[1].y) / (ln1[0].x - ln1[1].x);
B1 = ln1[0].y - A1 * ln1[0].x;
}
if(A0 == DBL_MAX)
{
if(A1 == DBL_MAX)
return FALSE;
X = B0;
Y = A1 * X + B1;
}
else
{
if(A1 == DBL_MAX)
X = B1;
else
{
if(DBL_CMP( A1, A0 ) != 0)
X = (B0 - B1) / (A1 - A0);
else
return FALSE;
}
Y = A0 * X + B0;
}
double y = Y, x = X;
if(fCheck0)
{
double x0 = ln0[0].x, x1 = ln0[1].x;
double y0 = ln0[0].y, y1 = ln0[1].y;
if(DBL_CMP(y0, y1) == -1 && (DBL_CMP(y0, y) == 1 || DBL_CMP(y, y1) == 1))
return FALSE;
if(DBL_CMP(y0, y1) == 1 && (DBL_CMP(y0, y) == -1 || DBL_CMP(y, y1) == -1))
return FALSE;
if(DBL_CMP(x0, x1) == -1 && (DBL_CMP(x0, x) == 1 || DBL_CMP(x, x1) == 1))
return FALSE;
if(DBL_CMP(x0, x1) == 1 && (DBL_CMP(x0, x) == -1 || DBL_CMP(x, x1) == -1))
return FALSE;
}
if(fCheck1)
{
double x0 = ln1[0].x, x1 = ln1[1].x;
double y0 = ln1[0].y, y1 = ln1[1].y;
if(DBL_CMP(y0, y1) == -1 && (DBL_CMP(y0, y) == 1 || DBL_CMP(y, y1) == 1))
return FALSE;
if(DBL_CMP(y0, y1) == 1 && (DBL_CMP(y0, y) == -1 || DBL_CMP(y, y1) == -1))
return FALSE;
if(DBL_CMP(x0, x1) == -1 && (DBL_CMP(x0, x) == 1 || DBL_CMP(x, x1) == 1))
return FALSE;
if(DBL_CMP(x0, x1) == 1 && (DBL_CMP(x0, x) == -1 || DBL_CMP(x, x1) == -1))
return FALSE;
}
pCross->x = X;
pCross->y = Y;
return TRUE;
}
BOOL PtInLine(LPVERTEX ln, VERTEX& p)
{
double A, B;
if(DBL_CMP(ln[0].x, ln[1].x) == 0)
{
A = DBL_MAX;
B = ln[0].x;
}
else
{
A = (ln[0].y - ln[1].y) / (ln[0].x - ln[1].x);
B = ln[0].y - A * ln[0].x;
}
if(A == DBL_MAX)
{
if( DBL_CMP(p.x, B) == 0)
return TRUE;
}
else
{
if(DBL_CMP(p.x * A + B, p.y) == 0)
return TRUE;
}
return FALSE;
}
int comparePoints(const void *arg1, const void *arg2)
{
if(((LPVERTEX)arg1)->r > ((LPVERTEX)arg2)->r)
return +1;
if(((LPVERTEX)arg1)->r < ((LPVERTEX)arg2)->r)
return -1;
return 0;
}
void AddPointToSegment(LPSEGMENTS pSegments, INT nIndex, LPVERTEX pPoint)
{
LPVERTEX pNew = new VERTEX[pSegments[nIndex].nCount + 1];
::CopyMemory(pNew, pSegments[nIndex].pPoints, sizeof(VERTEX) * pSegments[nIndex].nCount);
::CopyMemory(pNew + pSegments[nIndex].nCount, pPoint, sizeof(VERTEX));
if(pSegments[nIndex].pPoints)
delete pSegments[nIndex].pPoints;
pSegments[nIndex].pPoints = pNew;
pSegments[nIndex].nCount++;
}
void StickSegments(LPSEGMENTS pSegments, INT nIndex)
{
if(pSegments[nIndex].nCount)
{
qsort(pSegments[nIndex].pPoints, pSegments[nIndex].nCount, sizeof(VERTEX), comparePoints);
LPVERTEX pTemp = new VERTEX[pSegments[nIndex].nCount];
LPVERTEX pPoints = pSegments[nIndex].pPoints;
int f, nT = 0;
pTemp[nT++] = pPoints[0];
f = pPoints[0].f;
for(int i = 1; i < pSegments[nIndex].nCount;)
{
f += pPoints[i].f;
if(f == 0)
{
pTemp[nT++] = pPoints[i++];
if(i < pSegments[nIndex].nCount)
{
pTemp[nT++] = pPoints[i];
f = pPoints[i++].f;
}
}
else
i++;
}
if(pSegments[nIndex].pPoints)
delete[] pSegments[nIndex].pPoints;
pSegments[nIndex].pPoints = new VERTEX[nT];
::CopyMemory(pSegments[nIndex].pPoints, pTemp, sizeof(VERTEX) * nT);
pSegments[nIndex].nCount = nT;
if(pTemp)
delete[] pTemp;
}
}
int compareSegments(const void *arg1, const void *arg2)
{
if(((LPSEGMENT)arg1)->nIndex > ((LPSEGMENT)arg2)->nIndex)
return +1;
if(((LPSEGMENT)arg1)->nIndex < ((LPSEGMENT)arg2)->nIndex)
return -1;
if(((LPSEGMENT)arg1)->pPoints[0] > ((LPSEGMENT)arg2)->pPoints[0])
return +1;
if(((LPSEGMENT)arg1)->pPoints[0] < ((LPSEGMENT)arg2)->pPoints[0])
return -1;
return 0;
}
void CPolygons::ProduceHiddenZones(HDC hDC, POINT& center, double range,
const double& scale, double dZeroShift )
{
const int MAX_RAY_COUNT = 360;
const double TWO_PI = 6.2831853071796; // 2 * PI, 18/04/2001
VERTEX pLines[MAX_RAY_COUNT];
SEGMENTS pSegments[MAX_RAY_COUNT >> 1];
double double_range = range * 2;
for(int idx = 0; idx < (m_nRayCount >> 1); idx++)
{
double a = TWO_PI * idx / m_nRayCount;
pLines[(idx << 1) + 0 ].x = cos(a) * double_range;
pLines[(idx << 1) + 0 ].y = -sin(a) * double_range;
pLines[(idx << 1) + 1 ].x = -cos(a) * double_range;
pLines[(idx << 1) + 1 ].y = sin(a) * double_range;
pSegments[idx].nCount = 0;
pSegments[idx].pPoints = NULL;
}
for(int i = 0; i < m_nCount; i++)
{
CPolygon *pPolygon = m_pPolygons[i];
if(pPolygon->GetCount() > 2/* && pPolygon->GetType() == 0*/)
{
LPVERTEX pPoints = new VERTEX[pPolygon->GetCount() + 1];
for(int j = 0; j < pPolygon->GetCount(); j++)
{
pPoints[j].x = ((double)pPolygon->GetPoints()[j].x);
pPoints[j].y = ((double)pPolygon->GetPoints()[j].y);
}
pPoints[pPolygon->GetCount()].x = ((double)pPolygon->GetPoints()[0].x);
pPoints[pPolygon->GetCount()].y = ((double)pPolygon->GetPoints()[0].y);
for(j = 0; j <= pPolygon->GetCount(); j++)
for(int v = 0; v < (m_nRayCount >> 1); v++)
if(PtInLine(pLines + (v << 1), pPoints[j] ) )
{
for(int g = -1; g < 2; g += 2)
for(int f = -1; f < 2; f += 2)
{
POINT p = { (INT)(pPoints[j].x) + f,
(INT)(pPoints[j].y) + g };
if(!pPolygon->PtInPolygon(p))
{
pPoints[j].x += (double)f * 1e-5;
pPoints[j].y += (double)g * 1e-5;
goto NEXT;
}
}
NEXT:;
}
for(j = 0; j < (m_nRayCount >> 1); j ++ )
{
INT nCCount = 0;
VERTEX pCPoints[MAX_RAY_COUNT * 10];
for(int m = 0; m < pPolygon->GetCount(); m++)
{
VERTEX cp;
if(CrossProduct(pPoints + m, pLines + (j << 1), &cp))
{
cp.n = m;
cp.r = sqrt(pow(cp.x - pLines[j << 1].x, 2) + pow(cp.y - pLines[j << 1].y, 2));
if(nCCount > 0 &&
DBL_CMP(pCPoints[nCCount-1].x, cp.x) == 0 &&
DBL_CMP(pCPoints[nCCount-1].y, cp.y) == 0)
{
int i1 = (m + 1) % pPolygon->GetCount();
int i2 = (m - 1 + pPolygon->GetCount()) % pPolygon->GetCount();
VERTEX line[] = { { pPoints[i1].x, pPoints[i1].y },
{ pPoints[i2].x, pPoints[i2].y } };
LPVERTEX l0 = pLines + (j << 1);
if(!CrossProduct( pLines + (j << 1), line, &cp, FALSE))
pCPoints[nCCount++] = cp;
}
else
pCPoints[nCCount++] = cp;
}
} // end of for(int m = 0; m < pPolygon->GetCount(); m++)
if(nCCount > 0 &&
DBL_CMP( pCPoints[nCCount-1].x, pCPoints[0].x) == 0 &&
DBL_CMP( pCPoints[nCCount-1].y, pCPoints[0].y) == 0)
{
int i1 = (pCPoints[0].n + 1) % pPolygon->GetCount();
int i2 = (pCPoints[0].n - 1 + pPolygon->GetCount()) % pPolygon->GetCount();
VERTEX line[] = { { pPoints[i1].x, pPoints[i1].y },
{ pPoints[i2].x, pPoints[i2].y } };
LPVERTEX l0 = pLines + (j << 1);
VERTEX cp;
if(CrossProduct( pLines + (j << 1), line, &cp, FALSE))
nCCount--;
}
if(nCCount % 2 == 0)
{
qsort(pCPoints, nCCount, sizeof(VERTEX), comparePoints);
for(m = 0; m < nCCount; m++)
{
pCPoints[m].f = ( m % 2 ) ? -1 : +1;
AddPointToSegment( pSegments, j, pCPoints + m );
}
}
} // end of for(j = 0; j < (m_nRayCount >> 1); j ++ )
delete[] pPoints; // Added delete[] 22/04/2001
} // end of if(pPolygon->GetCount() > 2 && pPolygon->GetType() == 0)
} // end of for(int i = 0; i < m_nCount; i++)
INT sc = 0;
for(INT j = 0; j < (m_nRayCount >> 1); j ++)
{
StickSegments( pSegments, j );
sc += pSegments[j].nCount;
}
LPSEGMENT pSegment = new SEGMENT[sc];
::FillMemory(pSegment, sizeof(SEGMENT)*sc, 0xFF);
INT cn = 0;
dZeroShift *= 3.1415 / 180.0;
for(j = 0; j < (m_nRayCount >> 1); j ++)
{
LPVERTEX pPoints = pSegments[j].pPoints;
if(pSegments[j].nCount % 2)
CMessageBox::MessageBox(NULL, "Produced odd number of points", NULL, MB_OK);
else
{
double k = scale;
HPEN hPen = ::CreatePen( PS_SOLID, 1, 0xFFFFFFFF );
HPEN hOldPen = (HPEN)::SelectObject(hDC, hPen);
// convert zero shift to radians
for(int m = 0; m < pSegments[j].nCount; m += 2)
{
pPoints[m].r -= double_range;//*scale;
pPoints[m+1].r -= double_range;//*scale;
pPoints[m].x *= k;
pPoints[m].y *= k;
// rotate coordinates
double x = pPoints[m].x * cos( dZeroShift ) + pPoints[m].y * sin( dZeroShift );
double y = -pPoints[m].x * sin( dZeroShift ) + pPoints[m].y * cos( dZeroShift );
pPoints[m].x = x;
pPoints[m].y = y;
pPoints[m].x += center.x;
pPoints[m].y += center.y;
::MoveToEx(hDC, (int)pPoints[m].x, (int)pPoints[m].y, NULL);
pPoints[m+1].x *= k;
pPoints[m+1].y *= k;
// rotate coordinates
x = pPoints[m+1].x * cos( dZeroShift ) + pPoints[m+1].y * sin( dZeroShift );
y = -pPoints[m+1].x * sin( dZeroShift ) + pPoints[m+1].y * cos( dZeroShift );
pPoints[m+1].x = x;
pPoints[m+1].y = y;
pPoints[m+1].x += center.x;
pPoints[m+1].y += center.y;
::LineTo(hDC, (int)pPoints[m + 1].x, (int)pPoints[m + 1].y);
if(pPoints[m].r < 0.0 && pPoints[m+1].r > 0.0)
{
pSegment[cn].nIndex = j;
pSegment[cn].pPoints[0] = 0;
pSegment[cn++].pPoints[1] = (INT)(-pPoints[m].r);
pSegment[cn].nIndex = j + (m_nRayCount >> 1);
pSegment[cn].pPoints[0] = 0;
pSegment[cn++].pPoints[1] = (INT)(pPoints[m + 1].r);
}
else
{
if(pPoints[m].r < 0 || pPoints[m+1].r < 0)
{
if(pPoints[m].r > pPoints[m+1].r)
{
pSegment[cn].nIndex = j;
pSegment[cn].pPoints[0] = (INT)(-pPoints[m].r);
pSegment[cn++].pPoints[1] = (INT)(-pPoints[m + 1].r);
}
else
{
pSegment[cn].nIndex = j;
pSegment[cn].pPoints[0] = (INT)(-pPoints[m + 1].r);
pSegment[cn++].pPoints[1] = (INT)(-pPoints[m].r);
}
}
else
if(pPoints[m].r < pPoints[m+1].r)
{
pSegment[cn].nIndex = j + (m_nRayCount >> 1);
pSegment[cn].pPoints[0] = (INT)(pPoints[m].r);
pSegment[cn++].pPoints[1] = (INT)(pPoints[m + 1].r);
}
else
{
pSegment[cn].nIndex = j + (m_nRayCount >> 1);
pSegment[cn].pPoints[0] = (INT)(pPoints[m + 1].r);
pSegment[cn++].pPoints[1] = (INT)(pPoints[m].r);
}
} // end of else
} // end of for(int m = 0; m < pSegments[j].nCount; m += 2)
::SelectObject(hDC, hOldPen);
::DeleteObject(hPen);
} // end of else
delete [] pPoints;
pSegments[j].pPoints = NULL;
} // end of for(j = 0; j < (m_nRayCount >> 1); j ++)
qsort(pSegment, sc, sizeof(SEGMENT), compareSegments);
FILE *file = fopen("Hidden.txt", "wt");
if( file )
{
for(j = 0; j < sc; j++)
if(pSegment[j].nIndex != 0xFFFFFFFF)
{
INT nMin = pSegment[j].pPoints[0] * LGS_MAX_DETECTOR_RANGE / range; //?
INT nMax = pSegment[j].pPoints[1] * LGS_MAX_DETECTOR_RANGE / range; //?
if( nMin <= LGS_MAX_DETECTOR_RANGE )
{
if( nMax > LGS_MAX_DETECTOR_RANGE )
nMax = LGS_MAX_DETECTOR_RANGE;
fprintf(file, "%d %d %d\n", pSegment[j].nIndex, nMin, nMax);
}
}
fclose(file);
}
file = fopen("Polygons.txt", "wt");
if( file )
{
for(j = 0; j < m_nCount; j++)
{
CPolygon *pPolygon = m_pPolygons[j];
fprintf(file, "polygon %d\n", pPolygon->GetType() );
LPPOINT pPoint = pPolygon->GetPoints();
for( int m = 0; m < pPolygon->GetCount(); m++ )
{
POINT sp = pPoint[m];
sp.x = (sp.x + range) * LGS_MAX_DETECTOR_RANGE / range;
sp.y = (sp.y + range) * LGS_MAX_DETECTOR_RANGE / range;
fprintf(file, "%d %d\n", sp.x, sp.y );
}
}
fclose(file);
}
delete[] pSegment;
}
void CPolygon::Draw(HDC hDC, POINT& center, const double& scale, double dZeroShift )
{
if(m_fVisible)
{
double k = scale;
COLORREF colors[] = { 0x00000000, 0x007F0000, 0x00FF0000, // 18/04/2001
0x007F007F, 0x00FF00FF, 0x00004F00 };
int background_mode;
HPEN hPen = ::CreatePen(PS_SOLID, 1, colors[m_Type]);
HPEN hOldPen = (HPEN)::SelectObject(hDC, hPen);
HBRUSH hBrush = ::CreateHatchBrush(HS_DIAGCROSS, colors[m_Type]);
HBRUSH hOldBrush = (HBRUSH)::SelectObject(hDC, hBrush);
background_mode = ::SetBkMode(hDC, TRANSPARENT);
dZeroShift *= 3.1415 / 180;
for(int j = 0; j < GetCount(); j++)
{
double x = m_pPoints[j].x * cos( dZeroShift ) + m_pPoints[j].y * sin( dZeroShift );
double y = -m_pPoints[j].x * sin( dZeroShift ) + m_pPoints[j].y * cos( dZeroShift );
m_pPointsDraw[j].x = (long)(x * k + center.x);
m_pPointsDraw[j].y = (long)(y * k + center.y);
::Ellipse(hDC, m_pPointsDraw[j].x - 2, m_pPointsDraw[j].y - 2,
m_pPointsDraw[j].x + 2, m_pPointsDraw[j].y + 2);
}
::Polygon(hDC, m_pPointsDraw, GetCount());
::SetBkMode(hDC, background_mode);
::SelectObject(hDC, hOldBrush);
::DeleteObject(hBrush);
::SelectObject(hDC, hOldPen);
::DeleteObject(hPen);
} // end of if(m_fVisible)
}
CPolygons::CPolygons()
{
m_pPolygons = NULL;
m_nCount = NULL;
m_nRayCount = 360;
InitializeCriticalSection(&cs);
LGLOG( LOG_LEVEL_MEDIUM, LOG_TYPE_INFO, this, "CPolygons::CPolygons" );
}
CPolygons::~CPolygons()
{
Empty();
DeleteCriticalSection(&cs);
LGLOG( LOG_LEVEL_MEDIUM, LOG_TYPE_INFO, this, "CPolygons::~CPolygons" );
}
BOOL CPolygons::AddPolygon(CPolygon *pPolygon)
{
CPolygon** pPolygons = new CPolygon*[m_nCount + 1];
if(m_pPolygons)
{
memcpy(pPolygons, m_pPolygons, sizeof(CPolygon*) * m_nCount);
delete[] m_pPolygons;
}
EnterCriticalSection(&cs); // 24/05/2001
m_pPolygons = pPolygons;
m_pPolygons[m_nCount++] = pPolygon;
LeaveCriticalSection(&cs);
return TRUE;
}
BOOL CPolygons::PtInPolygons(POINT& point, BOOL *pFlags) const
{
for(int i = 0; i < m_nCount; i++)
if(pFlags[m_pPolygons[i]->GetType()])
if(m_pPolygons[i]->PtInPolygon(point))
return TRUE;
return FALSE;
}
void CPolygons::Empty()
{
if(m_pPolygons)
{
for(int i = 0; i < m_nCount; i++)
delete m_pPolygons[i];
delete[] m_pPolygons;
}
m_pPolygons = NULL;
m_nCount = 0;
}
BOOL CPolygons::Save(LPCTSTR pName)
{
HANDLE hFile = ::CreateFile(pName, GENERIC_WRITE, FILE_SHARE_READ | FILE_SHARE_WRITE,
NULL, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
DWORD dwWritten = 0UL;
::WriteFile(hFile, &m_nCount, sizeof(m_nCount), &dwWritten, NULL);
for(int i = 0; i < m_nCount; i++)
m_pPolygons[i]->Save(hFile);
::CloseHandle(hFile);
return TRUE;
}
BOOL CPolygons::Load(LPCTSTR pName)
{
Empty();
HANDLE hFile = ::CreateFile(pName, GENERIC_READ, FILE_SHARE_READ | FILE_SHARE_WRITE,
NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
if(hFile != INVALID_HANDLE_VALUE)
{
DWORD dwReaded = 0UL;
::ReadFile(hFile, &m_nCount, sizeof(m_nCount), &dwReaded, NULL);
m_pPolygons = new CPolygon*[m_nCount];
for(int i = 0; i < m_nCount; i++)
m_pPolygons[i] = new CPolygon(hFile);
::CloseHandle(hFile);
return TRUE;
}
return FALSE;
}
void CPolygons::Draw(HDC hDC, POINT& center, const double& scale, double dZeroShift )
{
for(int i = 0; i < GetCount(); i++)
m_pPolygons[i]->Draw(hDC, center, scale, dZeroShift );
}
BOOL CPolygons::Delete(POINT& p)
{
BOOL fDeleted = FALSE;
for(int i = 0; i < m_nCount;)
{
if(m_pPolygons[i]->PtInPolygon(p))
{
CPolygon *pPolygon = m_pPolygons[i];
m_pPolygons[i] = NULL;
delete pPolygon;
m_nCount--;
memcpy(m_pPolygons + i, m_pPolygons + i + 1, sizeof(CPolygon*) * (m_nCount - i));
fDeleted = TRUE;
}
else
i++;
}
return fDeleted;
}
void CPolygons::SetVisible(INT nType, BOOL fVisible)
{
for(int i = 0; i < GetCount(); i++)
if(m_pPolygons[i]->GetType() == nType)
m_pPolygons[i]->SetVisible(fVisible);
}