无法打印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
答案 0 :(得分:1)
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))