gpt4 book ai didi

c# - 查找与不精确点的交点

转载 作者:行者123 更新时间:2023-11-30 17:26:36 24 4
gpt4 key购买 nike

我有一个问题。我创建了这个类来查找六边形的所有共享边:

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 时,它也可以是 350 和 349,或者350 和 351。有人可以帮我吗?

最佳答案

定义自定义比较器:

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())

关于c# - 查找与不精确点的交点,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56231969/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com