Public Class Form1
Private WithEvents TMcircle As New Timer
Dim Angle As Single
Dim SunCentre As New Point(300, 300)
Dim SunRadius As Integer = 50
Dim OrbitRadius As Integer = 150
Dim PlanetDiameter As Integer = 10
Dim PlanetRadius As Integer = PlanetDiameter / 2
Dim PlanetCentre As New Point(OrbitRadius - PlanetRadius, 0)
Dim M As New Drawing2D.Matrix
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
TMcircle.Interval = 50
TMcircle.Enabled = False
End Sub
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
TMcircle.Start()
End Sub
Private Sub Tmr_Tick(sender As Object, e As System.EventArgs) Handles TMcircle.Tick
Angle += 2
Invalidate()
circleDetection()
End Sub
Public Sub Form1_Paint(sender As Object, e As System.Windows.Forms.PaintEventArgs) Handles Me.Paint
' draw sun
e.Graphics.TranslateTransform(SunCentre.X, SunCentre.Y)
Dim rc As New Rectangle(New Point(0, 0), New Size(1, 1))
rc.Inflate(SunRadius, SunRadius)
e.Graphics.FillEllipse(Brushes.Yellow, rc)
' draw planet
e.Graphics.RotateTransform(Angle)
rc = New Rectangle(PlanetCentre, New Size(1, 1))
rc.Inflate(PlanetRadius, PlanetRadius)
e.Graphics.FillEllipse(Brushes.Blue, rc)
End Sub
Public Sub circleDetection()
M.Reset()
M.Translate(SunCentre.X, SunCentre.Y)
M.Rotate(Angle)
Dim points() As Point = {PlanetCentre}
M.TransformPoints(points)
Dim planetRC As New Rectangle(points(0), New Size(1, 1))
planetRC.Inflate(PlanetRadius, PlanetRadius)
If planetRC.IntersectsWith(Label1.Bounds) Then
Label1.Top = 0
Label1.Left = 0
MessageBox.Show("you DIED!")
End If
End Sub
End Class
1条答案
按热度按时间pw136qt21#
使用Matrix类执行相同的操作(平移和旋转)。然后从该转换点构建一个矩形,现在您就有了一个矩形,它在与Label相同的坐标系中表示行星(相对于左上角的Forms原点)。现在我们可以简单地使用
Rectangle.IntersectsWith()
方法来测试冲突。示例代码:
下面是它的实际应用: