非典型扫描线填充正方形的4-连通边界填充算法

Option   Explicit

Private  cltStackX  As  Collection
Private  lTopX  As   Long
Private  cltStackY  As  Collection
Private  lTopY  As   Long

Dim  X%, Y%
Const  Vnumber  =   7        ' 一共7个点
Const  xMax  =   14       ' 坐标最大值12
Const  grid  =   400

Private   Sub Command1_Click()
Form1.Cls
Call DrawCoordinate
Call DrawPset
If Text1.Text <> "" And Text2.Text <> "" Then
    
Call Fill(Text1.Text, Text2.Text)
End If
End Sub


Private   Sub Form_Load()
    
Dim x0, y0, x1, y1 As Single
    x0 
= -ScaleWidth / 4
    y0 
= ScaleHeight - 200
    x1 
= ScaleWidth * 3 / 4 - 200
    y1 
= -200
    Scale (x0 
+ 1500, y0)-(x1 + 1500, y1)
    
Call Class_InitializeX
    
Call Class_InitializeY
End Sub


Private   Sub Form_Activate()
Dim X, Y As Integer
= 0
= 0
Call DrawCoordinate     '调用画格子过程
Call DrawPset
End Sub


Private   Sub DrawCoordinate()        '画坐标方格
Dim i As Integer
For i = 1 To xMax + 1
    Line (
0, Y)-(xMax * grid, Y)
    Line (X, 
0)-(X, xMax * grid)
    X 
= X + grid
    Y 
= Y + grid
Next
End Sub


Private   Sub DrawPset()
Dim i As Integer
    DrawWidth 
= 15 '划笔粗细
    For i = 2 To 12
            PSet (i 
* grid, 2 * grid), vbBlue
    
Next i
    
For i = 2 To 12
            PSet (
2 * grid, i * grid), vbBlue
    
Next i
    
For i = 2 To 12
            PSet (
12 * grid, i * grid), vbBlue
    
Next i
    
For i = 2 To 12
            PSet (i 
* grid, 12 * grid), vbBlue
    
Next i
    DrawWidth 
= 1: X = 0: Y = 0
End Sub


Private   Sub Class_InitializeX()
    
Set cltStackX = New Collection
    lTopX 
= 0
End Sub


Public   Sub PushX(data As Integer)
    cltStackX.Add data, 
CStr(lTopX)
    lTopX 
= lTopX + 1
End Sub


Public   Function PopX() As Integer
    PopX 
= cltStackX.Item(lTopX)
    cltStackX.Remove (lTopX)
    lTopX 
= lTopX - 1
End Function


Public   Function IsEmptyX() As Boolean
    IsEmptyX 
= (lTopX <= 0)
End Function


Private   Sub Class_InitializeY()
    
Set cltStackY = New Collection
    lTopY 
= 0
End Sub


Public   Sub PushY(data As Integer)
    cltStackY.Add data, 
CStr(lTopY)
    lTopY 
= lTopY + 1
End Sub


Public   Function PopY() As Integer
    PopY 
= cltStackY.Item(lTopY)
    cltStackY.Remove (lTopY)
    lTopY 
= lTopY - 1
End Function


Public   Function IsEmptyY() As Boolean
    IsEmptyY 
= (lTopY <= 0)
End Function


Private   Sub Fill(ptX As Integer, ptY As Integer)
    
Dim Sign As Boolean
    
Dim xRight, xLeft As Integer
    
Dim xT, yT As Integer
    
Dim i As Integer
    
Dim counter As Double
    DrawWidth 
= 15 '划笔粗细
    If ptX > 2 And ptX < 12 And ptY > 2 And ptY < 12 Then
        Sign 
= True
    
End If
    
If Sign = True Then
        PushX (ptX)
        PushY (ptY)
        
Do While IsEmptyX = False
            
'处理种子所在线
            xT = PopX
            yT 
= PopY
            
            i 
= xT
            
Do
                
For counter = 1 To 1000000
                
Next
                
If Point(i * grid, yT * grid) = vbBlue Then
                    
Exit Do
                
End If
                PSet (i 
* grid, yT * grid), vbRed       '填充点
                i = i + 1
            
Loop
            
            i 
= xT
            
Do
                
For counter = 1 To 1000000
                
Next
                
If Point(i * grid, yT * grid) = vbBlue Then
                    
Exit Do
                
End If
                PSet (i 
* grid, yT * grid), vbRed       '填充点
                i = i - 1
            
Loop
            
'处理下一条线
            i = xT
            
If Point(i * grid, (yT - 1* grid) <> vbBlue And Point(i * grid, (yT - 1* grid) <> vbRed Then
                
Do
                    
If Point(i * grid, (yT - 1* grid) = vbBlue Then
                        xLeft 
= i + 1
                        PushX (xLeft)
                        PushY (yT 
- 1)
                        
Exit Do
                    
End If
                    i 
= i - 1
                
Loop
            
End If
            
'处理上一条线
            i = xT
            
If Point(i * grid, (yT + 1* grid) <> vbBlue And Point(i * grid, (yT + 1* grid) <> vbRed Then
                
Do
                    
If Point(i * grid, (yT + 1* grid) = vbBlue Then
                        xLeft 
= i + 1
                        PushX (xLeft)
                        PushY (yT 
+ 1)
                        
Exit Do
                    
End If
                    i 
= i - 1
                
Loop
            
End If
        
Loop
    
Else
        
MsgBox ("所用种子点超出正方形范围...")
    
End If
    DrawWidth 
= 1: X = 0: Y = 0
End Sub

新人写的一个非典型扫描线填充正方形的4-连通边界填充算法...用到了栈的结构...VB6.0

如果有幸被高人看见...希望指正...

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值