public void ClosestPointOnLine_WhenGivenPoint_ShouldReturnClosestPoint() { var line = new[] { new Vector2(-5, 5), new Vector2(5, 5) }; var point = new Vector2(2.5F, 0); var nearest = helper.NearestPointOnLine(point, line); Assert.AreEqual(2.5, nearest.X, 0.0001); Assert.AreEqual(5, nearest.Y, 0.0001); }
private Vector2 FindNearestPoint(Vector2 point, Vector2[][] pairs) { float distance = float.MaxValue; Vector2 nearest = Vector2.Zero; for (int i = 0; i < pairs.Length; i++) { Vector2 n = geometryHelper.NearestPointOnLine(point, pairs[i]); float distToNearestPoint = Vector2.Distance(point, n); if (distToNearestPoint < distance) { distance = distToNearestPoint; nearest = n; } } return(nearest); }