标题:关于VB中绝对坐标和相对坐标的问题
取消只看楼主
maosuhang
Rank: 1
等 级:新手上路
帖 子:6
专家分:0
注 册:2011-4-24
 问题点数:0 回复次数:5 
关于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
搜索更多相关主题的帖子: Object 
2011-04-24 23:03
maosuhang
Rank: 1
等 级:新手上路
帖 子:6
专家分:0
注 册:2011-4-24
得分:0 
2011-04-24 23:04
maosuhang
Rank: 1
等 级:新手上路
帖 子:6
专家分:0
注 册:2011-4-24
得分:0 
2011-04-24 23:04
maosuhang
Rank: 1
等 级:新手上路
帖 子:6
专家分:0
注 册:2011-4-24
得分:0 
找到问题了,是数据类型设置错了。m和FI应该设置成DOUBLE型
2011-04-24 23:53
maosuhang
Rank: 1
等 级:新手上路
帖 子:6
专家分:0
注 册:2011-4-24
得分:0 
不是什么绝对坐标和相对坐标的问题
2011-04-24 23:53
maosuhang
Rank: 1
等 级:新手上路
帖 子:6
专家分:0
注 册:2011-4-24
得分:0 
不是什么绝对坐标和相对坐标的问题
2011-04-24 23:53



参与讨论请移步原网站贴子:https://bbs.bccn.net/thread-337798-1-1.html




关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 1.341726 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved