主题:请教用VB做计算机图形学“扫描线种子填充”
Private Type point
X As Long
Y As Long
End Type
Dim oldP As point
Dim FirstP As point
Dim bFirst As Boolean
Dim result As Boolean
Dim xmin As Long, xmax As Long, ymin As Long, ymax As Long, ii As Long, jj As Long
Dim n, i, cnt As Integer
Dim polyarray() As point
Dim pt As point
Private Sub Command2_Click()
Picture1.Cls
End Sub
Private Sub Picture1_DblClick()
Picture1.Line (oldP.X, oldP.Y)-(FirstP.X, FirstP.Y)
bFirst = True
Line1.Visible = False
End Sub
Private Sub Form_Load()
'计算屏幕坐标
ReDim Preserve polyarray(1)
cnt = 1
bFirst = True
Line1.Visible = False
Picture1.AutoRedraw = True
End Sub
Private Sub Picture1_MouseDown(Button As Integer, Shift As Integer, X As Single, Y As Single)
ReDim Preserve polyarray(UBound(polyarray) + 1)
polyarray(cnt).X = X
polyarray(cnt).Y = Y
cnt = cnt + 1
If Button = 1 Then
Line1.X1 = X
Line1.Y1 = Y
If bFirst = False Then
Picture1.Line (oldP.X, oldP.Y)-(X, Y)
Else
bFirst = False
Line1.Visible = True
FirstP.X = X
FirstP.Y = Y
End If
oldP.X = X
oldP.Y = Y
End If
End Sub
Private Sub Picture1_MouseMove(Button As Integer, Shift As Integer, X As Single, Y As Single)
Line1.X2 = X
Line1.Y2 = Y
End Sub
Friend Function IsInRegion(p As point, polyarray() As point) As Boolean
'变量定义
ncross = 0
n = UBound(polyarray)
ReDim Preserve polyarray(n + 1)
polyarray(n + 1).X = polyarray(0).X
polyarray(n + 1).Y = polyarray(0).Y
x0 = p.X: y0 = p.Y
For i = 0 To n - 1
X1 = polyarray(i).X: Y1 = polyarray(i).Y
X2 = polyarray(i + 1).X: Y2 = polyarray(i + 1).Y
If (((p.X >= X1) And (p.X < X2)) Or ((p.X >= X2) And (p.X < X1))) Then
yt = Y1 + (p.X - X1) * (Y2 - Y1) / (X2 - X1) '求交点的纵坐标
If (yt > y0) Then
ncross = ncross + 1 '计算扫描线与多边形相交的次数
End If
End If
Next i
If ((ncross > 0) And ((ncross Mod 2) = 1)) Then '判断点是否在多边形内
IsInRegion = True
Else
IsInRegion = False
End If
End Function
Public Sub Command1_Click()
xmin = polyarray(1).X: xmax = polyarray(1).X: ymin = polyarray(1).Y: ymax = polyarray(1).Y [color=FF0000]'这里xmin,xmax,ymin,ymax值不能改变,怎么回事啊?[/color]
For i = 2 To cnt
If polyarray(i).X < xmin Then xmin = polyarray(i).X
If polyarray(i).Y < ymin Then ymin = polyarray(i).Y
If polyarray(i).X > xmax Then xmax = polyarray(i).X
If polyarray(i).Y > ymax Then ymax = polyarray(i).Y
Next i
For ii = ymin To ymax Step 150
For jj = xmin To xmax Step 150
pt.X = jj: pt.Y = ii
result = IsInRegion(pt, polyarray)
If result = True Then
Picture1.PSet (pt.X, pt.Y), vbRed
End If
Next jj
Next ii
End Sub
程序中红色部分有问题,敢问哪位高手能帮帮忙?谢谢
X As Long
Y As Long
End Type
Dim oldP As point
Dim FirstP As point
Dim bFirst As Boolean
Dim result As Boolean
Dim xmin As Long, xmax As Long, ymin As Long, ymax As Long, ii As Long, jj As Long
Dim n, i, cnt As Integer
Dim polyarray() As point
Dim pt As point
Private Sub Command2_Click()
Picture1.Cls
End Sub
Private Sub Picture1_DblClick()
Picture1.Line (oldP.X, oldP.Y)-(FirstP.X, FirstP.Y)
bFirst = True
Line1.Visible = False
End Sub
Private Sub Form_Load()
'计算屏幕坐标
ReDim Preserve polyarray(1)
cnt = 1
bFirst = True
Line1.Visible = False
Picture1.AutoRedraw = True
End Sub
Private Sub Picture1_MouseDown(Button As Integer, Shift As Integer, X As Single, Y As Single)
ReDim Preserve polyarray(UBound(polyarray) + 1)
polyarray(cnt).X = X
polyarray(cnt).Y = Y
cnt = cnt + 1
If Button = 1 Then
Line1.X1 = X
Line1.Y1 = Y
If bFirst = False Then
Picture1.Line (oldP.X, oldP.Y)-(X, Y)
Else
bFirst = False
Line1.Visible = True
FirstP.X = X
FirstP.Y = Y
End If
oldP.X = X
oldP.Y = Y
End If
End Sub
Private Sub Picture1_MouseMove(Button As Integer, Shift As Integer, X As Single, Y As Single)
Line1.X2 = X
Line1.Y2 = Y
End Sub
Friend Function IsInRegion(p As point, polyarray() As point) As Boolean
'变量定义
ncross = 0
n = UBound(polyarray)
ReDim Preserve polyarray(n + 1)
polyarray(n + 1).X = polyarray(0).X
polyarray(n + 1).Y = polyarray(0).Y
x0 = p.X: y0 = p.Y
For i = 0 To n - 1
X1 = polyarray(i).X: Y1 = polyarray(i).Y
X2 = polyarray(i + 1).X: Y2 = polyarray(i + 1).Y
If (((p.X >= X1) And (p.X < X2)) Or ((p.X >= X2) And (p.X < X1))) Then
yt = Y1 + (p.X - X1) * (Y2 - Y1) / (X2 - X1) '求交点的纵坐标
If (yt > y0) Then
ncross = ncross + 1 '计算扫描线与多边形相交的次数
End If
End If
Next i
If ((ncross > 0) And ((ncross Mod 2) = 1)) Then '判断点是否在多边形内
IsInRegion = True
Else
IsInRegion = False
End If
End Function
Public Sub Command1_Click()
xmin = polyarray(1).X: xmax = polyarray(1).X: ymin = polyarray(1).Y: ymax = polyarray(1).Y [color=FF0000]'这里xmin,xmax,ymin,ymax值不能改变,怎么回事啊?[/color]
For i = 2 To cnt
If polyarray(i).X < xmin Then xmin = polyarray(i).X
If polyarray(i).Y < ymin Then ymin = polyarray(i).Y
If polyarray(i).X > xmax Then xmax = polyarray(i).X
If polyarray(i).Y > ymax Then ymax = polyarray(i).Y
Next i
For ii = ymin To ymax Step 150
For jj = xmin To xmax Step 150
pt.X = jj: pt.Y = ii
result = IsInRegion(pt, polyarray)
If result = True Then
Picture1.PSet (pt.X, pt.Y), vbRed
End If
Next jj
Next ii
End Sub
程序中红色部分有问题,敢问哪位高手能帮帮忙?谢谢