diff options
Diffstat (limited to 'sem5/oop/m4/shapes/src/MyPoint.java')
-rw-r--r-- | sem5/oop/m4/shapes/src/MyPoint.java | 44 |
1 files changed, 44 insertions, 0 deletions
diff --git a/sem5/oop/m4/shapes/src/MyPoint.java b/sem5/oop/m4/shapes/src/MyPoint.java new file mode 100644 index 0000000..46f1ffb --- /dev/null +++ b/sem5/oop/m4/shapes/src/MyPoint.java @@ -0,0 +1,44 @@ + +public class MyPoint { + private double x, y; + + public MyPoint(double x, double y) { + this.x = x; + this.y = y; + } + + public double getX() { + return this.x; + } + public double getY() { + return this.y; + } + + /** + * Subtracts two vectors, thus returning the difference. + * @param b point to subtract from this. + * @return returns a MyPoint which is actually a vector + */ + public MyPoint sub(MyPoint b) { + // This is actually a vector and not a point + return new MyPoint(this.getX() - b.getX(), this.getY() - b.getY()); + } + /** + * Returns a new MyVector with positive components + */ + public MyPoint abs() { + return new MyPoint(Math.abs(this.getX()), Math.abs(this.getY())); + } + + /** + * Calculates the length of MyPoint as if it was a vector. + * @return + */ + public double modulo() { + return Math.sqrt(this.getX() * this.getX() + this.getY() * this.getY()); + } + + public String toString() { + return String.format("[%f, %f]", this.getX(), this.getY()); + } +} |