about summary refs log tree commit diff
path: root/usth/ICT2.2/final/Point.java
diff options
context:
space:
mode:
Diffstat (limited to 'usth/ICT2.2/final/Point.java')
-rw-r--r--usth/ICT2.2/final/Point.java43
1 files changed, 43 insertions, 0 deletions
diff --git a/usth/ICT2.2/final/Point.java b/usth/ICT2.2/final/Point.java
new file mode 100644
index 0000000..5e79428
--- /dev/null
+++ b/usth/ICT2.2/final/Point.java
@@ -0,0 +1,43 @@
+// Immutable Point
+public class Point
+{
+  private double x;
+  private double y;
+
+  public Point(double x, double y)
+  {
+    this.x = x;
+    this.y = y;
+  }
+
+  public double getX()
+  {
+    return x;
+  }
+
+  public double getY()
+  {
+    return y;
+  }
+
+  public String toString()
+  {
+    return String.format("(%g, %g)", x, y);
+  }
+
+  public static Point add(Point a, Point b)
+  {
+    return new Point(a.getX() + b.getX(), a.getY() + b.getY());
+  }
+
+  public static Point subtract(Point a, Point b)
+  {
+    return new Point(a.getX() - b.getX(), a.getY() - b.getY());
+  }
+
+  public static double calDisEuclid(Point a, Point b)
+  {
+    var trans = Point.subtract(a, b);
+    return Math.sqrt(trans.getX()*trans.getX() + trans.getY()*trans.getY());
+  }
+}