0

I am finding three neighbors of value (6,33).what is the best way to do that. am using json-Simple for reading file.if the origin is (6,33), then the first item in the output would be the coordinate closest to(6,33), then the second closest, then third closest, and so on for all 26 input coordinates:

[
    {"id":"a","value":[31,49]},

    {"id":"b","value":[44,67]},

    {"id":"c","value":[93,6]},

    {"id":"d","value":[20,16]},

    {"id":"e","value":[68,53]},

    {"id":"f","value":[71,8]},

    ....,

    {"id":"y","value":[75,92]},

    {"id":"z","value":[32,33]}

]

public class readData {

    public static void main(String[] args) throws FileNotFoundException,
    IOException, ParseException {

        Scanner xInput =  new Scanner(System.in);   
        System.out.println("Enter your X value");
        int x = xInput.nextInt();
        Scanner yInput = new Scanner(System.in);
        System.out.println("Enter your Y value");
        int y = yInput.nextInt();

        JSONParser parser = new JSONParser();
        JSONArray jsonArray = (JSONArray) parser.parse(new FileReader("E:\\data\\coordinates.json"));

        for (Object o : jsonArray) {
            JSONObject data = (JSONObject) o;

            String id = (String) data.get("id");
            System.out.print("id:" + id+"\t");      

            JSONArray value = (JSONArray) data.get("value");
            for(Object v: value){
                System.out.print(v+ " ");
            }
            System.out.println("\n");
        }

        xInput.close();
        yInput.close();
    }
}
Hakan Fıstık
  • 16,800
  • 14
  • 110
  • 131
Nan2015
  • 21
  • 4

1 Answers1

0

This work for me, please checkout next: 1. Only one instance of Scanner is needed 2. I use a point of dimension 3 (x,y,distance) for calculation

First I have an Euclidean calculation method

private static double distance(Double[] p1, Double[] p2){
    return Math.sqrt(Math.pow(p1[0] - p2[0], 2) + Math.pow(p1[1] - p2[1], 2));
}

Then a method for calculate the distance and set into position 3 of point (distance calculation)

private static List<Double[]> minimumDistance(List<Double[]> points, Double[] point){

    for(Double[] p : points){
        double d = distance(p, point);
        p[2] = d; //Save the distance at index-3 of point
    }

    Collections.sort(result, new Comparator<Double[]>() {
        @Override
        public int compare(Double[] o1, Double[] o2) {
            return (int)(o1[2] - o2[2]);
        }
    });

    return points; //this list is ordered desc by distance
}

Finally, the main

public static void main(String[] args) throws FileNotFoundException,
IOException, ParseException, org.json.simple.parser.ParseException {

    Scanner in =  new Scanner(System.in);   

    System.out.println("Enter your X value");
    int x = in.nextInt();

    System.out.println("Enter your Y value");
    int y = in.nextInt();

    Double[] pointIn = new Double[]{
        Double.valueOf(x),
        Double.valueOf(y),
        0.0
    };


    JSONParser parser = new JSONParser();
    JSONArray jsonArray = (JSONArray) parser.parse(new FileReader("E:\\data\\coordinates.json"));


    List<Double[]> points = new ArrayList<>();

    for (Object o : jsonArray) {
        JSONObject data = (JSONObject) o;

        String id = (String) data.get("id");
        System.out.print("id:" + id+"\t");      

        JSONArray value = (JSONArray) data.get("value");

        Double[] point = new Double[]{
            Double.valueOf(value.get(0).toString()),
            Double.valueOf(value.get(1).toString()),
            0.0
        };

        points.add(point);

        System.out.print("Point: " + ArrayUtils.toString(point));

        System.out.println("\n");
    }

    //Find the neighbors
    List<Double[]> neighbors = minimumDistance(points, pointIn);

    System.out.println("Ordered neighbors: ");

    for(Double[] point : neighbors){
        System.out.println(ArrayUtils.toString(point));
    }

    in.close();
}

Output:

Enter your X value
93
Enter your Y value
5
id:a    Point: {31.0,49.0,0.0}

id:b    Point: {44.0,67.0,0.0}

id:c    Point: {93.0,6.0,0.0}

id:d    Point: {20.0,16.0,0.0}

id:e    Point: {68.0,53.0,0.0}

id:f    Point: {71.0,8.0,0.0}

id:y    Point: {75.0,92.0,0.0}

id:z    Point: {32.0,33.0,0.0}

Ordered neighbor: 
{93.0,6.0,1.0}
{71.0,8.0,22.20360331117452}
{68.0,53.0,54.120236510939236}
{32.0,33.0,67.11929677819934}
{20.0,16.0,73.824115301167}
{31.0,49.0,76.02631123499285}
{44.0,67.0,79.02531240052139}
{75.0,92.0,88.84255736976509}

Please note: I am using ArrayUtils library from Apache (org.apache.commons.lang.ArrayUtils)

I hope this useful. Regards!

Gonza
  • 193
  • 3
  • 11