From 555c4cb38513db0f6402aef0500e145e165cb383 Mon Sep 17 00:00:00 2001 From: Alex Date: Thu, 12 Dec 2019 01:36:54 +0400 Subject: [PATCH] completed lecture 2 practice --- .../src/main/java/ru/atom/geometry/Bar.java | 74 +++++++++++++++++++ .../main/java/ru/atom/geometry/Geometry.java | 4 +- .../src/main/java/ru/atom/geometry/Point.java | 27 +++++-- .../ru/atom/geometry/BarBarCollisionTest.java | 2 - .../atom/geometry/BarPointCollisionTest.java | 2 - .../geometry/PointPointCollisionTest.java | 2 - 6 files changed, 98 insertions(+), 13 deletions(-) create mode 100644 lecture02/src/main/java/ru/atom/geometry/Bar.java diff --git a/lecture02/src/main/java/ru/atom/geometry/Bar.java b/lecture02/src/main/java/ru/atom/geometry/Bar.java new file mode 100644 index 0000000000..59d6aef9b4 --- /dev/null +++ b/lecture02/src/main/java/ru/atom/geometry/Bar.java @@ -0,0 +1,74 @@ +package ru.atom.geometry; + +public class Bar implements Collider { + + private final int leftX; + private final int bottomY; + private final int rightX; + private final int topY; + + public Bar(int firstCornerX, int firstCornerY, int secondCornerX, int secondCornerY) { + this.leftX = Math.min(firstCornerX, secondCornerX); + this.bottomY = Math.min(firstCornerY, secondCornerY); + this.rightX = Math.max(firstCornerX, secondCornerX); + this.topY = Math.max(firstCornerY, secondCornerY); + } + + @Override + public boolean equals(Object obj) { + if (this == obj) { + return true; + } + if (!(obj instanceof Bar)) { + return false; + } + Bar other = (Bar) obj; + return this.leftX == other.leftX && this.bottomY == other.bottomY + && this.rightX == other.rightX && this.topY == other.topY; + } + + /** + * Checks if val is inside range + * + * @param val of interest + * @param left boundary + * @param right boundary + * @return true if val is inside else false + */ + private boolean isInside(int val, int left, int right) { + return val >= left && val <= right; + } + + public int width() { + return rightX - leftX; + } + + public int height() { + return topY - bottomY; + } + + private int centerX() { + return leftX + (rightX - leftX) / 2; + } + + private int centerY() { + return bottomY + (topY - bottomY) / 2; + } + + @Override + public boolean isColliding(Collider other) { + if (other instanceof Point) { + Point point = (Point) other; + return isInside(point.getX(), leftX, rightX) && isInside(point.getY(), bottomY, topY); + } + if (other instanceof Bar) { + Bar bar = (Bar) other; + int meanWidth = (this.width() + bar.width()) / 2; + int meanHeight = (this.height() + bar.height()) / 2; + int centersXDist = Math.abs(this.centerX() - bar.centerX()); + int centersYDist = Math.abs(this.centerY() - bar.centerY()); + return centersXDist <= meanWidth && centersYDist <= meanHeight; + } + return false; + } +} diff --git a/lecture02/src/main/java/ru/atom/geometry/Geometry.java b/lecture02/src/main/java/ru/atom/geometry/Geometry.java index 79a8c95465..4ea895f5ae 100644 --- a/lecture02/src/main/java/ru/atom/geometry/Geometry.java +++ b/lecture02/src/main/java/ru/atom/geometry/Geometry.java @@ -22,7 +22,7 @@ private Geometry() { * @return new Bar */ public static Collider createBar(int firstCornerX, int firstCornerY, int secondCornerX, int secondCornerY) { - throw new UnsupportedOperationException(); + return new Bar(firstCornerX, firstCornerY, secondCornerX, secondCornerY); } /** @@ -30,6 +30,6 @@ public static Collider createBar(int firstCornerX, int firstCornerY, int secondC * @return new Point */ public static Collider createPoint(int x, int y) { - throw new UnsupportedOperationException(); + return new Point(x, y); } } diff --git a/lecture02/src/main/java/ru/atom/geometry/Point.java b/lecture02/src/main/java/ru/atom/geometry/Point.java index 6f13561350..6f7d33a1d8 100644 --- a/lecture02/src/main/java/ru/atom/geometry/Point.java +++ b/lecture02/src/main/java/ru/atom/geometry/Point.java @@ -3,9 +3,23 @@ /** * Template class for */ -public class Point /* super class and interfaces here if necessary */ { - // fields - // and methods +public class Point implements Collider { + + private final int x; + private final int y; + + public Point(int x, int y) { + this.x = x; + this.y = y; + } + + public int getX() { + return x; + } + + public int getY() { + return y; + } /** * @param o - other object to check equality with @@ -18,8 +32,11 @@ public boolean equals(Object o) { // cast from Object to Point Point point = (Point) o; + return x == point.x && y == point.y; + } - // your code here - throw new UnsupportedOperationException(); + @Override + public boolean isColliding(Collider other) { + return this.equals(other); } } diff --git a/lecture02/src/test/java/ru/atom/geometry/BarBarCollisionTest.java b/lecture02/src/test/java/ru/atom/geometry/BarBarCollisionTest.java index 127a6df537..5c2916589a 100644 --- a/lecture02/src/test/java/ru/atom/geometry/BarBarCollisionTest.java +++ b/lecture02/src/test/java/ru/atom/geometry/BarBarCollisionTest.java @@ -1,12 +1,10 @@ package ru.atom.geometry; -import org.junit.Ignore; import org.junit.Test; import static junit.framework.TestCase.assertTrue; import static org.junit.Assert.assertFalse; -@Ignore public class BarBarCollisionTest { @Test public void barSelfCollide() { diff --git a/lecture02/src/test/java/ru/atom/geometry/BarPointCollisionTest.java b/lecture02/src/test/java/ru/atom/geometry/BarPointCollisionTest.java index dc3b6c2b54..ddfa478df4 100644 --- a/lecture02/src/test/java/ru/atom/geometry/BarPointCollisionTest.java +++ b/lecture02/src/test/java/ru/atom/geometry/BarPointCollisionTest.java @@ -1,12 +1,10 @@ package ru.atom.geometry; -import org.junit.Ignore; import org.junit.Test; import static junit.framework.TestCase.assertTrue; import static org.junit.Assert.assertFalse; -@Ignore public class BarPointCollisionTest { @Test public void pointInsideBar() { diff --git a/lecture02/src/test/java/ru/atom/geometry/PointPointCollisionTest.java b/lecture02/src/test/java/ru/atom/geometry/PointPointCollisionTest.java index bee377589a..6b47f8a524 100644 --- a/lecture02/src/test/java/ru/atom/geometry/PointPointCollisionTest.java +++ b/lecture02/src/test/java/ru/atom/geometry/PointPointCollisionTest.java @@ -1,12 +1,10 @@ package ru.atom.geometry; -import org.junit.Ignore; import org.junit.Test; import static junit.framework.TestCase.assertTrue; import static org.junit.Assert.assertFalse; -@Ignore public class PointPointCollisionTest { @Test public void pointSelfCollide() {