我已經作出了類位置,其允許設置,改變一個位置,(座標x,y,該限制由XMIN,XMAX,YMIN,YMAX決定),以caluculate之間的距離兩點,並從另一個位置獲取方向。
方向是從另一個位置遞減(在[0,2pi])。
方向從北變爲(假定北是在有較高的座標定向的杆),以順時針方向。損失精度計算度兩點
package TruckingCompany;
public class Location
{
private double x;
private double y;
private static final double xMax=1000.0;
private static final double xMin=-1000.0;
private static final double yMax=1000.0;
private static final double yMin=-1000.0;
public Location()
{
setX(0.0);
setY(0.0);
}
public Location(double x,double y)
{
setX(x);
setY(y);
}
public Location(Location location)
{
setX(location.getX());
setY(location.getY());
}
public void setX(double x)
{
if(x>=xMin && x<=xMax)
this.x=x;
}
public void setY(double y)
{
if(y>=yMin && y<=yMax)
this.y=y;
}
public void set(double x,double y)
{
setX(x);
setY(y);
}
public double getX()
{
return x;
}
public double getY()
{
return y;
}
public double getDistanceFrom(Location from)
{
double dx,dy;
dx=from.getX()-x;
dy=from.getY()-y;
return Math.sqrt(Math.pow(dx, 2.0)+Math.pow(dy, 2.0));
}
public double getDirectionFrom(Location from)
{
double dy=from.getY()-y;
double direction=Math.PI/4 - Math.asin (Math.toRadians(dy/getDistanceFrom(from)));
if(Double.isNaN(direction)==false)
{
if(from.getX()-x<0.0)
direction+=Math.PI/2;
if(dy<0.0)
direction+=Math.PI;
}
return direction;
}
@Override
public String toString()
{
return "(" + x + " , " + y + ")";
}
}
問題是精度,例如我嘗試這兩個位置計算距離:
Location l1,l2;
l1=new Location(0.0,0.0);
l2=new Location(300.0,300.0);
System.out.print(Math.toDegrees(l1.getDirectionFrom(l2)));
的問題是精度:在這個例子中它打印44.29度,它應該是45.0,爲什麼這麼大的精度損失?
旁註:你就不能使用'Math.atan2()'? – Mysticial 2012-03-01 20:30:10
您是否獲得了0,0-> 0,300和0,0-> 300,0的預期結果? – DNA 2012-03-01 20:32:09