C# 找到与不精确点相交的点

C# 找到与不精确点相交的点,c#,C#,我有个问题。我创建此类是为了查找六边形的所有共享边: 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 f

我有个问题。我创建此类是为了查找六边形的所有共享边:

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;
    }
}
公共类六边形注册表列表
{
public int HexagonNum{get;set;}
公共浮点x1{get;set;}
公共浮点y1{get;set;}
公共浮点x2{get;set;}
公共浮点y2{get;set;}
公共浮点x3{get;set;}
公共浮点y3{get;set;}
公共浮点x4{get;set;}
公共浮点y4{get;set;}
公共浮点x5{get;set;}
公共浮点y5{get;set;}
公共浮点x6{get;set;}
公共浮点y6{get;set;}
公共int ShapeNum{get;set;}
公共六边形注册列表()
{
this.AdjacentShapeNumbers=新列表();
}
公共列表邻接ShapeEnumber{get;set;}
公共IEnumerable GetPoints()
{
收益率收益率(x1,y1);
收益率收益率(x2,y2);
收益率收益率(x3,y3);
收益率收益率(x4,y4);
收益率收益率(x5,y5);
收益率收益率(x6,y6);
}
公共布尔值是相邻的(六边形注册表列表其他)
{
var Isajacentto=
GetPoints().Intersect(其他.GetPoints()).Count()>=2;
如果(与之相邻)
{
如果(other.shapeenum!=0)
{
Add(其他.ShapeNum);
}
}
返回相邻位置;
}
}

但现在我想要一些东西,所以值不必完全相同,但它们可以有最大值1的差异。所以当我比较350和350时,也可能是350和349,或者350和351。有人能帮我吗?

您需要创建一个使用有界绝对差来确定接近度的实现;这可以按如下方式进行

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;
}
var-DIFF_阈值=1.0f;
var DIFF_THRESHOLD_SQUARE=DIFF_THRESHOLD*DIFF_THRESHOLD;
专用IsCloseTo(浮点x1、浮点y1、浮点x2、浮点y2)
{
泛绿变种
=(x1-x2)*(x1-x2)+(y1-y2)*(y1-y2);

return EuklidenDistance定义自定义比较器:

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;
    }
}

您可以通过比较阈值的平方来避免平方根计算。@trollingchar是的,当然,谢谢您的评论。如果它们相等,我只需要返回一个布尔值,因为
var isAdjacentTo=GetPoints().Intersect(other.GetPoints(),new PointComparer())
本来应该是bool
Intersect
只支持
IEqualityComparer
并返回
IEnumerable
,我不明白你说的bool是什么意思?如果你想把
EqualityComparer
传递给
Intersect
,那么你需要编写自己的重载。我需要使用这样的东西:
var isAdjacentTo=GetPoints().Intersect(other.GetPoints(),new PointComparer()).Count>=2;
但这给了我一个错误:运算符'>='不能应用于'method group'和'int'类型的操作数…..我如何解决这个问题
Count
之后缺少括号,应该是
.Count()
,看看你的问题。它仍然不起作用。我有两个不同的y坐标:935.3074和935.3075。当我调用这一行时,
var isAdjacentTo=GetPoints().Intersect(other.GetPoints(),new PointComparer()).Count()>=2;
它仍然返回false?
GetPoints().Intersect(other.GetPoints(), new PointComparer())