java - 从用户坐标中找到最近点

标签 java hashmap coordinates user-input

我想知道如何使用坐标查找闭合点。我正在使用一个包含坐标和字符串的 Hashmap。我已经允许用户输入 x 和 y 轴并将它们存储为 int a 和 int b 但我不知道从那里去哪里。感谢收看

import java.util.HashMap;
import java.util.Scanner;

public class Coordinate {

static class Coords {
    int x;
    int y;

    public boolean equals(Object o) {
        Coords c = (Coords) o;
        return c.x == x && c.y == y;
    }

    public Coords(int x, int y) {
        super();
        this.x = x;
        this.y = y;
    }

    public int hashCode() {
        return new Integer(x + "0" + y);
    }

    public String toString()
    {
        return x + ";" + y;
    }


}

public static void main(String args[]) {

    HashMap<Coords, String> map = new HashMap<Coords, String>();

    map.put(new Coords(250, 140), "Clifton street");
    map.put(new Coords(195, 115), "twoli");
    map.put(new Coords(165, 95), "Jacobs well");
    map.put(new Coords(140, 90), "moxbridge");
    map.put(new Coords(55, 95), "parkway");
    map.put(new Coords(15, 120), "easton");
    map.put(new Coords(260, 25), "Weston on shore");
    map.put(new Coords(250, 60), "newbridge");
    map.put(new Coords(185, 85), "central");
    map.put(new Coords(140, 100), "stdennis");
    map.put(new Coords(85, 140), "trim bridge");
    map.put(new Coords(170, 35), "windmill hill");
    map.put(new Coords(150, 60), "shakespeare court");
    map.put(new Coords(95, 50), "temple fields");
    map.put(new Coords(55, 125), "pirac cresent");
    map.put(new Coords(150, 155), "st judes hill");

    Scanner input = new Scanner(System.in);
    int i;
    int a;
    int b;

    System.out.println(map.size());
    System.out.println(map.toString());
    Coords c = new Coords(65,72);
    System.out.println(c + " - " + map.get(c));

    System.out.println("choose from the following");
    System.out.println("find closest station = 1");
    System.out.println("plan train route = 2");
    i = input.nextInt();

    if (i==1){
        System.out.println("enter your x axis ");
        a = input.nextInt();
        System.out.println("enter your y axis");
        b = input.nextInt();

        System.out.println("the nearest station is");
    }
    else if (i==2){
        System.out.println("route planner");
    }
    else {
        System.out.println("entered incorrect number");

    }
}
}

最佳答案

首先,我建议您采纳 KevinMangold 的建议,因为 Java 提供了一个非常合适的 Point类供您使用。


这是一个最小化问题。本质上,您想使用某种度量(可能是Euclidean distance?)计算输入点与每个已知站点之间的距离。然后,您选择与找到的最小距离相对应的站点。

这是一些使用 Collections.min 的示例代码:

final Map<Point, String> names = ...;
final Set<Point> stations = names.keySet();
final Point source = ...;
final Point nearest = Collections.min(stations, new Comparator<Point>() {

  public int compare(final Point p1, final Point p2) {
    return (int) p1.distanceSq(p2);
  }
});
final String name = names.get(nearest);

关于java - 从用户坐标中找到最近点,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11819082/

相关文章:

javascript - 获取线包围框的坐标

rotation - 控制一个 Sprite 的旋转,使其始终瞄准另一个 Sprite

java - 安卓显卡0x1​​04567911

java - 我应该如何升级我的 Java 编译器

c++ - 我如何重载模板类的下标运算符

rust - 实现返回HashMap::IntoIter的IntoIterator时的“wrong number of type arguments”

Java HashMap<String, ArrayList<Object[]>>如何添加一个空数组?

java - 代号 1 BrowserComponent setBrowserNavigationCallback

Java Printf 对齐输出

ruby - PDF 坐标如何工作?