Форматы Хранения Графических данных, страница 5

if (IsFilled) FillRgn(hDC, rgn, hbr);

DeleteObject(rgn);

DeleteObject(hbr);

}

int GetPixel(int x, int y)

{

if(PixelSize > 1)

{     x = (x+0.5) * PixelSize;

y = (y+0.5) * PixelSize;

}

return GetPixel(hdcComp, x, y);

}

void UpdateScreen(HDC hDC)

{     for (int i = 0; i < 2; i++)

{     for (int j = 0; j < PointsCnt[i] - 1; j++)

Line(hDC, PolysX[j][i]/PixelSize, PolysY[j][i]/PixelSize, PolysX[j + 1][i]/PixelSize, PolysY[j + 1][i]/PixelSize);

if (IsComplete[i])

Line(hDC, PolysX[0][i]/PixelSize, PolysY[0][i]/PixelSize, PolysX[j][i]/PixelSize, PolysY[j][i]/PixelSize);

}

if(IsComplete[0] && IsComplete[1] && IsIn(xUser, yUser, 0) && !IsIn(xUser, yUser, 1))

{     xmax = xmaxW / PixelSize;

ymax = ymaxW / PixelSize;

flstr(xUser/PixelSize, yUser/PixelSize);

}

if (IsSelection)

{     MoveToEx(hDC, XLeft, YTop, NULL);

LineTo(hDC, XRight, YTop);

LineTo(hDC, XRight, YBottom);

LineTo(hDC, XLeft, YBottom);

LineTo(hDC, XLeft, YTop);

}

}

void Paint(HWND hWnd, HDC hMemDC)

{

PAINTSTRUCT ps;

RECT r;

HDC hdc;

GetClientRect(hWnd, &r);

hdc = BeginPaint(hWnd, &ps);

BitBlt(hdc, 0, 0, r.right - r.left, r.bottom - r.top, hMemDC, 0, 0, SRCCOPY);

EndPaint(hWnd, &ps);

}

void DrawAll(HWND hWnd, HDC hMemDC)

{

EraseAll(hMemDC);

UpdateScreen(hMemDC);

InvalidateRect(hWnd, NULL, NULL);

}

int VectorM(int x0, int y0, int x1, int y1, int x, int y)

{return(x1-x0)*(y-y0)-(x-x0)*(y1-y0);}

bool IsIn(int x, int y, int p)

{     int s = sign(VectorM(PolysX[PointsCnt[p]-1][p], PolysY[PointsCnt[p]-1][p], PolysX[0][p], PolysY[0][p], x, y));

for (int i = 0; i < PointsCnt[p]-1; i++)

{           if(sign(VectorM(PolysX[i][p], PolysY[i][p], PolysX[i+1][p], PolysY[i+1][p], x, y)) != s)

return false;

}

return true;

}

#define pop() x=stx[--deep];y=sty[deep]

#define push(a,b) stx[deep]=a;sty[deep++]=b

int deep=0, stx[1000], sty[1000] ;

#define putpixel(x, y, c) PutPixel(hdcComp, x, y, true)

#define getpixel(x, y)    GetPixel(x, y)

void flstr(int x, int y)

{

int tempx;

int xleft, xright;

int xenter;

int flag, i ;

push(x,y) ;

while(deep>0)

{

pop() ;

if(getpixel(x,y)!=FillColor)

{

putpixel(x,y,FillColor) ;

tempx=x; //сохранение текущей коорд. x

x++;     //перемещение вправо

while(getpixel(x,y)!=FillColor && x<=xmax)

putpixel(x++, y, FillColor);

xright=x-1;

x=tempx; x--; //перемещение влево

while(getpixel(x,y)!=FillColor && x>=xmin)

putpixel(x--, y, FillColor);

xleft=x+1;

x=tempx;

for(i=0;i<2;i++)

{

//при i=0 проверяем нижнюю,

//а при i=1 - верхнюю строку

if(y<ymax && y>ymin)

{

x=xleft; y+=1-i*2;

while(x<=xright)

{

flag=0;

while(getpixel(x,y)!=FillColor && x<xright)

{

if(flag==0) flag=1;

x++;

}

if (flag==1)

{

if(x==xright &&

getpixel(x,y)!=FillColor)

{

push(x,y);

}

else

{push(x-1,y);}

flag=0;

}

xenter=x;

while(getpixel(x,y)==FillColor && x<xright)

x++;

if(x==xenter) x++;

}

}

y-- ;

}

}

}

}

void Fill(int x, int y)

{

xUser = x;

yUser = y;

}

void ParseMenu(HINSTANCE hInst, HWND hWnd, int wmId)

{

HMENU hmnu = GetMenu(hWnd);

switch (wmId)

{

case IDM_FILESAVE:

SaveImage(hWnd);

return;

case IDM_FILELOAD:

OpenImage(hWnd);

return;

case IDM_RESET:

CurPolyInd = 0;

CurPoly = 0;

PointsCnt[0] = 0;

PointsCnt[1] = 0;

IsComplete[0] = false;

IsComplete[1] = false;

xUser = yUser = 0x7fffffff;

break;

case IDM_EXIT:

DestroyWindow(hWnd);

return;

}

DrawAll(hWnd, hdcComp);

}

Результат работы программы: