diff options
author | Jesse Morgan <jesse@jesterpm.net> | 2016-04-09 14:22:20 -0700 |
---|---|---|
committer | Jesse Morgan <jesse@jesterpm.net> | 2016-04-09 15:48:01 -0700 |
commit | 3102d8bce3426d9cf41aeaf201c360d342677770 (patch) | |
tree | 38c4f1e8828f9af9c4b77a173bee0d312b321698 /src/main/java/com/p4square/grow/model/Point.java | |
parent | bbf907e51dfcf157bdee24dead1d531122aa25db (diff) |
Switching from Ivy+Ant to Maven.
Diffstat (limited to 'src/main/java/com/p4square/grow/model/Point.java')
-rw-r--r-- | src/main/java/com/p4square/grow/model/Point.java | 79 |
1 files changed, 79 insertions, 0 deletions
diff --git a/src/main/java/com/p4square/grow/model/Point.java b/src/main/java/com/p4square/grow/model/Point.java new file mode 100644 index 0000000..e9fc0ca --- /dev/null +++ b/src/main/java/com/p4square/grow/model/Point.java @@ -0,0 +1,79 @@ +/* + * Copyright 2013 Jesse Morgan + */ + +package com.p4square.grow.model; + +/** + * Simple double based point class. + * + * @author Jesse Morgan <jesse@jesterpm.net> + */ +public class Point { + /** + * Parse a comma separated x,y pair into a point. + * + * @return The point represented by the string. + * @throws IllegalArgumentException if the input is malformed. + */ + public static Point valueOf(String str) { + final int comma = str.indexOf(','); + if (comma == -1 || comma == 0 || comma == str.length() - 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; + + /** + * Create a new point with the given coordinates. + * + * @param x The x coordinate. + * @param y The y coordinate. + */ + public Point(double x, double y) { + mX = x; + mY = y; + } + + /** + * Compute the distance between this point and another. + * + * @param other The other point. + * @return The distance between this point and other. + */ + public double distance(Point other) { + final double dx = mX - other.mX; + final double dy = mY - other.mY; + + return Math.sqrt(dx*dx + dy*dy); + } + + /** + * @return The x coordinate. + */ + public double getX() { + return mX; + } + + /** + * @return The y coordinate. + */ + public double getY() { + return mY; + } + + /** + * @return The point represented as a comma separated pair. + */ + @Override + public String toString() { + return String.format("%.2f,%.2f", mX, mY); + } +} |