public void GetDistanceBetweenFeatures_Works_As_Expected() { var feat1 = new Feature("place1", 0, 0); var feat2 = new Feature("place2", 0, 5); var distance = FeatureHelper.GetDistance(feat2, feat1); Assert.Equal(5, distance); }
public double FindDistanceToNearestNeighbour(KDNode currentNode, Feature goalFeature, KDNode closestNeighbour, double closestNeighbourDistance) { if (currentNode == null) { return(closestNeighbourDistance); } var currentDistance = FeatureHelper.GetDistance(currentNode.Feature, goalFeature); if (currentDistance < closestNeighbourDistance) { if (currentNode.Feature != goalFeature) { closestNeighbour = currentNode; closestNeighbourDistance = currentDistance; } } if (currentNode.UseXAxis) { if (goalFeature.X < currentNode.Feature.X) //is goal.x LEFT current node.x? IF so, traverse LEFT, otherwise go RIGHT (easy to understand because this is X comparison node) { closestNeighbourDistance = FindDistanceToNearestNeighbour(currentNode.LeftNode, goalFeature, closestNeighbour, closestNeighbourDistance); if (Math.Abs(currentNode.Feature.X - goalFeature.X) < closestNeighbourDistance) { closestNeighbourDistance = FindDistanceToNearestNeighbour(currentNode.RightNode, goalFeature, closestNeighbour, closestNeighbourDistance); } } else { closestNeighbourDistance = FindDistanceToNearestNeighbour(currentNode.RightNode, goalFeature, closestNeighbour, closestNeighbourDistance); if (Math.Abs(goalFeature.X - currentNode.Feature.X) < closestNeighbourDistance) { closestNeighbourDistance = FindDistanceToNearestNeighbour(currentNode.LeftNode, goalFeature, closestNeighbour, closestNeighbourDistance); } } } else { if (goalFeature.Y > currentNode.Feature.Y) //is goal.y ABOVE current node.y? IF so, traverse to RIGHT to go ABOVE (because this is a Y comparison node) otherwise go left to look down { closestNeighbourDistance = FindDistanceToNearestNeighbour(currentNode.RightNode, goalFeature, closestNeighbour, closestNeighbourDistance); if (Math.Abs(currentNode.Feature.Y - goalFeature.Y) < closestNeighbourDistance) { closestNeighbourDistance = FindDistanceToNearestNeighbour(currentNode.LeftNode, goalFeature, closestNeighbour, closestNeighbourDistance); } } else { closestNeighbourDistance = FindDistanceToNearestNeighbour(currentNode.LeftNode, goalFeature, closestNeighbour, closestNeighbourDistance); if (Math.Abs(goalFeature.Y - currentNode.Feature.Y) < closestNeighbourDistance) { closestNeighbourDistance = FindDistanceToNearestNeighbour(currentNode.RightNode, goalFeature, closestNeighbour, closestNeighbourDistance); } } } return(closestNeighbourDistance); }