Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

User drawn Polygon Hit Testing 1

Status
Not open for further replies.

SqueakinSweep

Programmer
Jun 20, 2002
945
GB
We had a thread a few months back, where someone wanted a clickable Map. Rick came up with a cracking solution for that by using a gradient background image onto which the mouse click was transposed to work out where the click was made. I have always wanted however to figure out how it was possible to determine if a mouse click was inside a user drawn polygon. There are mathematical approaches to this solution, but after a little playing around and research, I realised that .Net had this facility built in after all. The following code will show how this is done.
All that is on the form is a picturebox which is named mapdraw. Any click inside the drawn polygon will activate a Messagebox.

Code:
Imports System.Drawing
Dim WASHINGTON As PointF()

Private Sub formGraphics_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

'Defines new bitmap
Dim states As Bitmap = New Bitmap(mapdraw.Width, mapdraw.Height)
'Draws image to Picturebox
Me.mapdraw.Image = states
'Define graphics holder
Dim g As Graphics = Graphics.FromImage(states)

WASHINGTON = New PointF() { _
        New PointF(95, 35), _
        New PointF(82, 72), _
        New PointF(93, 78), _
        New PointF(92, 88), _
        New PointF(158, 110), _
        New PointF(177, 63), _
        New PointF(113, 35), _
        New PointF(112, 46) _
        }

Dim p As New Pen(Color.Blue)
g.DrawPolygon(p, WASHINGTON)

End Sub

Private Sub mapdraw_MouseDown(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles mapdraw.MouseDown

If Me.PolyGonHitTest(sample, New PointF(e.X, e.Y)) Then
    MsgBox("IN")
End If

End Sub

Public Function PolyGonHitTest(ByVal polygonPoints() As PointF, ByVal mousePos As PointF) As Boolean

Dim path As New System.Drawing.Drawing2D.GraphicsPath

path.AddLines(polygonPoints)
Dim region As New Region(path)

' Hittest the region to verify if the point is in the Polygon
If region.IsVisible(mousePos) Then Return True
Return False

End Function




Sweep
...if it works dont f*** with it
...if its f****ed blame someone else
...if its your fault that its f***ed, say and admit nothing.
 
Sweep,

I tried your algorithm, but it doesn't work for me. All I did was copy paste the code to a vb.net (2002) form. Then moved the imports line so that it appears above the 'Public Class Form' line. When I run the project, I get an error "Name 'sample' is not declared". This is in the mapdraw_MouseDown event.

I wrote a mapping application in VB6 with this same functionality but implemented with a COMPLETLY different mathematical alogrithm. The benefit of doing this the hard way is that I was able to re-write the algorithm in a SQL Server stored procedure. If anyone is interested, I could probably be convinced to share.

-George

Strong and bitter words indicate a weak cause. - Fortune cookie wisdom
 
George

My apologies, the word sample in the mouse down event should refer to the defined shape which in the example above is WASHINGTON.


Sweep
...if it works dont f*** with it
...if its f****ed blame someone else
...if its your fault that its f***ed, say and admit nothing.
 
Thanks. It works great now.

-George

Strong and bitter words indicate a weak cause. - Fortune cookie wisdom
 
Rick

I would agree entirely, and the solutions offered would I see be used in entirely different circumstances. Basically I have a parts table which describes a parts dimensions, and also defines its basic shape. From a combination of the shape definition and the part dimensions it is possible to easily create a list of polygon defining points. In effect I guess I am creating a very basic CAD system.


Sweep
...if it works dont f*** with it
...if its f****ed blame someone else
...if its your fault that its f***ed, say and admit nothing.
 
I think it's worth a star.

Christiaan Baes
Belgium

If you want to get an answer read this FAQ faq796-2540
There's no such thing as a winnable war - Sting
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top