[MSPAINT] drawing.cpp: Refactor Erase, Replace and Airbrush

CORE-18867
This commit is contained in:
Katayama Hirofumi MZ 2023-06-14 16:44:54 +09:00
parent 6d9b7f77ad
commit 78c8df8393

View file

@ -116,46 +116,41 @@ Fill(HDC hdc, LONG x, LONG y, COLORREF color)
void void
Erase(HDC hdc, LONG x1, LONG y1, LONG x2, LONG y2, COLORREF color, LONG radius) Erase(HDC hdc, LONG x1, LONG y1, LONG x2, LONG y2, COLORREF color, LONG radius)
{ {
LONG a, b; LONG cx = (x1 + x2) / 2, cy = (y1 + y2) / 2;
HPEN oldPen; RECT rc = { cx - radius, cy - radius, cx + radius, cy + radius };
HBRUSH oldBrush = (HBRUSH) SelectObject(hdc, CreateSolidBrush(color));
b = max(1, max(abs(x2 - x1), abs(y2 - y1))); HBRUSH hbr = ::CreateSolidBrush(color);
oldPen = (HPEN) SelectObject(hdc, CreatePen(PS_SOLID, 1, color)); ::FillRect(hdc, &rc, hbr);
for(a = 0; a <= b; a++) ::DeleteObject(hbr);
Rectangle(hdc,
(x1 * (b - a) + x2 * a) / b - radius,
(y1 * (b - a) + y2 * a) / b - radius,
(x1 * (b - a) + x2 * a) / b + radius,
(y1 * (b - a) + y2 * a) / b + radius);
DeleteObject(SelectObject(hdc, oldBrush));
DeleteObject(SelectObject(hdc, oldPen));
} }
void void
Replace(HDC hdc, LONG x1, LONG y1, LONG x2, LONG y2, COLORREF fg, COLORREF bg, LONG radius) Replace(HDC hdc, LONG x1, LONG y1, LONG x2, LONG y2, COLORREF fg, COLORREF bg, LONG radius)
{ {
LONG a, b, x, y; LONG cx = (x1 + x2) / 2, cy = (y1 + y2) / 2;
b = max(1, max(abs(x2 - x1), abs(y2 - y1))); RECT rc = { cx - radius, cy - radius, cx + radius, cy + radius };
for(a = 0; a <= b; a++) for (LONG y = rc.top; y < rc.bottom; ++y)
for(y = (y1 * (b - a) + y2 * a) / b - radius + 1; {
y < (y1 * (b - a) + y2 * a) / b + radius + 1; y++) for (LONG x = rc.left; x < rc.right; ++x)
for(x = (x1 * (b - a) + x2 * a) / b - radius + 1; {
x < (x1 * (b - a) + x2 * a) / b + radius + 1; x++) if (::GetPixel(hdc, x, y) == fg)
if (GetPixel(hdc, x, y) == fg) ::SetPixelV(hdc, x, y, bg);
SetPixel(hdc, x, y, bg); }
}
} }
void void
Airbrush(HDC hdc, LONG x, LONG y, COLORREF color, LONG r) Airbrush(HDC hdc, LONG x, LONG y, COLORREF color, LONG r)
{ {
LONG a, b; for (LONG dy = -r; dy <= r; dy++)
{
for(b = -r; b <= r; b++) for (LONG dx = -r; dx <= r; dx++)
for(a = -r; a <= r; a++) {
if ((a * a + b * b <= r * r) && (rand() % 4 == 0)) if ((dx * dx + dy * dy <= r * r) && (rand() % 4 == 0))
SetPixel(hdc, x + a, y + b, color); ::SetPixelV(hdc, x + dx, y + dy, color);
}
}
} }
void void