Hello, hopefully this is super simple for someone experienced with graphics and can explain the problem here (I'm guessing it has to do with Rounding.
I'm drawing a triangle here and I'm using IsVisible and doing a hit test on a point (to see if the point is inside the triangle. For testing purposes I'm scanning a few points in a couple of lines. The triangle is (0,0),(10,0),(10,10). I would've expected the point 5,5 to be right on the line (i.e. unsure if it hits), but certainly nothing earlier in X (e.g. 4.9,5) to be a hit. However if you look at my results below, this starts to evaluate true far earlier at 4.5,5. Is this just a rounding error? (sort of weird since it's so far).
Is there a better way to do a hit test? I can't just do equations as I want my code to be fairly generic.
private static GraphicsPath RunHitTestsWithGP()
{
var gp = new GraphicsPath();
gp.StartFigure();
gp.AddLines(new PointF[]
{
new PointF(0, 0),
new PointF(10, 0),
new PointF(10, 10),
new PointF(0, 0)
});
gp.CloseFigure();
for (int i = 0; i < 20; i++)
{
var t1 = new PointF(4f + i / 10f, 5);
System.Diagnostics.Debug.WriteLine("Testing: {0} Yields: {1}", t1, gp.IsVisible(t1));
}
for (int i = 0; i < 20; i++)
{
var t1 = new PointF(4f + i / 10f, 6);
System.Diagnostics.Debug.WriteLine("Testing: {0} Yields: {1}", t1, gp.IsVisible(t1));
}
return gp;
}
Testing: {X=4, Y=5} Yields: False
Testing: {X=4.1, Y=5} Yields: False
Testing: {X=4.2, Y=5} Yields: False
Testing: {X=4.3, Y=5} Yields: False
Testing: {X=4.4, Y=5} Yields: False
Testing: {X=4.5, Y=5} Yields: True
Testing: {X=4.6, Y=5} Yields: True
Testing: {X=4.7, Y=5} Yields: True
Testing: {X=4.8, Y=5} Yields: True
Testing: {X=4.9, Y=5} Yields: True
Testing: {X=5, Y=5} Yields: True
Testing: {X=5.1, Y=5} Yields: True
Testing: {X=5.2, Y=5} Yields: True
Testing: {X=5.3, Y=5} Yields: True
Testing: {X=5.4, Y=5} Yields: True
Testing: {X=5.5, Y=5} Yields: True
Testing: {X=5.6, Y=5} Yields: True
Testing: {X=5.7, Y=5} Yields: True
Testing: {X=5.8, Y=5} Yields: True
Testing: {X=5.9, Y=5} Yields: True
Testing: {X=4, Y=6} Yields: False
Testing: {X=4.1, Y=6} Yields: False
Testing: {X=4.2, Y=6} Yields: False
Testing: {X=4.3, Y=6} Yields: False
Testing: {X=4.4, Y=6} Yields: False
Testing: {X=4.5, Y=6} Yields: False
Testing: {X=4.6, Y=6} Yields: False
Testing: {X=4.7, Y=6} Yields: False
Testing: {X=4.8, Y=6} Yields: False
Testing: {X=4.9, Y=6} Yields: False
Testing: {X=5, Y=6} Yields: False
Testing: {X=5.1, Y=6} Yields: False
Testing: {X=5.2, Y=6} Yields: False
Testing: {X=5.3, Y=6} Yields: False
Testing: {X=5.4, Y=6} Yields: False
Testing: {X=5.5, Y=6} Yields: True
Testing: {X=5.6, Y=6} Yields: True
Testing: {X=5.7, Y=6} Yields: True
Testing: {X=5.8, Y=6} Yields: True
Testing: {X=5.9, Y=6} Yields: True