Quantcast
Channel: VBForums - Visual Basic .NET
Viewing all articles
Browse latest Browse all 27220

VS 2010 VB2010 : how to detect mouse collision with a point?

$
0
0
Use the following controls on form1:
- button named button1
- picturebox named XY
- checkbox named checkbox1

past the following code in the editor:

Code:

Public Class Form1
    Public bmp As Bitmap
    Public gfx As Graphics
    Public numpoints As Integer = 10000
    Public t(numpoints) As Point

    Private Sub XY_Paint(ByVal sender As Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles XY.Paint
        XY.Image = bmp
    End Sub

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Me.WindowState = FormWindowState.Maximized
        bmp = New Bitmap(XY.Width, XY.Height)
        gfx = Graphics.FromImage(bmp)
    End Sub

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        gfx.Clear(Color.Black)
        For r = 1 To numpoints
            t(r).X = CInt(Rnd() * (XY.Width - 1))
            t(r).Y = CInt(Rnd() * (XY.Height - 1))
        Next
        If CheckBox1.Checked Then
            gfx.DrawLines(Pens.DarkOliveGreen, t)
        End If
        For r = 1 To numpoints
            bmp.SetPixel(t(r).X, t(r).Y, Color.Yellow)
        Next
    End Sub

    Private Sub CheckBox1_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CheckBox1.CheckedChanged
        gfx.Clear(Color.Black)
        If CheckBox1.Checked Then
            gfx.DrawLines(Pens.DarkOliveGreen, t)
        End If
        For r = 1 To numpoints
            bmp.SetPixel(t(r).X, t(r).Y, Color.Yellow)
        Next
    End Sub
End Class

and tell me how (different ways?) I can detect when the mouse collides with any yellow point on screen. The collision detection should return the appropriate index from the t()-array.

Viewing all articles
Browse latest Browse all 27220

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>