0

原点として使用されるポイント (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++)

そしてここで私は正気を失い始めます。

4

2 に答える 2

1

あなたが説明したアプローチは正しいです。「同じ勾配を共有しているものを確認し、それらのポイントを別々のリストにグループ化する」必要があります。

次のように、を使用しMapてグループ化を処理できます。

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

これは、任意精度BigDecimalクラスを使用して、プリミティブ浮動小数点型の丸めに関連する問題を回避することに注意してください。これを気にしない場合は、代わりにDoubleandを使用できます。double

于 2012-10-07T06:07:34.700 に答える
0

ポイントのリストを作成し、リストを反復し、勾配を計算し、以下のようにマップ内の計算された各勾配に対してポイントのリストを維持します。

        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
        }
于 2012-10-07T06:21:45.083 に答える