2012-08-05 98 views
2

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

    } 
} 
} 
+1

你为什么不使用自带的Java Point类? – 2012-08-05 18:47:22

+0

@KevinMangold @ Djchunky123'awt.Point'代表屏幕上的像素,而不是通用坐标。它没有这个错误的/错误的'hashCode'方法 - 尝试输入'x = 101'和'y = 1'与'x = 1'和'y = 101':D – amon 2012-08-05 18:56:07

+0

@amon真的,但它确实包含可用于提供帮助的distance和distanceSq方法。 – 2012-08-05 18:59:47

回答

8

首先,我建议你把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); 
+0

使用这种方法相同的距离将重新计算多次。如果您需要最大化速度,您可能需要采用[另一种解决方案](http://stackoverflow.com/a/23042847/1600770)。 – Agostino 2015-04-08 22:25:35

+0

@Agostino距离只需要多行代码即可缓存 – oldrinb 2015-04-16 01:29:01