Vitalianskiy
@Vitalianskiy
Student

Порядковый алгоритм заполнения с затравкой для многоугольных областей?

Порядковый алгоритм заполнения с затравкой для многоугольных областей с дыркой на python.
Как реализовать на python подскажите.

Line_Fill(x,y);
начало
Найти крайнюю левую точку xl интервала;
Найти крайнюю правую точку xr интервала;
Заполнить интервал от xl до xr;
Для всех х от xl до xr выполнить
если точка (х, у +1) не принадлежит пределу и не окрашена,
то Line_Fill(x, y+1);
Для всех х от xl до xr выполнить
если точка (х, у – 1) не принадлежит пределу и не окрашена,
то Line_Fill (x, y – 1)
конец.
  • Вопрос задан
  • 223 просмотра
Пригласить эксперта
Ответы на вопрос 1
Vitalianskiy
@Vitalianskiy Автор вопроса
Student
#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();

}
Ответ написан
Комментировать
Ваш ответ на вопрос

Войдите, чтобы написать ответ

Войти через центр авторизации
Похожие вопросы