tags:

views:

350

answers:

2

I have a Polygon (a Hex for a board game) in Silverlight, something like;

public class GridHex : IGridShape { ..... public IList Points { get; protected set; } public bool Intersects(Point point) { ... } }

I would like to be able to say

if(myHex.Intersects(clickedPoint)) { ... }

However I'm unsure of which algorithm to use within the Intersects method - currently I'm using an internal "bounding box" inside each Hexagon to detect if a point is within it, but there must be an algorithm to figure this out? I know the coordinates for the 6 points of each Hexagon.

I was thinking I could maybe create a Silverlight Polygon and do some kind of hit testing? Of course this would be rather memory intensive (I'd be iterating a large number of Hexes to see which Hex a mouse click falls within...) so it might be better to use a mathematical formula to work out if a point is within a Hex....

+2  A: 

This page nicely explains an algorithm for finding out if a point is inside a polygon.

Toon Van Acker
+1  A: 

Have you looked into native support using the FindElementsInHostCoordinates? I'd expect this to be faster since it probably utilizes unmanaged code.

Here's a sample which works with vector shapes, supported in Silverlight 2.

And here's an updated sample which utilizes a WriteableBitmap to extend support for hit testing to bitmap images, as well.

Jon Galloway