Skip to main content
added 27 characters in body
Source Link
Yam Marcovic
  • 9.3k
  • 2
  • 28
  • 29
interface Point {
    Point convertTo(CoordinateSystem cs);
}

class CartesianPoint implements Point {
    CartesianPoint(double x, double y) {
        this.x = x;
        this.y = y;
    }

    public Point convertTo(CoordinateSystem cs) {
        return cs.convert(this);
    }

    double x;
    double y;
}

class PolarPoint implements Point {
    public PolarPoint(double angle, double radius) {
        this.angle = angle;
        this.radius = radius;
    }

    public Point convertTo(CoordinateSystem cs) {
        return cs.convert(this);
    }

    double angle;
    double radius;
}

interface CoordinateSystem {
    Point convert(CartesianPoint p);
    Point convert(PolarPoint p);

    double distance(Point p1, Point p2);
}

class CartesianCoordinateSystem implements CoordinateSystem {
    public Point convert(CartesianPoint p) {
        return p;
    }

    public Point convert(PolarPoint p) {
        double x = p.radius * Math.cos(p.angle);
        double y = p.radius * Math.sin(p.angle);
        return new CartesianPoint(x, y);
    }

    public double distance(Point p1, Point p2) {
        CartesianPoint cp1 = (CartesianPoint)p1.convertTo(this);
        CartesianPoint cp2 = (CartesianPoint)p2.convertTo(this);

        double a = cp1.x - cp2.x;
        double b = cp1.y - cp2.y;
        return Math.sqrt((a * a) + (b * b));
    }
}

class Program {
    public static void main(String[] args) {
        PolarPoint p1 = new PolarPoint(1.23, 5);
        CartesianPoint p2 = new CartesianPoint(4, 6);
        CartesianCoordinateSystem cs = new CartesianCoordinateSystem();
        
        CartesianPoint cp1 = (CartesianPoint)cs.convert(p1);
        System.out.println("P1=(" + cp1.x + "," + cp1.y + ")");
        System.out.println("P2=(" + p2.x + "," + p2.y + ")");
        System.out.println("DISTANCE=" + cs.distance(p1, p2));
    }
}
interface Point {
    Point convertTo(CoordinateSystem cs);
}

class CartesianPoint implements Point {
    CartesianPoint(double x, double y) {
        this.x = x;
        this.y = y;
    }

    public Point convertTo(CoordinateSystem cs) {
        return cs.convert(this);
    }

    double x;
    double y;
}

class PolarPoint implements Point {
    public PolarPoint(double angle, double radius) {
        this.angle = angle;
        this.radius = radius;
    }

    public Point convertTo(CoordinateSystem cs) {
        return cs.convert(this);
    }

    double angle;
    double radius;
}

interface CoordinateSystem {
    Point convert(CartesianPoint p);
    Point convert(PolarPoint p);

    double distance(Point p1, Point p2);
}

class CartesianCoordinateSystem implements CoordinateSystem {
    public Point convert(CartesianPoint p) {
        return p;
    }

    public Point convert(PolarPoint p) {
        double x = p.radius * Math.cos(p.angle);
        double y = p.radius * Math.sin(p.angle);
        return new CartesianPoint(x, y);
    }

    public double distance(Point p1, Point p2) {
        CartesianPoint cp1 = (CartesianPoint)p1.convertTo(this);
        CartesianPoint cp2 = (CartesianPoint)p2.convertTo(this);

        double a = cp1.x - cp2.x;
        double b = cp1.y - cp2.y;
        return Math.sqrt((a * a) + (b * b));
    }
}

class Program {
    public static void main(String[] args) {
        PolarPoint p1 = new PolarPoint(1.23, 5);
        CartesianPoint p2 = new CartesianPoint(4, 6);
        CartesianCoordinateSystem cs = new CartesianCoordinateSystem();
        
        CartesianPoint cp1 = (CartesianPoint)cs.convert(p1);
        System.out.println("P1=(" + cp1.x + "," + cp1.y + ")");
        System.out.println("P2=(" + p2.x + "," + p2.y + ")");
        System.out.println("DISTANCE=" + cs.distance(p1, p2));
    }
}
interface Point {
    Point convertTo(CoordinateSystem cs);
}

class CartesianPoint implements Point {
    CartesianPoint(double x, double y) {
        this.x = x;
        this.y = y;
    }

    public Point convertTo(CoordinateSystem cs) {
        return cs.convert(this);
    }

    double x;
    double y;
}

class PolarPoint implements Point {
    public PolarPoint(double angle, double radius) {
        this.angle = angle;
        this.radius = radius;
    }

    public Point convertTo(CoordinateSystem cs) {
        return cs.convert(this);
    }

    double angle;
    double radius;
}

interface CoordinateSystem {
    Point convert(CartesianPoint p);
    Point convert(PolarPoint p);

    double distance(Point p1, Point p2);
}

class CartesianCoordinateSystem implements CoordinateSystem {
    public Point convert(CartesianPoint p) {
        return p;
    }

    public Point convert(PolarPoint p) {
        double x = p.radius * Math.cos(p.angle);
        double y = p.radius * Math.sin(p.angle);
        return new CartesianPoint(x, y);
    }

