[英]Trying to calculate the distance between the position of two objects
我有這兩個實例變量,它們在對象的構造函數中設置為:
(int)(Math.random()*100);
這兩個實例變量是:
private double xPos;
private double yPos;
該類稱為Soldier,我還有其他三個繼承自Soldier的類(目前僅是構造函數)
我現在得到的輸出是:
我的名字是:騎士我的位置:(45.0,56.0)
我的名字是:Crossbowman我現在的位置:(15.0,91.0)
我的名字是:Halberdier我現在的位置:(67.0,8.0)
我正在嘗試計算對象在x和y位置的距離
我目前正在嘗試的方法是:
protected Soldier distanceBetween(Soldier x, Soldier y){
double distBetween = (x.xPos - y.xPos)+(x.yPos-y.yPos);
return this;
}
我試圖實現的方法是將兩個繼承自Soldier的對象放入divBetween參數中,例如,我使用名稱:halberdier,cavalier和crossbowman。
當我調用此方法時:
cavalier.distanceBetween(cavalier,crossbowman);
我希望它計算x和y坐標之間的距離
我將如何實現?
完全錯誤。
您需要返回值,而不是士兵。
這是更一般的:
public static double distanceBetween(double x1, double y1, double x2, double y2) {
double dx = Math.abs(x2-x1);
double dy = Math.abs(y2-y1);
if (dx > dy) {
double r = dy/dx;
return dx*Math.sqrt(1.0 + r*r);
} else {
double r = dx/dy;
return dy*Math.sqrt(1.0 + r*r);
}
}
^
運算符不取冪; 是XOR。
您可以通過以下方式覆蓋此方法:
public static double distanceBetween(Soldier s1, Solder s2) {
return distanceBetween(s1.xPos, s1.yPos, s2.xPos, s2.yPos);
}
既然您遇到了麻煩,我會為您說明:
/**
* Distance calc
* User: mduffy
* Date: 11/1/2015
* Time: 9:58 AM
* @link http://stackoverflow.com/questions/33462961/trying-to-calculate-the-distance-between-the-position-of-two-objects/33463222?noredirect=1#comment54712511_33463222
*/
public class Soldier {
public final double xPos;
public final double yPos;
public static void main(String[] args) {
Soldier s = new Soldier(0, 0);
Cavalier c = new Cavalier(3, 4);
System.out.println(String.format("distance: %f10.3", s.distanceBetween(c)));
}
public Soldier(double xPos, double yPos) {
this.xPos = xPos;
this.yPos = yPos;
}
public double distanceBetween(Soldier s) {
return distanceBetween(this.xPos, this.yPos, s.xPos, s.yPos);
}
public static double distanceBetween(double x1, double y1, double x2, double y2) {
double dx = Math.abs(x2-x1);
double dy = Math.abs(y2-y1);
if (dx > dy) {
double r = dy/dx;
return dx*Math.sqrt(1.0 + r*r);
} else {
double r = dx/dy;
return dy*Math.sqrt(1.0 + r*r);
}
}
public static double distanceBetween(Soldier s1, Soldier s2) {
return distanceBetween(s1.xPos, s1.yPos, s2.xPos, s2.yPos);
}
}
class Cavalier extends Soldier {
public Cavalier(double x, double y) {
super(x, y);
}
}
如果您想要不同的距離計算方法(例如,歐幾里得,曼哈頓,皮爾遜,球面等),則可以使用一個接口,通過添加新的實現來更改它:
public interface DistanceCalculator {
double distance(double x1, double y1, double x2, double y2);
}
現在,您可以通過添加新代碼而不是修改現有代碼來輕松切換。
在Soldier類中,您需要類似此成員函數的東西。
public double ManhattanDistance (Soldier other)
{
return Math.Abs(this.xPos - other.xPos) + Math.Abs(this.xPos - other.xPos);
}
如果要笛卡爾-烏鴉-距離,則需要此成員函數。
public double Distance (Soldier other)
{
double dx = (this.xPos - other.xPos);
double dy = (this.yPos - other.yPos);
return Math.Sqrt(dx*dx + dy*dy);
}
要使用這些成員函數來得到你之間的距離cavalier
和你的crossbowman
你做這種事情。
double howFar = cavalier.Distance(crossbowman);
要么
double howManyBlocks = crossbowman.ManhattanDistance(cavalier);
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.