about summary refs log tree commit diff
path: root/usth/ICT2.2/final/Point.java
blob: 5e794281432185979455a512b6ad361a775dabc3 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
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());
  }
}