    public double distance(Point p1, Point p2) {
        CartesianPoint cp1 = (CartesianPoint)p1.convertTo(this);
        CartesianPoint cp2 = (CartesianPoint)p2.convertTo(this);

        double a = cp1.x - cp2.x;
        double b = cp1.y - cp2.y;
        return Math.sqrt((a * a) + (b * b));
    }
}

class Program {
    public static void main(String[] args) {
        PolarPoint p1 = new PolarPoint(1.23, 5);
        CartesianPoint p2 = new CartesianPoint(4, 6);
        CartesianCoordinateSystem cs = new CartesianCoordinateSystem();
        
        CartesianPoint cp1 = (CartesianPoint)cs.convert(p1);
        System.out.println("P1=(" + cp1.x + "," + cp1.y + ")");
        System.out.println("P2=(" + p2.x + "," + p2.y + ")");
        System.out.println("DISTANCE=" + cs.distance(p1, p2));
    }
}
interface Point {
    Point convertTo(CoordinateSystem cs);
}

class CartesianPoint implements Point {
    CartesianPoint(double x, double y) {
        this.x = x;
        this.y = y;
    }

    public Point convertTo(CoordinateSystem cs) {
        return cs.convert(this);
    }

    double x;
    double y;
}

class PolarPoint implements Point {
    public PolarPoint(double angle, double radius) {
        this.angle = angle;
        this.radius = radius;
    }

    public Point convertTo(CoordinateSystem cs) {
        return cs.convert(this);
    }

    double angle;
    double radius;
}

interface CoordinateSystem {
    Point convert(CartesianPoint p);
    Point convert(PolarPoint p);

    double distance(Point p1, Point p2);
}

class CartesianCoordinateSystem implements CoordinateSystem {
    public Point convert(CartesianPoint p) {
        return p;
    }

    public Point convert(PolarPoint p) {
        double x = p.radius * Math.cos(p.angle);
        double y = p.radius * Math.sin(p.angle);
        return new CartesianPoint(x, y);
    }

    public double distance(Point p1, Point p2) {
        CartesianPoint cp1 = (CartesianPoint)p1.convertTo(this);
        CartesianPoint cp2 = (CartesianPoint)p2.convertTo(this);

        double a = cp1.x - cp2.x;
        double b = cp1.y - cp2.y;
        return Math.sqrt((a * a) + (b * b));
    }
}

class Program {
    public static void main(String[] args) {
        PolarPoint p1 = new PolarPoint(1.23, 5);
        CartesianPoint p2 = new CartesianPoint(4, 6);
        CartesianCoordinateSystem cs = new CartesianCoordinateSystem();
        
        CartesianPoint cp1 = (CartesianPoint)cs.convert(p1);
        System.out.println("P1=(" + cp1.x + "," + cp1.y + ")");
        System.out.println("P2=(" + p2.x + "," + p2.y + ")");
        System.out.println("DISTANCE=" + cs.distance(p1, p2));
    }
}
Source Link
Yam Marcovic
  • 9.3k
  • 2
  • 28
  • 29

As mentioned by @Erik, the interface exposes the fact that it's implemented using the Cartesian coordinate system.

This is only a theoretical argument, as sometimes it's inappropriate not to design points as simple (x,y), for such reasons as instant familiarity and performance. But extending the theoretical argument, you could say that it's a CoordinateSystem's function to give you a numeric description of the location of a particular point. Just an an example, you could use double dispatch to convert a point from one coordinate system to another. E.g. in here.

interface Point {
    Point convertTo(CoordinateSystem cs);
}

class CartesianPoint implements Point {
    CartesianPoint(double x, double y) {
        this.x = x;
        this.y = y;
    }

    public Point convertTo(CoordinateSystem cs) {
        return cs.convert(this);
    }

    double x;
    double y;
}

class PolarPoint implements Point {
    public PolarPoint(double angle, double radius) {
        this.angle = angle;
        this.radius = radius;
    }

    public Point convertTo(CoordinateSystem cs) {
        return cs.convert(this);
    }

    double angle;
    double radius;
}

interface CoordinateSystem {
    Point convert(CartesianPoint p);
    Point convert(PolarPoint p);

    double distance(Point p1, Point p2);
}

class CartesianCoordinateSystem implements CoordinateSystem {
    public Point convert(CartesianPoint p) {
        return p;
    }

    public Point convert(PolarPoint p) {
        double x = p.radius * Math.cos(p.angle);
        double y = p.radius * Math.sin(p.angle);
        return new CartesianPoint(x, y);
    }

    public double distance(Point p1, Point p2) {
        CartesianPoint cp1 = (CartesianPoint)p1.convertTo(this);
        CartesianPoint cp2 = (CartesianPoint)p2.convertTo(this);

        double a = cp1.x - cp2.x;
        double b = cp1.y - cp2.y;
        return Math.sqrt((a * a) + (b * b));
    }
}

class Program {
    public static void main(String[] args) {
        PolarPoint p1 = new PolarPoint(1.23, 5);
        CartesianPoint p2 = new CartesianPoint(4, 6);
        CartesianCoordinateSystem cs = new CartesianCoordinateSystem();
        
        CartesianPoint cp1 = (CartesianPoint)cs.convert(p1);
        System.out.println("P1=(" + cp1.x + "," + cp1.y + ")");
        System.out.println("P2=(" + p2.x + "," + p2.y + ")");
        System.out.println("DISTANCE=" + cs.distance(p1, p2));
    }
}

That's the basic point (pun intended).