通过与LINQ的距离来过滤XY点 [英] Filter points XY by distance with LINQ

查看:182
本文介绍了通过与LINQ的距离来过滤XY点的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个XY列表,我想按照给定的距离对它们进行分组,假设它们之间x距离处的所有点应该分组在不同的列表中。



基本上,如果我有A =(0,0),B =(0,1),C =(0,2),我想要组合所有具有maxDistance为1的点,以获得:[[A,B],[C]];

解决方案

对不起,基本上即时通讯使用C#和我不包括聚类为特定的目的,假设我有一些点和他们的ID,我需要聚集他们,保持有关ID的信息,然后简单地在X轴上做一个中间点,在这个位置属性分组。



最后,分数是最多10个,保持有关id的信息是非常重要的知道谁在哪里,所以我想收集点足够接近的点,然后使用该列表的坐标列出结果,做得很原始,因为在急于,但福所以我使用了这样的东西:




pre> // class to hold information

public class userObject {

public string id;
public Vector3D position = Vector3D.Zero;

public userObject(string Id,Vector3D Position){

id = Id;
position =头寸;




$ b //分组ID的列表(nanocluster :)

public Dictionary< int,List< ; userObject>>插槽;



private void forceCheck(){



//根据传入的坐标(id和point vector3d)

List< userObject> users = new List< userObject>(); (int a = 0; a< FId_In.SliceCount; a ++){

userObject uo = new userObject(FId_In [a],FPositions_In [a]);



users.Add(uo);
}



//清理结果,这是不同的另一个版本im工作



slots = new Dictionary< int,List< userObject>>();


//检查关闭点(应该改变几行以实现一个真正的clustring,但是这样我可以控制所有的点将不会创建一个横向集群,告诉你raw (int k = 0; k< users.Count; k ++){

List< userObject> matches = new List< userObject>(); $ b在
$ b $

$ b //检查ids是否已经在一个插槽中注册
int isInSlot = checkIdInSlots(users [k] .id);


如果(isInSlot == -1){

matches.Add(users [k]);

for(int j = k + 1; j < users.Count; j ++){




//调用一个函数来检查x距离,但是可以在需要时使用完整的vector3d

if(checkClose(users [k] .position,users [j] .position,FXThreshold_In [0])){


matches.Add(users [j]);

}


}



//最后添加分组标识....确保所有这些都是一行linq:D

addNewSlot(matches );

}



$ b}


}

我们很高兴能够更好地理解linq如何用于实现相同的结果,当然可以更加健壮,谢谢大家:)


I have a list of points XY and I want to group them by a given distance, let's say all the points that are at x distance between them should be grouped in different list.

Basically if I have A=(0,0), B=(0,1), C=(0,2), I want to group all points that have a maxDistance of 1, in order to obtain :[[A,B],[C]] ;

解决方案

Sorry to all, i made a post not so clear, basically im using c# and i was excluding clustering for specific purpose, let's say i have some points and their ids and i need to "cluster them", keeping information about ids , then simply made a medium point on X axis, cos im interested only in grouping by that position attribute.

At the end, points are maximum 10, and keeping information about ids is very important to know who is where, so i thought to collect ids of points close enough and then use that list of list of coordinates to make out results, did it very raw, cos im in a rush, but fully opened to further implementation, just im not able to use linq :)

So i used something like this :

// class to hold information

public class userObject{

        public string id;
        public Vector3D position=Vector3D.Zero;

        public userObject(string Id, Vector3D Position){

            id=Id;
            position=Position;


        }
    }

    // list of grouped ids (nanocluster :)

    public Dictionary<int, List<userObject>> slots ;



    private void forceCheck(){



        // create list of object from incoming coordinates (ids and point vector3d) 

        List<userObject> users=new List<userObject>();

        for(int a=0;a<FId_In.SliceCount;a++){

            userObject uo=new userObject(FId_In[a],FPositions_In[a]);

            users.Add(uo);
        }



        // Clean result, this is different in another version im working on



        slots =new Dictionary<int,List<userObject>>();


         // check for close points ( a couple of lines should be changed to achieve a real clustring, but this way i can control all points will not create an horizontal cluster, told u raw mode on 

        for(int k=0;k<users.Count;k++){

            List<userObject> matches=new List<userObject>();


            // Check if ids is already registered in one slot
            int isInSlot=checkIdInSlots(users[k].id);



            if(isInSlot==-1){

                matches.Add(users[k]);

                for(int j=k+1;j<users.Count;j++){




            // call a function to check x distance, but  can use full vector3d when needed

            if(checkClose(users[k].position,users[j].position,FXThreshold_In[0])){      


                        matches.Add(users[j]);

                    }


                }   



           // finally add entry with grouped ids....sure all this is a line of linq :D

                addNewSlot(matches);

            }




        }


    }

WOuld be nice to understand better how linq can be used to achive same result, sure can be more robust, thank you all :)

这篇关于通过与LINQ的距离来过滤XY点的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