#include<iostream>
#include<windows.h>
using namespace std;
struct dot
{
int x, y;
};
struct color
{
int r, g, b;
};
void drawLine(int x1, int y1, int x2, int y2, HDC& hdc)
{
MoveToEx(hdc, x1, y1, NULL);
LineTo(hdc, x2 + 1, y2);
}
void drawPolygon(dot* dots, int n, HDC& hdc)
{
int i;
for (i = 0; i < n - 1; i++)
{
drawLine(dots[i].x, dots[i].y, dots[i + 1].x, dots[i + 1].y, hdc);
}
drawLine(dots[i].x, dots[i].y, dots[0].x, dots[0].y, hdc);
}
bool equalColor(int x1, int y1, HDC& hdc, color test)
{
COLORREF col = GetPixel(hdc, x1, y1);
if ((int)GetRValue(col) != test.r)
{
return false;
}
if ((int)GetGValue(col) != test.g)
{
return false;
}
if ((int)GetBValue(col) != test.b)
{
return false;
}
return true;
}
void lineFill(int x, int y, HDC& hdc, color bound, color newColor)
{
int x0 = x, xl, xr;
while (!equalColor(x0, y, hdc, bound))
{
x0--;
}
xl = ++x0;
x0 = x;
while (!equalColor(x0, y, hdc, bound))
{
x0++;
}
xr = --x0;
drawLine(xl, y, xr, y, hdc);
Sleep(50);
x0 = xl;
while (x0 < xr)
{
if (equalColor(x0, y + 1, hdc, bound) == false && equalColor(x0, y + 1, hdc, newColor) == false)
{
lineFill(x0, y + 1, hdc, bound, newColor);
}
x0++;
}
x0 = xl;
while (x0 < xr)
{
if (equalColor(x0, y - 1, hdc, bound) == false && equalColor(x0, y - 1, hdc, newColor) == false)
{
lineFill(x0, y - 1, hdc, bound, newColor);
}
x0++;
}
}
int main()
{
HWND hwnd = GetConsoleWindow();
HDC hdc = GetDC(hwnd);
HBRUSH brush;
HPEN pen;
dot* dots;
color cred = { 255, 0, 0 }, cgreen = { 0, 255, 0 }, cblue = { 0, 0, 255 };
pen = CreatePen(PS_SOLID, 1, RGB(cgreen.r, cgreen.g, cgreen.b));
brush = CreateSolidBrush(RGB(cgreen.r, cgreen.g, cgreen.b));
dots = new dot[5];
dots[0] = { 150, 270 };
dots[1] = { 350, 330 };
dots[2] = { 460, 180 };
dots[3] = { 170, 60 };
dots[4] = { 130, 190 };
SelectObject(hdc, brush);
Ellipse(hdc, 190, 200, 250, 270);
SelectObject(hdc, pen);
drawPolygon(dots, 5, hdc);
pen = CreatePen(PS_SOLID, 1, RGB(cred.r, cred.g, cred.b));
SelectObject(hdc, pen);
lineFill(270, 210, hdc, cgreen, cred);
ReleaseDC(hwnd, hdc);
cin.ignore();
}