2016-03-02 20 views
-1

麻煩,使它打印J和旋轉它崩潰,每當我按下按鈕1,但適用於除了做一個其他的事情罰款正確的J和我嘗試改變的點,它仍然是試圖使VB中的圖形假設做一個J,並使旋轉,但它不斷使一個爛攤子

Public Class Form1 
    Dim g As Graphics 
    Dim triangX(12) As Single 
    Dim triangY(12) As Single 

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load 
     PictureBox1.Width = 200 
     PictureBox1.Height = 200 
     PictureBox1.BackColor = Color.White 
     g = PictureBox1.CreateGraphics 
     Call initTriangCoords(triangX, triangY) 
     Timer1.Interval = 200 
     Timer1.Enabled = False 
    End Sub 

    Private Sub initTriangCoords(ByRef x() As Single, ByRef y() As Single) 
     x(0) = 80 : y(0) = 80 
     x(1) = 50 : y(1) = 70 
     x(2) = 80 : y(2) = 70 
     x(3) = 80 : y(3) = 80 
     x(4) = 65 : y(4) = 80 
     x(5) = 55 : y(5) = 80 
     x(6) = 55 : y(6) = 130 
     x(7) = 65 : y(7) = 130 
     x(8) = 45 : y(8) = 120 
     x(9) = 40 : y(9) = 130 
     x(10) = 40 : y(10) = 110 
     x(11) = 45 : y(11) = 110 

    End Sub 

    Private Sub drawPolygon(ByRef x() As Single, ByRef y() As Single, ByVal totPoints As Integer, ByRef g As Graphics) 
     Dim i As Integer 
     For i = 1 To totPoints - 1 Step 1 
      g.DrawLine(Pens.Black, x(i), y(i), x(i + 1), y(i + 1)) 
     Next 
     g.DrawLine(Pens.Black, x(1), y(1), x(totPoints), y(totPoints)) 
    End Sub 

    Private Sub BtnDraw_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BtnDraw.Click 
     PictureBox1.Refresh() 
     Call drawPolygon(triangX, triangY, 12, g) 
    End Sub 

    Private Sub rotateFigure(ByRef x() As Single, ByRef y() As Single, ByVal totPoints As Integer, ByVal angle As Single) 

     Dim nx As Single 
     Dim ny As Single 
     Dim i As Integer 
     angle = angle * Math.PI/180 
     For i = 1 To totPoints Step 1 
      nx = x(i) * Math.Cos(angle) - y(i) * Math.Sin(angle) 
      ny = x(i) * Math.Sin(angle) + y(i) * Math.Cos(angle) 
      x(i) = nx 
      y(i) = ny 
     Next i 
    End Sub 

    Private Sub rotateFigure2(ByRef x() As Single, ByRef y() As Single, ByVal totPoints As Integer, ByVal angle As Single) 

     Dim nx As Single 
     Dim ny As Single 
     Dim i As Integer 
     angle = angle * Math.PI/180 
     For i = 1 To totPoints Step 1 
      nx = x(i) * Math.Sin(angle) - y(i) * Math.Cos(angle) 
      ny = x(i) * Math.Cos(angle) + y(i) * Math.Sin(angle) 
      x(i) = nx 
      y(i) = ny 
     Next i 
    End Sub 

    Private Sub BtnRotate_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BtnRotate.Click 
     Call rotateFigure(triangX, triangY, 14, 14) 
    End Sub 

    Private Sub rotateAroundPoint(ByRef x As Single, ByRef y As Single, ByVal angle As Single, ByVal xp As Single, ByVal yp As Single) 


     Dim nx, ny As Single 
     angle = angle * Math.PI/180 
     nx = ((x - xp) * Math.Cos(angle) - (y - yp) * Math.Sin(angle)) + xp 
     ny = ((x - xp) * Math.Sin(angle) + (y - yp) * Math.Cos(angle)) + yp 
     x = nx 
     y = ny 
    End Sub 

    Private Sub rotateFigureAroundPivot(ByRef x() As Single, ByRef y() As Single, ByVal angle As Single, ByVal xp As Single, ByVal yp As Single, ByVal totpoints As Integer) 

     Dim i As Integer 
     For i = 1 To totpoints Step 1 
      Call rotateAroundPoint(x(i), y(i), angle, xp, yp) 
     Next 
    End Sub 

    Private Sub BtnRotateAroundPivot_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BtnRotateAroundPivot.Click 
     Call rotateFigureAroundPivot(triangX, triangY, 12, triangX(0), triangY(0), 12) 
    End Sub 

    Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick 
     Call rotateFigureAroundPivot(triangX, triangY, 12, triangX(0), triangY(0), 12) 
     PictureBox1.Refresh() 
     Call drawPolygon(triangX, triangY, 12, g) 
    End Sub 

    Private Sub BtnActivateTimer_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BtnActivateTimer.Click 
     Timer1.Enabled = True 
    End Sub 

    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click 

     Call rotateFigure2(triangX, triangY, 112, 12) 
    End Sub 
End Class 

回答

1

Math.PI等的值都是雙打。 將您的triangXtriangY更改爲雙倍或將結果轉換爲單個。

我在代碼中看到兩個問題。
首先,你必須12點,但使用的是14分有

Call rotateFigure(triangX, triangY, 14, 14) 

或112點有

Call rotateFigure2(triangX, triangY, 112, 12) 

在vb.net開始第二排索引0
如果你想改變您需要使用的循環中的所有點

For i = 0 To totpoints - 1 Step 1 

drawPolygon應該是

For i = 0 To totPoints - 2 Step 1 
    g.DrawLine(Pens.Black, x(i), y(i), x(i + 1), y(i + 1)) 
Next 
g.DrawLine(Pens.Black, x(0), y(0), x(totPoints - 1), y(totPoints - 1)) 
相關問題