在让它打印 J 并旋转它时遇到问题,每当我点击按钮 1 时它就会崩溃,但除了制作正确的 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
Math.PI
等的值都是双精度值。将triangX
和triangY
更改为双精度或将结果转换为单倍。
我在你的代码中看到的两个问题。
首先你有 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))