24/5/2011, 13:26
Code đồ họa Trẻn Visua basic, anh em nào hiểu giải thích hộ cái, cần thiết thì viết 1 đoạn trên visua studio c# cho anh em tham khảo
- Code:
#define MAXSIZE 100
typedef struct tagSTACK {
int top;
POINT a[MAXSIZE];
} STACK;
bool IsFull(STACK stack)
{
if (stack.top == MAXSIZE)
return true;
else
return false;
}
bool IsEmpty(STACK stack)
{
if (stack.top == 0)
return true;
else
return false;
}
void Push(STACK &stack, POINT p)
{
stack.a[stack.top] = p;
stack.top++;
return;
}
POINT Pop(STACK &stack)
{
stack.top--;
return stack.a[stack.top];
}
int LoangPhai(HDC hdc, int x, int y, COLORREF BC)
{
while (true) {
if (GetPixel(hdc,x+1,y) == BC)
return x;
x++;
}
}
int ToTrai(HDC hdc, int x, int y, COLORREF FC, COLORREF BC)
{
while (true) {
SetPixel(hdc,x,y,FC);
if (GetPixel(hdc,x-1,y)==FC || GetPixel(hdc,x-1,y)==BC)
return x;
x--;
}
}
void TimCacXBenPhaiNhat(HDC hdc, STACK &stack, int x, int y, int xmin, COLORREF FC, COLORREF BC)
{
int m;
m = x;
POINT p;
// Ben phai
while (GetPixel(hdc,x,y) != BC && GetPixel(hdc,x,y) != FC) {
if (GetPixel(hdc,x+1,y) == BC || GetPixel(hdc,x+1,y) == FC) {
p.x = x;
p.y = y;
Push(stack,p);
break;
}
x++;
}
// Ben trai
m = x;
while (m > xmin) {
if (GetPixel(hdc,m,y) == FC || GetPixel(hdc,m,y) == BC)
if (GetPixel(hdc,m-1,y) != FC && GetPixel(hdc,m-1,y) != BC)
if (!IsFull(stack)) {
p.x = m-1;
p.y = y;
Push(stack,p);
}
m--;
}
return;
}
void ToMauKhongDeQui(HWND hWnd)
{
HDC hdc;
hdc = GetDC(hWnd);
COLORREF FC = RGB(255,0,0);
COLORREF BC = RGB(0,0,255);
STACK stack;
stack.top = 0;
POINT p;
int x = 366, y = 210;
int xmax, xmin;
xmax = LoangPhai(hdc,x,y,BC);
if (!IsFull(stack)) {
p.x = xmax;
p.y = y;
Push(stack,p);
}
while (!IsEmpty(stack)) {
p = Pop(stack);
xmin = ToTrai(hdc,p.x,p.y,FC,BC);
// Di len
TimCacXBenPhaiNhat(hdc,stack,p.x,p.y-1,xmin,FC,BC);
// Di xuong
TimCacXBenPhaiNhat(hdc,stack,p.x,p.y+1,xmin,FC,BC);
}
ReleaseDC(hWnd,hdc);
return;
}