Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/394.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 寻找具有匹配斜率的点_Java_Points - Fatal编程技术网

Java 寻找具有匹配斜率的点

Java 寻找具有匹配斜率的点,java,points,Java,Points,假设我有一个点(0,0),我说它将用作原点。如何检查以下点(在阵列中)是否与原点具有相同的坡度 要点是: (6000, 7000) (10000, 0) (16000, 17000) (7000, 3000) (3000, 7000) (20000, 21000) (3000, 4000) (0, 10000). 基本上,我想比较每个点与原点的关系,看看哪些点具有相同的斜率,并将这些点分组在单独的列表中。我只是对它背后的算法和逻辑有点困惑。我知道for循环最好,但它的实现似乎离我越来越远

假设我有一个点(0,0),我说它将用作原点。如何检查以下点(在阵列中)是否与原点具有相同的坡度

要点是:

(6000, 7000) (10000, 0) (16000, 17000) (7000, 3000) 
(3000, 7000) (20000, 21000) (3000, 4000) (0, 10000).
基本上,我想比较每个点与原点的关系,看看哪些点具有相同的斜率,并将这些点分组在单独的列表中。我只是对它背后的算法和逻辑有点困惑。我知道for循环最好,但它的实现似乎离我越来越远

   for (int j = 0; j < array.length - 1; i++)
for(int j=0;j

这就是我开始失去理智的地方

您描述的方法是正确的。您希望“查看哪些点具有相同的坡度,并将这些点分组到单独的列表中”

您可以使用
映射
为您处理分组,如:

Map<BigDecimal, List<Point>> lists = new HashMap<BigDecimal, List<Point>>();
for (Point point : points) {
    BigDecimal slope = new BigDecimal(point.getY()).divide(new BigDecimal(point.getX()));
    List<Point> list = lists.get(slope);
    if (list == null) {
        list = new ArrayList<Point>();
        lists.put(slope, list);
    }
    list.add(point);
}
Map list=newhashmap();
用于(点:点){
BigDecimal斜率=新的BigDecimal(point.getY()).divide(新的BigDecimal(point.getX());
List=lists.get(斜率);
if(list==null){
列表=新的ArrayList();
列表。放置(坡度、列表);
}
列表。添加(点);
}

请注意,这使用了任意精度
BigDecimal
类来避免与基本浮点类型的舍入相关的问题。如果您不关心这一点,您可以使用
Double
Double
来代替。

创建点列表,迭代列表,计算坡度,并根据地图中的每个计算坡度维护点列表,如下所示:

        List<int[]> myPoints = new ArrayList<int[]>();
        int[] point1 = new int[]{6000, 7000};
        myPoints.add(point1);
        int[] point2 = new int[]{10000, 0};
        myPoints.add(point2);
        int[] point3 = new int[]{16000, 17000};
        myPoints.add(point3);

        Map<Float, List<int[]>> myMatchingSlopePoints = new HashMap<Float, List<int[]>>();
        for(int[] point: myPoints){
            Float slope = new Float(point[1]/point[0]);
            if(myMatchingSlopePoints.get(slope) == null){
                //create a new list as this slope doesn't match with previous one
                myMatchingSlopePoints.put(slope, new ArrayList<int[]>());
            }
            //add the slope to match list
            myMatchingSlopePoints.get(slope).add(point);
        }

        //retrieve various slope
        Set<Float> variousFloats = myMatchingSlopePoints.keySet();

        //retrieve mathing points for each slope
        for(Float slope: variousFloats){
            List<int[]> matchingPointsListForSlope = myMatchingSlopePoints.get(slope);
            //use matching points
        }
List myPoints=new ArrayList();
int[]point1=新的int[]{6000,7000};
添加(第1点);
int[]point2=新的int[]{10000,0};
添加(第2点);
int[]point3=新的int[]{16000,17000};
添加(第3点);
Map myMatchingSlopePoints=new HashMap();
对于(int[]点:myPoints){
浮动斜率=新浮动(点[1]/点[0]);
if(myMatchingSlopePoints.get(斜率)==null){
//创建新列表,因为此坡度与上一个坡度不匹配
myMatchingSlopePoints.put(斜率,新ArrayList());
}
//将坡度添加到匹配列表
myMatchingSlopePoints.get(斜率).add(点);
}
//检索各种坡度
设置variousFloats=myMatchingSlopePoints.keySet();
//检索每个坡度的匹配点
用于(浮动坡度:各种浮动){
列出匹配点slistforslope=myMatchingSlopePoints.get(斜率);
//使用匹配点
}

这不只是两者的划分吗?斜率=(y2-y1)/(x2-x1)最简单的方法:使用以斜率为键的列表映射(map)来收集具有相同斜率的点(参见关于如何计算它的其他注释)。更好的解决方案是考虑浮点运算的不精确性,但您可能可以忽略这个问题。除了使用for循环并使用并排比较来比较每个点相对于原点的斜率外,我还有什么方法可以完成同样的事情吗?不幸的是,到目前为止,我的java技能还不太高级。@user1710357-你可以,但效率会更低。对于
循环,您需要使用两个
,这样,对于每个点,您可以每隔一点检查一次,以查看它是否具有相同的坡度。不管怎样,你仍然需要在某个地方收集你的结果。你能告诉我如何使用这两个for循环吗?我真的很感谢你迄今为止给我的帮助!