summaryrefslogtreecommitdiff
path: root/src/com/p4square/grow/backend/resources/Point.java
blob: e1b15a8eae88bdfa100b99a299ebbd666d37f2c9 (plain)
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
44
45
46
47
48
49
50
51
52
/*
 * Copyright 2013 Jesse Morgan
 */

package com.p4square.grow.backend.resources;

/**
 * Simple double based point class.
 *
 * @author Jesse Morgan <jesse@jesterpm.net>
 */
class Point {
    public static Point valueOf(String str) {
        final int comma = str.indexOf(',');
        if (comma == -1) {
            throw new IllegalArgumentException("Malformed point string");
        }

        final String sX = str.substring(0, comma);
        final String sY = str.substring(comma + 1);

        return new Point(Double.valueOf(sX), Double.valueOf(sY));
    }     

    private final double mX;
    private final double mY;

    public Point(double x, double y) {
        mX = x;
        mY = y;
    }

    public double distance(Point other) {
        final double dx = mX - other.mX;
        final double dy = mY - other.mY;

        return Math.sqrt(dx*dx + dy*dy);
    }

    public double getX() {
        return mX;
    }

    public double getY() {
        return mY;
    }

    @Override
    public String toString() {
        return String.format("%.2f,%.2f", mX, mY);
    }
}