-1

最近、Point2DポイントとPoint3Dポイントを表す2つのクラスを実装するように依頼されました。距離やその他の方法は問題なく機能しますが、実装する必要があるのは1つだけであり、アルゴリズムを理解できません。それはこのように述べられています:

「空間内のポイントのセットを指定して、空間内の別のポイントに最も近いセットからそのポイントを決定します(セット内にありません)。」

以下は2つのクラスです。誰かが最後に必要なアルゴリズムを手伝ってくれますか?

    public class Point2D {
    protected double x;
    protected double y;

    public Point2D() { }

    public Point2D(double x, double y)
    {
        this.x = x;
        this.y = y;
    }

    public double getX() { return x; }
    public void setX(double x) { this.x = x; }
    public double getY() { return y; }
    public void setY(double y) { this.y = y; }

    public double Point2DDistance(Point2D punct)
    {
        double px = this.x - punct.x;
        double py = this.y - punct.y;

        return Math.sqrt((px * px) + (py * py));
    }

    public String toString() {
        return "(" + x + ", " + y + ")";
    } 
}

public class Point3D extends Point2D {
    private double z;

    public Point3D() { }

    public Point3D(double x, double y, double z)
    {
        super(x, y);
        this.z = z;
    }

    public double getZ() { return z; }
    public void setZ(double z) { this.z = z; }

    public double Point3DDistance(Point3D punct)
    {
        double pz = this.z - punct.z;

        return this.Point2DDistance(punct) + Math.sqrt(pz * pz);
    }

    public String toString() {
        return "(" + this.x + ", " + this.y + ", " + this.z + ")";
    }
}
4

1 に答える 1

2

ポイントを繰り返します。

各ポイントについて、

  • これが最初の点である場合、set min_distance=設定されていない点PclosestPまでの点の距離を設定し、この点を、にコピーします。

  • それ以外の場合、このポイントのPまでの距離が、未満の場合は、このポイントの距離min_distanceに設定min_distanceし、このポイントをにコピーしclosestPます。

ここclosestPで、最も近い点を保持し、 Pmin_distanceからの距離を保持します。

やあ!

于 2012-11-12T13:39:07.313 に答える