我有问题。我创建了此类,以查找六边形的所有共享边:
public class HexagonRegistryList
{
public int HexagonNum { get; set; }
public float x1 { get; set; }
public float y1 { get; set; }
public float x2 { get; set; }
public float y2 { get; set; }
public float x3 { get; set; }
public float y3 { get; set; }
public float x4 { get; set; }
public float y4 { get; set; }
public float x5 { get; set; }
public float y5 { get; set; }
public float x6 { get; set; }
public float y6 { get; set; }
public int ShapeNum { get; set; }
public HexagonRegistryList()
{
this.AdjacentShapeNumbers = new List<int>();
}
public List<int> AdjacentShapeNumbers { get; set; }
public IEnumerable<(float x, float y)> GetPoints()
{
yield return (x1, y1);
yield return (x2, y2);
yield return (x3, y3);
yield return (x4, y4);
yield return (x5, y5);
yield return (x6, y6);
}
public bool IsAdjacentTo(HexagonRegistryList other)
{
var isAdjacentTo =
GetPoints().Intersect(other.GetPoints()).Count() >= 2;
if (isAdjacentTo)
{
if (other.ShapeNum != 0)
{
AdjacentShapeNumbers.Add(other.ShapeNum);
}
}
return isAdjacentTo;
}
}
但是现在我想要一些东西,因此值不必完全相同,但是它们可以具有最大1的差。因此,当我将350与350进行比较时,也可能是349与350的关系,或者350和351。有人可以帮我吗?
答案 0 :(得分:1)
定义自定义比较器:
public struct PointComparer : IEqualityComparer<(float x, float y)>
{
public bool Equals((float x, float y) p1, (float x, float y) p2)
{
return Math.Abs(p1.x - p2.x) < 1f && Math.Abs(p1.y - p2.y) < 1f;
}
public int GetHashCode((float x, float y) obj)
{
return 1;
}
}
然后将其传递给Intersect
方法
GetPoints().Intersect(other.GetPoints(), new PointComparer())
答案 1 :(得分:0)
您将需要创建一个实现,该实现使用有限的绝对差来确定邻近度;可以按照以下步骤进行。
var DIFF_THRESHOLD = 1.0f;
var DIFF_THRESHOLD_SQUARE = DIFF_THRESHOLD * DIFF_THRESHOLD;
private IsCloseTo(float x1, float y1, float x2, float y2)
{
var EuklideanDistance
= ( x1 - x2 ) * ( x1 - x2 ) + ( y1 - y2 ) * ( y1 - y2 );
return EuklidenDistance <= DIFF_THRESHOLD_SQUARE;
}
然后,您可以使用Linq来确定彼此接近的点数。