> I'll accept that, but I still need to perform a precise hittest against
> an arbitrary polygon. Using a Region is not necessary, but I cannot use
[quoted text clipped - 4 lines]
>
> matt tag
Precise? Not that I am aware of. Almost everything built into the Framework
is to deal with displayed graphics, not necessarily any sort of meaningful
analysis.
Below is some code I threw together that might help. It is based on some
other code I use elsewhere in a full Polygon Class, but have altered it to
work within your described context. I haven't thoroughly tested it as
presented, so make sure to test it well. It isn't exactly the most efficient
and certainly not the only method, but it is conceptually "simple" and works
well. You probably don't really need to worry about optimizations unless
your needs are beyond what I glean from here. Clearly, a number of things,
like obvious error trapping, and other things were left out. But as far as
simple polygons are concerned, it should be much more precise than what the
Framework provides.
'------------------------------------------------------------
'PointInPolygon - Basic PIP test
' Uses simple and popular method based upon intersection count
' along the X axis. This version treats special cases as follows:
' If point falls directly on left or top edge, then is inside.
' If point falls directly on right or bottom edge, then is outside.
' Meaning for our puposes, a point is always either Inside or not.
' TestPoint - PointF to test for containment
' PolyPoints() - Array of ordered vertices defining an arbitrary
' polygon. Coincident endpoints are implied and generated
' within this method.
'------------------------------------------------------------
Private Function PointInPolygon(ByVal TestPoint As PointF, _
ByVal PolyPoints() As PointF) As Boolean
Dim V0, V1 As PointF
Dim D1, D2 As Single
Dim idx As Integer, numPoints As Integer
Dim inside As Boolean = False
numPoints = PolyPoints.GetUpperBound(0)
' An imaginary closing segment is implied,
' so begin testing with that.
V1 = PolyPoints(numPoints)
For idx = 0 To numPoints
V0 = PolyPoints(idx)
D1 = (TestPoint.Y - V0.Y) * (V1.X - V0.X)
D2 = (TestPoint.X - V0.X) * (V1.Y - V0.Y)
If (TestPoint.Y < V1.Y) Then
'V1 below ray
If (V0.Y <= TestPoint.Y) Then
'V0 on or above ray
'Perform intersection test
If (D1 > D2) Then
inside = Not inside 'Toggle state
End If
End If
ElseIf (TestPoint.Y < V0.Y) Then
'V1 is on or above ray, V0 is below ray
'Perform intersection test
If (D1 < D2) Then
inside = Not inside 'Toggle state
End If
End If
V1 = V0 'Store previous endpoint as next startpoint
Next
Return inside
End Function
'------------------------------------------------------------