Java 如何找到半径为16(x;z)的给定点的中心点?
我正试图打印出中心点的位置。比较点的半径必须为16。仅当从中心开始计算的一个位置上有5个以上的点时打印 中心点是,它距离所有附近的点最近 我的代码给了我误报和重复。方法:Java 如何找到半径为16(x;z)的给定点的中心点?,java,coordinates,coordinate-systems,Java,Coordinates,Coordinate Systems,我正试图打印出中心点的位置。比较点的半径必须为16。仅当从中心开始计算的一个位置上有5个以上的点时打印 中心点是,它距离所有附近的点最近 我的代码给了我误报和重复。方法: HashMap map=newhashmap(); 对于(位置副本loc:locs){ HashSet-locoscopy=新的HashSet(locs); 移除(loc); 用于(位置复制位置2:位置透视){ 如果(距离(loc2.getX(),loc.getX(),loc2.getZ(),loc.getZ())5){ 写
HashMap map=newhashmap();
对于(位置副本loc:locs){
HashSet-locoscopy=新的HashSet(locs);
移除(loc);
用于(位置复制位置2:位置透视){
如果(距离(loc2.getX(),loc.getX(),loc2.getZ(),loc.getZ())5){
写入(“超过”+map.get(loc).size()+locToString(loc)+:“+getLocs(map.get(loc))+”
”,文件名,beta);
}
}
专用双距离(双x1、双x2、双z1、双z2){
返回Math.sqrt(Math.pow(x2-x1,2)+Math.pow(z2-z1,2));
}
类别:
公共类位置副本{
私有整数x,y,z;
私人弦世界;
公共位置复制(整数x、整数y、整数z、字符串世界){
这个.x=x;
这个。y=y;
这个。z=z;
这个世界=世界;
}
公共位置复制(位置生成程序){
this.x=spawner.getBlockX();
this.y=spawner.getBlockY();
this.z=spawner.getBlockZ();
this.world=spawner.getWorld().getName();
}
公共int getX(){
返回x;
}
公共int getZ(){
返回z;
}
公共位置getLoc(){
返回新位置(Bukkit.getWorld(world),x,y,z);
}
}
您的代码中有几个小问题:
应该是map.put(loc2.getLoc(),hs);
map.put(loc.getLoc(),hs);
- 您需要向类中添加一个
方法,因为您使用的hashCode
s将无法识别表示相同位置的对象HashSet
- 只搜索点组,而不搜索其中心
/**
* A class for comparing the distances for locations
*/
public class LocationDistance implements Comparable<LocationDistance> {
private Location location;
private double distance;
public LocationDistance(Location location, double distance) {
this.location = location;
this.distance = distance;
}
@Override
public String toString() {
return "LocationDistance [location=" + location + ", distance=" + distance + "]";
}
@Override
public int compareTo(LocationDistance other) {
return Double.compare(distance, other.getDistance());
}
public Location getLocation() {
return location;
}
public void setLocation(Location location) {
this.location = location;
}
public double getDistance() {
return distance;
}
public void setDistance(double distance) {
this.distance = distance;
}
}
/**
*用于比较位置距离的类
*/
公共类LocationDistance实现可比较{
私人位置;
私人双倍距离;
公共位置距离(位置,双倍距离){
这个位置=位置;
这个距离=距离;
}
@凌驾
公共字符串toString(){
返回“LocationDistance[location=“+location+”,distance=“+distance+”];
}
@凌驾
公共内部比较(位置距离其他){
返回Double.compare(distance,other.getDistance());
}
公共位置getLocation(){
返回位置;
}
公共无效设置位置(位置){
这个位置=位置;
}
公共双getDistance(){
返回距离;
}
公共空隙设置距离(双倍距离){
这个距离=距离;
}
}
输出为:
More than 6 2 0 0: <br>
More than 6 1 0 2: <br>
More than 6 2 0 1: <br>
More than 6 5 0 0: <br>
More than 6 4 0 1: <br>
More than 6 3 0 2: <br>
More than 6 10 0 30: <br>
More than 6 12 0 31: <br>
More than 6 8 0 36: <br>
More than 6 15 0 31: <br>
More than 6 13 0 33: <br>
More than 6 15 0 36: <br>
More than 6 18 0 34: <br>
More than 6 0 0 0: <br>
center found: Location [x=2, y=0, z=1]
center found: Location [x=13, y=0, z=33]
超过6200:
超过6102:
超过6 2 0 1:
超过6500:
超过6 4 0 1:
超过6 3 0 2:
超过6 10 0 30:
超过6120 31:
超过68036:
超过6150 31:
超过6130 33:
超过615036:
超过618034:
超过600:
找到中心:位置[x=2,y=0,z=1]
找到中心:位置[x=13,y=0,z=33]
这里最后两行是您要搜索的中心
/**
* A class for comparing the distances for locations
*/
public class LocationDistance implements Comparable<LocationDistance> {
private Location location;
private double distance;
public LocationDistance(Location location, double distance) {
this.location = location;
this.distance = distance;
}
@Override
public String toString() {
return "LocationDistance [location=" + location + ", distance=" + distance + "]";
}
@Override
public int compareTo(LocationDistance other) {
return Double.compare(distance, other.getDistance());
}
public Location getLocation() {
return location;
}
public void setLocation(Location location) {
this.location = location;
}
public double getDistance() {
return distance;
}
public void setDistance(double distance) {
this.distance = distance;
}
}
More than 6 2 0 0: <br>
More than 6 1 0 2: <br>
More than 6 2 0 1: <br>
More than 6 5 0 0: <br>
More than 6 4 0 1: <br>
More than 6 3 0 2: <br>
More than 6 10 0 30: <br>
More than 6 12 0 31: <br>
More than 6 8 0 36: <br>
More than 6 15 0 31: <br>
More than 6 13 0 33: <br>
More than 6 15 0 36: <br>
More than 6 18 0 34: <br>
More than 6 0 0 0: <br>
center found: Location [x=2, y=0, z=1]
center found: Location [x=13, y=0, z=33]