winforms 检测旋转圆的边界何时碰到正方形

pokxtpni  于 2022-12-04  发布在  其他
关注(0)|答案(1)|浏览(204)

我在尝试检测一个圆时遇到了麻烦,这个圆跟随着圆中的一个点,它碰到了一个标签。当它碰到标签时,我希望标签移到屏幕的右上方。到目前为止,它会画这个圆,并且永远绕着这个点,但是它会直接穿过标签,而标签不会移到左上方。这是我的代码。
第一个
我试着在Angular 指向的地方画一个辅助圆,但是没有成功,因为我没有旋转圆的x和y坐标。它没有失败,但是无论我画多大,什么都没有显示。

pw136qt2

pw136qt21#

使用Matrix类执行相同的操作(平移和旋转)。然后从该转换点构建一个矩形,现在您就有了一个矩形,它在与Label相同的坐标系中表示行星(相对于左上角的Forms原点)。现在我们可以简单地使用Rectangle.IntersectsWith()方法来测试冲突。
示例代码:

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

下面是它的实际应用:

相关问题