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..328242287b --- /dev/null +++ b/lecture02/src/main/java/ru/atom/geometry/Bar.java @@ -0,0 +1,70 @@ +package ru.atom.geometry; + +import java.util.Objects; + +public class Bar implements Collider { + + private final Point lowerLeft; + private final Point upperRight; + + public Bar(int firstCornerX, int firstCornerY, int secondCornerX, int secondCornerY) { + lowerLeft = new Point(Math.min(firstCornerX, secondCornerX), Math.min(firstCornerY, secondCornerY)); + upperRight = new Point(Math.max(firstCornerX, secondCornerX), Math.max(firstCornerY, secondCornerY)); + } + + @Override + public boolean equals(Object o) { + if (this == o) { + return true; + } + if (o == null || getClass() != o.getClass()) { + return false; + } + Bar otherBar = (Bar) o; + return Objects.equals(lowerLeft, otherBar.lowerLeft) && Objects.equals(upperRight, otherBar.upperRight); + } + + @Override + public boolean isColliding(Collider other) { + if (other instanceof Point) { + return this.contains((Point) other); + } + if (other instanceof Bar) { + return this.intersects((Bar) other); + } + return false; + } + + private boolean intersects(Bar other) { + double thisCenterX = average(lowerLeft.getX(), upperRight.getX()); + double otherCenterX = average(other.lowerLeft.getX(), other.upperRight.getX()); + if (Math.abs(thisCenterX - otherCenterX) > average(this.width(), other.width())) { + return false; + } + + double thisCenterY = average(lowerLeft.getY(), upperRight.getY()); + double otherCenterY = average(other.lowerLeft.getY(), other.upperRight.getY()); + return Math.abs(thisCenterY - otherCenterY) <= average(this.height(), other.height()); + } + + public int width() { + return upperRight.getX() - lowerLeft.getX(); + } + + public int height() { + return upperRight.getY() - lowerLeft.getY(); + } + + private double average(int from, int to) { + return (from + to) / 2.0; + } + + private boolean contains(Point point) { + return isInside(point.getX(), lowerLeft.getX(), upperRight.getX()) + && isInside(point.getY(), lowerLeft.getY(), upperRight.getY()); + } + + private boolean isInside(int value, int start, int end) { + return value >= start && value <= end; + } +} diff --git a/lecture02/src/main/java/ru/atom/geometry/Geometry.java b/lecture02/src/main/java/ru/atom/geometry/Geometry.java index 79a8c95465..5799a6de1c 100644 --- a/lecture02/src/main/java/ru/atom/geometry/Geometry.java +++ b/lecture02/src/main/java/ru/atom/geometry/Geometry.java @@ -1,16 +1,16 @@ package ru.atom.geometry; /** - * ^ Y - * | - * | - * | - * | X - * .----------> + * ^ Y + * | + * | + * | + * | X + * .----------> */ public final class Geometry { - + private Geometry() { } @@ -19,17 +19,19 @@ private Geometry() { * Like selection bar in desktop, this bar is defined by two opposite corners * Bar is not oriented * (It is not relevant, which opposite corners you choose to define bar) + * * @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); } /** * 2D point + * * @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..b6ab79e5a9 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 @@ -19,7 +33,14 @@ public boolean equals(Object o) { // cast from Object to Point Point point = (Point) o; - // your code here - throw new UnsupportedOperationException(); + return this.x == point.x && this.y == point.y; + } + + @Override + public boolean isColliding(Collider other) { + if (other instanceof Point) { + return this.equals(other); + } + return false; } } diff --git a/lecture02/src/test/java/ru/atom/geometry/BarBarCollisionTest.java b/lecture02/src/test/java/ru/atom/geometry/BarBarCollisionTest.java index 127a6df537..50ddf3bd2c 100644 --- a/lecture02/src/test/java/ru/atom/geometry/BarBarCollisionTest.java +++ b/lecture02/src/test/java/ru/atom/geometry/BarBarCollisionTest.java @@ -1,12 +1,11 @@ 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..1baffc2d4a 100644 --- a/lecture02/src/test/java/ru/atom/geometry/BarPointCollisionTest.java +++ b/lecture02/src/test/java/ru/atom/geometry/BarPointCollisionTest.java @@ -1,12 +1,11 @@ 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() {