从用户坐标中找到最近点 [英] find closest point from users coordinates

查看:111
本文介绍了从用户坐标中找到最近点的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道如何使用坐标找到关闭点。我使用了一个包含坐标和字符串的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;
返回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;




$ b public static void main(String args []){

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

map.put(新Coords(250,140),Clifton街道);
map.put(新的Coords(195,115),twoli);
map.put(新的Coords(165,95),Jacobs well);
map.put(新Coords(140,90),moxbridge);
map.put(新Coords(55,95),大路);
map.put(新Coords(15,120),easton);
map.put(新Coords(260,25),Weston on shore);
map.put(新Coords(250,60),newbridge);
map.put(新Coords(185,85),中央);
map.put(新Coords(140,100),stdennis);
map.put(新Coords(85,140),trim bridge);
map.put(新的Coords(170,35),windmill hill);
map.put(新的Coords(150,60),莎士比亚法院);
map.put(新的Coords(95,50),temple fields);
map.put(新Coords(55,125),pirac cresent);
map.put(新Coords(150,155),st judes hill);

扫描仪输入=新的扫描仪(System.in);
int i;
int a;
int b;

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

System.out.println(从以下选择);
System.out.println(find closest station = 1);
System.out.println(plan train route = 2);
i = input.nextInt();

if(i == 1){
System.out.println(输入您的x轴);
a = input.nextInt();
System.out.println(输入你的y轴);
b = input.nextInt();

System.out.println(最近的车站是);
}
else if(i == 2){
System.out.println(route planner);
}
else {
System.out.println(输入不正确的数字);




$ b

解决方案

首先,我建议你接受KevinMangold的建议,因为Java提供了一个非常合适的 Point 课程供您使用。






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



以下是一些使用 Collections.min

 最终地图< Point,String> names = ...; 
final Set< Point> stations = names.keySet();
final点源= ...;
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);


I would like to know how to use find the closes point using coordinates. i am using a Hashmap which holds coordinates and strings. i have allowed the user to input an x and y axis and store them as int a and int b but i don't know where to go from there. thanks for looking

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

    }
}
}

解决方案

First, I advise you take KevinMangold's advice, seeing as Java provides a perfectly suitable Point class for you to use.


This is a minimization problem. Essentially, you want to compute the distance between the entered point and every known station using some metric (perhaps Euclidean distance?). Then, you pick the station corresponding to the minimum distance found.

Here's some example code using 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);

这篇关于从用户坐标中找到最近点的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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