odległość między punktami - jak przejść z 2d i 3d do xd?

0

Mam kod obliczający odległość między punktami na płaszczyźnie:

public class Point {
    private double x;
    private double y;
    public static Point zero = new Point();
    public Point() {
    }
    public Point(double x, double y) {
        this.x = x;
        this.y = y;
    }
    @Override  // adnotacja
    public String toString() {
        return "[" + x + "," + y + "]";
    }
    public double distance(Point x) {
        return distance(this, x);
    }
    public static double distance(Point x, Point y) {
        double ret=0;
        ret += Math.pow(x.x - y.x, 2);
        ret += Math.pow(x.y - y.y, 2);
        return Math.sqrt(ret);
    }
    public static void main(String[] args) {
        Point a = new Point(2, 3);
        Point b = new Point(4, 1);
        System.out.println(a.distance (Point.zero));
        System.out.println(Point.distance(a, Point.zero));
        System.out.println(a.distance(a,b));
        System.out.println(Point.distance(a, b));
    }
}
'''

na tej podstawie zrobiłem kod obliczający odległość między punktami w przestrzeni 3d: 

'''java
import java.util.Scanner;
public class PointSpace {
    //    private double [] z;
    private double x;
    private double y;
    private double z;
    public static PointSpace zero = new PointSpace();
    public PointSpace() {
    }
    public PointSpace(double x, double y, double z) {
        this.x = x;
        this.y = y;
        this.z = z;
    }
    @Override
    public String toString() {
        return "(" + x + "," + y + "," + z + ")";
    }
    public double distance(PointSpace x) {
        return distance(this, x);
    }
    public static double distance(PointSpace x, PointSpace y) {
        double ret = 0;
        ret += Math.pow(x.x - y.x, 2);
        ret += Math.pow(x.y - y.y, 2);
        ret += Math.pow(x.z - y.z, 2);
        return Math.sqrt(ret);
    }
    public static void main(String[] args) {
        PointSpace a = new PointSpace(2, 2, 2);
        PointSpace b = new PointSpace(4, 3, 2);
        System.out.format("The distance between point a" + a + "and origin of coordinates is: %.2f\n", a.distance(PointSpace.zero));
        System.out.format("The distance between point a" + b + "and origin of coordinates is: %.2f\n", b.distance(PointSpace.zero));
        System.out.format("The distance between point a" + a + "and point b" + b + " is: %.2f", a.distance(a, b));
    }
}

a jak to uogólnić, żeby mogło być dla dowolnej przestrzeni?
Proszę o wyrozumiałość, bo to pewnie proste, ale nie dla mnie... Dopiero raczkuję.
Próbowałem to zrobić tablicami, ale nie mogę sobie poradzić z konwersją...

4

Dane są 2 punkty N-wymiarowe w postaci A(x1,x2,x3,x4,...,xn) oraz B(y1,y2,y3,y4,...,yn), wektor BN=[y1-x1, y2-x2,...,yn-xn] czyli odejmujesz początek od końca na każdej współrzędnej.
Długość takiego wektora to suma kwadratów każdego wymiaru wektora więc dist = (y1-x1)^2 + (y2-x2)^2 + (y3-x3)^2 + .... + (yn-xn)^2

1

Może tak:

public class Point
{
    private ArrayList<Double> coordinates = new ArrayList<>();
    public Point(double... coordinates)
    {
         for(double coordinate: coordinates)
         {
             this.coordinates.add(coordinate);
         }
    }
    public double distance(Point point)
    {
         int dimension = Math.min(coordinates.size(), point.coordinates.size());
         double squareDistance = 0.0;
         for(int i = 0; i < dimension; i++)
         {
             squareDistance += Math.pow(coordinates.get(i) - point.coordinates.get(i), 2);
         }
         return Math.sqrt(squareDistance);
    }
    public static void main(String[] args)
    {
        Point A = new Point(3.0, 4.0, 7.0);
        Point B = new Point(0, 0);
        System.out.println(A.distance(B)); => 5.0
        B = new Point(0);
        System.out.println(A.distance(B)); => 3.0
    }
}

Punkty mogą należeć do przestrzeni różnych wymiarów. Powyższy kod obliczy odległość między punktem z przestrzeni o mniejszym wymiarze od rzutu drugiego punktu.

0

W formie uogólnionej dla przestrzeni n-wymiarowych można np. tak:

public final class NVector {

  private final List<Double> dimensions;
  

  public NVector(Double... dimensions) {
    this.dimensions = Collections.unmodifiableList(Arrays.asList(dimensions));
  }

  public NVector(final List<Double> dimensions) {
    this.dimensions = Collections.unmodifiableList(new ArrayList<>(dimensions));
  }

  public NVector minus(NVector nvector) {
    if (this.dimensions.size() != nvector.dimensions.size()) {
      throw new UnsupportedOperationException("Length of the vector doesn't match");
    }

    List<Double> resultDimensions =
        IntStream.range(0, this.dimensions.size())
            .mapToDouble(i -> this.dimensions.get(i) - nvector.dimensions.get(i))
            .boxed()
            .collect(Collectors.toList());

    return new NVector(resultDimensions);
  }

  public double euclideanNorm() {
    return Math.sqrt(
        this.dimensions.stream().mapToDouble(value -> value).map(d -> Math.pow(d, 2.)).sum());
  }

  public double manhattanNorm() {
    return this.dimensions.stream().mapToDouble(value -> value).sum();
  }

  @Override
  public boolean equals(final Object obj) {
    if (!(obj instanceof NVector)) {
      return false;
    }
    NVector otherVector = (NVector) obj;
    if (this.dimensions.size() != otherVector.dimensions.size()) {
      return false;
    }
    return Math.abs(this.minus(otherVector).manhattanNorm()) < 1e-6;
  }

  @Override
  public int hashCode() {
    return Objects.hash(dimensions);
  }
}

import static org.junit.Assert.assertEquals;

import org.junit.Test;

public class NVectorTest {

  @Test
  public void diff4() {
    NVector startPoint = new NVector(0., 0., 0., 0.);
    NVector endPoint = new NVector(1., 1., 1., 1.);

    NVector resultVector = endPoint.minus(startPoint);

    assertEquals(endPoint, resultVector);
  }

  @Test
  public void euclideNorm4() {
    NVector vector4 = new NVector(1., 1., 1., 1.);
    double euclideanNorm = vector4.euclideanNorm();

    assertEquals(2., euclideanNorm, 1e-6);
  }

  @Test
  public void manhattanNorm4() {
    NVector vector4 = new NVector(1., 1., 1., 1.);
    double manhattanNorm = vector4.manhattanNorm();

    assertEquals(4., manhattanNorm, 1e-6);
  }
}
0

To jest niesamowite, Panowie się po prostu bawicie tym co dla mnie jest jeszcze ciężką pracą. Podziwiam i dziękuję, No a przede mną znowu praca: muszę to wszystko zrozumieć :)

1 użytkowników online, w tym zalogowanych: 0, gości: 1