关于VB中绝对坐标和相对坐标的问题
我用VB写了一个程序,是用来插补一条直线的。但是不知道为什么直线和插补路径好像和Y=kx+b对称,只有在K=45时,才重合。我是用VS2010编写的。附程序:期中,我定义了一个700*700的框来画图。X1,x2,y1,y2分别是两个点坐标。求助!!!
Private Sub PictureBox1_Paint(ByVal sender As Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles PictureBox1.Paint
Dim x1 As Integer
Dim y1 As Integer
Dim x2 As Integer
Dim y2 As Integer
Dim xi As Integer
Dim yi As Integer
Dim xn As Integer
Dim yn As Integer
Dim m As Integer
'Dim n As Integer
Dim EI As Integer
Dim FI As Integer
Dim blackPen As New Pen(Color.Black, 1)
Dim redPen As New Pen(Color.Red, 1)
e.Graphics.Clear(Color.Teal)
'Draw the coordinate system to screen.
e.Graphics.DrawLine(blackPen, 0, 350, 700, 350)
e.Graphics.DrawLine(blackPen, 350, 0, 350, 700)
'get the EI and FI
EI = Math.Abs(a - i) + Math.Abs(j - b)
FI = 0
x1 = i
y1 = j
x2 = a
y2 = b
'draw the line
e.Graphics.DrawLine(blackPen, 350 + i * 5, 350 - j * 5, 350 + a * 5, 350 - b * 5)
'ruin
If x1 - x2 = 0 Then
e.Graphics.DrawLine(redPen, 350 + i * 5, 350 - j * 5, 350 + a * 5, 350 - b * 5)
ElseIf y1 - y2 = 0 Then
e.Graphics.DrawLine(redPen, 350 + i * 5, 350 - j * 5, 350 + a * 5, 350 - b * 5)
Else
'xi = i
'yi = j
EI = Math.Abs(x1 - x2) + Math.Abs(y1 - y2)
If x1 < x2 Then
xi = x1
yi = y1
Else
xi = x2
yi = y2
End If
m = (y2 - y1) / (x2 - x1)
Select Case m
Case Is > 0 'when k>0
While EI >= 0
If FI >= 0 Then
'when the point above the line
xn = xi + 1
yn = yi
e.Graphics.DrawLine(redPen, 350 + xi * 5, 350 - yi * 5, 350 + xn * 5, 350 - yn * 5)
'when the point below the line
Else
xn = xi
yn = yi + 1
e.Graphics.DrawLine(redPen, 350 + xi * 5, 350 - yi * 5, 350 + xn * 5, 350 - yn * 5)
End If
xi = xn
yi = yn
EI = EI - 1
FI = yi - m * (xi - x1) - y1
End While
Case Is < 0
While EI >= 0
If FI >= 0 Then
xn = xi
yn = yi - 1
e.Graphics.DrawLine(redPen, 350 + xi * 5, 350 - yi * 5, 350 + xn * 5, 350 - yn * 5)
Else
xn = xi + 1
yn = yi
e.Graphics.DrawLine(redPen, 350 + xi * 5, 350 - yi * 5, 350 + xn * 5, 350 - yn * 5)
End If
xi = xn
yi = yn
EI = EI - 1
FI = yi - m * (xi - x1) - yi
End While
End Select
End If
End Sub
End Class