こんにちは私は元の質問が次のリンクで見つけることができますが、私はそれを私の実装に関連付けていなかったので保留にされたので、今これを行います。
別のスレッドでこの問題の解決策を見つけましたが、出力は妥当なように見えますが、これでは常に間違った結果が得られます...
私のコードは単純明快です。問題を特定するのに役立つポイントクラス(ポイントは常に正確な整数上にあるため、java.awt.Pointではありません)があります。
public static class Point {
/**
* the x position of this position
*/
private int x;
/**
* the y position of this position
*/
private int y;
public Point(int x, int y) {
this.x = x;
this.y = y;
}
public int getX() {
return x;
}
public void setX(int x) {
this.x = x;
}
public int getY() {
return y;
}
public void setY(int y) {
this.y = y;
}
/**
* calculates the Euclidean distance between this position and another one.
*
* @param point the position to which the distance should be calculated.
* @return the Euclidean distance
*/
public int getDistance(Point point) {
int dx = Math.abs(x - point.getX());
int dy = Math.abs(y - point.getY());
return (int) Math.sqrt(Math.pow(dx, 2) + Math.pow(dy, 2));
}
/**
* calculates the angle between two points.
*
* @param target the point to which the angle should be calculated
* @return the angle in degrees
*/
public double getAngle(Point target) {
double angle = Math.toDegrees(Math.atan2(target.y - y, target.x - x));
if (angle < 0) {
angle += 360;
}
return angle;
}
public Point getBestIntersectionPoint(int mySpeed, Point target, int targetSpeed, Point targetsTarget) {
double distance = getDistance(target);
double angle = 180 - Math.abs(target.getAngle(this) - target.getAngle(targetsTarget));
double a = Math.pow(mySpeed, 2) - Math.pow(targetSpeed, 2);
double b = 2 * distance * targetSpeed * Math.cos(Math.toRadians(angle));
double c = -Math.pow(distance, 2);
System.out.println("a: " + a);
System.out.println("b: " + b);
System.out.println("c: " + c);
//Finding out the roots
double temp1 = Math.sqrt((Math.pow(b, 2)) - 4 * a * c);
double root1 = (-b + temp1) / (2 * a);
double root2 = (-b - temp1) / (2 * a);
Point intersection1 = target.getPositionInTurns(targetsTarget, targetSpeed, root1);
Point intersection2 = target.getPositionInTurns(targetsTarget, targetSpeed, root2);
int distance1 = intersection1.getDistance(target);
int distance2 = intersection2.getDistance(target);
int targetDistance = target.getDistance(targetsTarget);
System.out.println(angle + " - " + intersection1 + "; " + intersection2);
return null;
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (!(o instanceof Point)) return false;
Point point = (Point) o;
if (getX() != point.getX()) return false;
return getY() == point.getY();
}
}
ここでの問題は、メソッド「getBestIntersectionPoint」です。たとえば、オブジェクト1が座標(x = 1100、y = 1200)にあり、オブジェクト2が(x = 8250、y = 4500)にあり、オブジェクト3が(x = 8250、y = 8999)にあるとすると、速度は同じに保たれます。他のスレッドの写真のように。正しい交点はほぼ上にある必要があります(x = 8250、y = 5000)。しかし、この実装では、このポイントにさえ近くない2つの交差点が得られます...
ポイント{x = 8250、y = -4738}; ポイント{x = 8250、y = 8538}
thx Salixが助けてくれましたが、数式にいくつかの間違いがありました。この問題を解決するのに少し時間がかかりましたが、これがそのトリックを実行する実装済みのメソッドです。
public Point getBestIntersectionPoint(double v1, Point p2, double v2, Point p3) {
int diffX = p3.getX() - p2.getX();
int diffY = p3.getY() - p2.getY();
Point u = new Point(
diffX == 0 ? 0 : diffX / Math.abs(diffX),
diffY == 0 ? 0 : diffY / Math.abs(diffY));
double p1x = getX();
double p1y = getY();
double p2x = p2.getX();
double p2y = p2.getY();
double a = v1 * v1 - v2 * v2 * (u.getX() * u.getX() + u.getY() * u.getY());
double b = (v2 * (u.getX() * (2 * p1x - 2 * p2x) + u.getY() * (2 * p1y - 2 * p2y)));
double c = -p2x * p2x - p2y * p2y - p1x * p1x - p1y * p1y + 2 * p2x * p1x + 2 * p2y * p1y;
double temp = Math.sqrt(Math.pow(b, 2) - 4 * a * c);
double t1, t2; //This is now a double, too.
t1 = (-b + temp) / (2 * a);
t2 = (-b - temp) / (2 * a);
Point s2_1 = new Point(
(int) (t1 * v2 * u.getX() + p2x),
(int) (t1 * v2 * u.getY() + p2y));
Point s2_2 = new Point(
(int) (t2 * v2 * u.getX() + p2x),
(int) (t2 * v2 * u.getY() + p2y));
Point ut1 = new Point((int) (s2_1.getX() - p2x), (int) (s2_1.getY() - p2y));
ut1.setX(ut1.getX() / Math.abs(ut1.getX()));
ut1.setY(ut1.getY() / Math.abs(ut1.getY()));
Point ut2 = new Point((int) (s2_2.getX() - p2x), (int) (s2_2.getY() - p2y));
ut2.setX(ut2.getX() / Math.abs(ut2.getX()));
ut2.setY(ut2.getY() / Math.abs(ut2.getY()));
return ut1.equals(u) ? s2_1 : ut2.equals(u) ? s2_2 : null;
}
この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。
侵害の場合は、連絡してください[email protected]
コメントを追加